-3

I'm not sure if this is even possible, but here goes.

I have a PHP function called code_check_result($code_name). It checks in the database and returns a bunch of information regarding the supplied code's name.

The name of the code is found within a span tag in the page. I've made a javascript function that opens a new window which should then execute the PHP function. However, the variable needed in the PHP function is stored in a jquery var as I have selected the span tag and stored whatever's inside.

Below you can see the part of the code where the window is created (just the part with the PHP function).

myWindow.document.write('Tjek: ' + code_name + '</br></br></br></br><?php code_check_result("kode2"); ?>');

"kode2" is the actual name of a code. The above works exactly as it's supposed to. However, if I substitute the "kode2" string with the javascript variable, 'code_name', it doesn't work. Like this:

myWindow.document.write('Tjek: ' + code_name + '</br></br></br></br><?php code_check_result("' + code_name + '"); ?>');

The first occurence of code_name writes it to the page, but the second one within the PHP function doesn't work.

I'm pretty sure this is just because the PHP is executed before the 'code_name' variable is written as a string. But I'm not entirely sure.

So, I'd appreciate it if you could either help me figuring out how to do this, OR tell me to abandon this idea because it's not possible and then give me some pointers as to how else to solve this.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
user3452005
  • 51
  • 1
  • 1
  • 4

2 Answers2

1

Given this:

the variable needed in the PHP function is stored in a jquery var

and this:

I've made a javascript function that opens a new window

It sounds like you just need to pass a value to the URL being opened in that window. So where you might have something like this:

window.open('somePage.php');

You would instead have this:

window.open('somePage.php?someValue=' + myVariable);

The URL of the window being opened is a normal URL like any other. You can include values on the query string, and the PHP code in somePage.php can read them from the$_GET collection.

David
  • 208,112
  • 36
  • 198
  • 279
0

It will be easier to create $_POST variable with jQuery. Something like this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script> 
    $.ajax({
        type: "POST",
        url: "[URL GOES HERE]",
        data:{ VARNAME: VALUE }, 
        success: function(data){
            console.log(data); 
        }
    })
</script>
RB_
  • 1,195
  • 15
  • 35