Passing PHP Variable to Javascript Function, Can't Open a Window in a Javascript function with a Parameter Pass |
Passing PHP Variable to Javascript Function, Can't Open a Window in a Javascript function with a Parameter Pass |
Jonah |
Feb 7 2017, 10:51 AM
Post
#1
|
Group: Members Posts: 6 Joined: 7-February 17 Member No.: 26,301 |
I apologize in advance if this has been answered but I've tried all the suggestions I've found but still no joy.
In PHP, I have pulled data from a database and displayed it in a table inserting a button in the first cell of each row titled with a PHP variable. Clicking that button should call a javascript function that opens a child window to display details of data on that row. Everything works fine except for the fact that I cannot pass a PHP variable to the javascript function. Passing hard-coded integer or string data appears to work. Here is the java script: <script language='javascript' type='text/javascript'> function myPopup(id) { window.open('flowdetail.php?uuid='+id, 'myWindow','status = 1, height = 300, width = 300, resizable = 0' ) }; </script> Here is the code in the target window (echos passed value just for testing): <?php // content="text/plain; charset=utf-8" $uuid = $_GET['uuid']; echo $uuid; ?> Here is the PHP that populates the table and offers the button: echo "><td><input name='found_one' type=button "; echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . " " . $results['location'] . "</td><td style='word-wrap: break-word'>" . substr($results['description'],0,140) . "</td><td>" . $cpname . "</td>"; I've been hacking at this for days so any help would be greatly appreciated! |
pandy |
Feb 7 2017, 11:05 AM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,732 Joined: 9-August 06 Member No.: 6 |
Not my forte, but shouldn't you split the below up so the PHP variable isn't quoted?
CODE echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . |
Christian J |
Feb 7 2017, 11:10 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Here is the code in the target window (echos passed value just for testing): <?php // content="text/plain; charset=utf-8" $uuid = $_GET['uuid']; echo $uuid; ?> What kind of value do you get from echo $uuid;? BTW, note that $_GET data must always be sanitized to prevent XSS exploits. QUOTE Here is the PHP that populates the table and offers the button: echo "><td><input name='found_one' type=button "; echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . " " . $results['location'] . "</td><td style='word-wrap: break-word'>" . substr($results['description'],0,140) . "</td><td>" . $cpname . "</td>"; The above might produce invalid HTML, to be on the safe side myPopup($uuid); should be inside (HTML) quotes. Does $uuid look correct in the rendered HTML? |
Jonah |
Feb 7 2017, 11:26 AM
Post
#4
|
Group: Members Posts: 6 Joined: 7-February 17 Member No.: 26,301 |
I tried quoting the js function 'myPopup($uuid)' but same result.
Not worried about the XSS right now - internal app. Echoing the $uuid variable in a cell will show the value. I've tried dropping in and out of PHP to echo the PHP variable to the function but still no luck. Thanks Here is the code in the target window (echos passed value just for testing): <?php // content="text/plain; charset=utf-8" $uuid = $_GET['uuid']; echo $uuid; ?> What kind of value do you get from echo $uuid;? BTW, note that $_GET data must always be sanitized to prevent XSS exploits. QUOTE Here is the PHP that populates the table and offers the button: echo "><td><input name='found_one' type=button "; echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . " " . $results['location'] . "</td><td style='word-wrap: break-word'>" . substr($results['description'],0,140) . "</td><td>" . $cpname . "</td>"; The above seems to produce invalid HTML, myPopup($uuid); should be inside (HTML) quotes. Does $uuid look correct in the rendered HTML? |
Jonah |
Feb 7 2017, 11:29 AM
Post
#5
|
Group: Members Posts: 6 Joined: 7-February 17 Member No.: 26,301 |
The quote is for PHP to display the whole line. I've tried dropping out and in to PHP to echo the variable to the function but no luck.
Not my forte, but shouldn't you split the below up so the PHP variable isn't quoted? CODE echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . |
Christian J |
Feb 7 2017, 11:37 AM
Post
#6
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
|
Christian J |
Feb 7 2017, 11:41 AM
Post
#7
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
The quote is for PHP to display the whole line. I've tried dropping out and in to PHP to echo the variable to the function but no luck. Not my forte, but shouldn't you split the below up so the PHP variable isn't quoted? CODE echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . Yes it seems correct, I've deleted my incorrect reply to pandy. @pandy: PHP variables echoed inside single quotes are indeed not expanded, but in this case the actual echo uses double quotes. |
Jonah |
Feb 7 2017, 11:53 AM
Post
#8
|
Group: Members Posts: 6 Joined: 7-February 17 Member No.: 26,301 |
If I enter a static value as a function argument, e.g. 1234 or "some text", it displays in the popup window. If I pass the PHP variable as the argument to the js function the window does not open.
Echoing the $uuid variable in a cell will show the value. What do you get in the actual popup page? |
Christian J |
Feb 7 2017, 03:17 PM
Post
#9
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
If I pass the PHP variable as the argument to the js function the window does not open. That sounds like either the javascript function call or the function itself is broken by the PHP variable's value. Does the value contain a single- or double-quote character? QUOTE Here is the PHP that populates the table and offers the button: echo "><td><input name='found_one' type=button "; echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . " " . $results['location'] . "</td><td style='word-wrap: break-word'>" . substr($results['description'],0,140) . "</td><td>" . $cpname . "</td>"; What does the above look like when rendered as HTML? QUOTE <script language='javascript' type='text/javascript'> function myPopup(id) { window.open('flowdetail.php?uuid='+id, 'myWindow','status = 1, height = 300, width = 300, resizable = 0' ) }; </script> What happens if you put an alertbox in the above function, like this: CODE <script> function myPopup(id) { alert(id); }; </script> ? Does the alertbox show the correct value for id? If the alertbox does not open, do you at least get a javascript error? |
Jonah |
Feb 7 2017, 03:49 PM
Post
#10
|
Group: Members Posts: 6 Joined: 7-February 17 Member No.: 26,301 |
The alert box will open and display the passed data only if it's static, not the PHP variable. Passing the variable prevents the alert from opening. I'm not seeing a js error displayed.
|
Christian J |
Feb 7 2017, 04:15 PM
Post
#11
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
The alert box will open and display the passed data only if it's static, not the PHP variable. Passing the variable prevents the alert from opening. I'm not seeing a js error displayed. That sounds like if the PHP variable breaks the HTML, so the onclick event never fires, or fails to call the javascript function. What does the button HTML look like when rendered as HTML? |
Christian J |
Feb 8 2017, 01:26 PM
Post
#12
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
The JS function call parameter (created by the PHP variable) needs to be quoted:
CODE myPopup('$uuid') --otherwise you should get a JS error. Personally I'd rewrite the PHP echo in order to output stricter HTML syntax, perhaps like this: CODE echo 'onClick="myPopup(\''.$uuid.'\');" value="$ctags[$z]">'; |
CharlesEF |
Feb 9 2017, 02:19 AM
Post
#13
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
The JS function call parameter (created by the PHP variable) needs to be quoted: CODE myPopup('$uuid') I don't think that is correct. Only text values need to be quoted. To be honest, I don't see how the button HTML that is echoed out works at all. This HTML is invalid: CODE echo "><td><input name='found_one' type=button "; Should be: echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . CODE echo "><td><input name='found_one' type='button' "; echo "onClick='myPopup($uuid);' value='$ctags[$z]'/>" . |
Christian J |
Feb 9 2017, 08:21 AM
Post
#14
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
I don't think that is correct. Only text values need to be quoted. If you don't quote it, the JS parser will think it's a JS variable. E.g., this PHP: CODE <?php $uuid='foo'; echo "<input type='button' onClick='myPopup($uuid);' value='123'>" ?> will be rendered as this HTML: HTML <input type='button' onClick='myPopup(foo);' value='123'> --but since no JS variable foo is previously defined (AFAIK), you get a JS error when you click. |
CharlesEF |
Feb 9 2017, 12:41 PM
Post
#15
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Yes, text values do need to be quoted but not numbers. From reading the threads I 'assumed' the $uuid value was numeric. Here is a test script to prove my point, numbers don't need to be quoted.
CODE <?php $uuid = 123454321.561879; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Title</title> <script type="text/javascript"> function myPopup(id) { window.alert(id); } </script> </head> <body> <?php echo(" <input name='found_one' type='button' "); echo("onClick='myPopup($uuid);' value='$ctags[$z]'>" . PHP_EOL); ?> </body> </html> |
Christian J |
Feb 9 2017, 04:25 PM
Post
#16
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
|
CharlesEF |
Feb 9 2017, 04:34 PM
Post
#17
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Also, because of this bad HTML button code:
CODE echo "><td><input name='found_one' type=button "; When the OP wraps the $uuid value in quotes then the button will not fire the function because the button HTML is invalid.echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" . EDIT: Funny thing, even with the bad HTML button code, if the $uuid value is numeric and NOT wrapped in quotes then the button will fire the function. The problem only shows up when the $uuid value is wrapped in quotes (single or double, it doesn't matter). This post has been edited by CharlesEF: Feb 9 2017, 04:42 PM |
Christian J |
Feb 9 2017, 05:10 PM
Post
#18
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
The problem only shows up when the $uuid value is wrapped in quotes (single or double, it doesn't matter). Or if the $uuid value is a string, and it's not quoted in the JS function call. This makes me believe it is a string, otherwise the JS should have worked one way or another. |
CharlesEF |
Feb 9 2017, 06:17 PM
Post
#19
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Or if the $uuid value is a string, and it's not quoted in the JS function call. This makes me believe it is a string, otherwise the JS should have worked one way or another. $uuid may be a string, as you suggest, but until the OP fixes the HTML button code, the function will never fire. This post has been edited by CharlesEF: Feb 9 2017, 06:18 PM |
Lo-Fi Version | Time is now: 26th April 2024 - 08:00 PM |