The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Passing PHP Variable to Javascript Function, Can't Open a Window in a Javascript function with a Parameter Pass
Jonah
post 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!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 7 2017, 11:05 AM
Post #2


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
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]'/>" .
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 7 2017, 11:10 AM
Post #3


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(Jonah @ Feb 7 2017, 04:51 PM) *

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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Jonah
post 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


QUOTE(Christian J @ Feb 7 2017, 11:10 AM) *

QUOTE(Jonah @ Feb 7 2017, 04:51 PM) *

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?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Jonah
post 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.

QUOTE(pandy @ Feb 7 2017, 11:05 AM) *

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]'/>" .


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 7 2017, 11:37 AM
Post #6


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(Jonah @ Feb 7 2017, 05:26 PM) *

Echoing the $uuid variable in a cell will show the value.

What do you get in the actual popup page?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 7 2017, 11:41 AM
Post #7


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(Jonah @ Feb 7 2017, 05:29 PM) *

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.

QUOTE(pandy @ Feb 7 2017, 11:05 AM) *

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. blush.gif

@pandy: PHP variables echoed inside single quotes are indeed not expanded, but in this case the actual echo uses double quotes.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Jonah
post 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.



QUOTE(Christian J @ Feb 7 2017, 11:37 AM) *

QUOTE(Jonah @ Feb 7 2017, 05:26 PM) *

Echoing the $uuid variable in a cell will show the value.

What do you get in the actual popup page?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 7 2017, 03:17 PM
Post #9


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(Jonah @ Feb 7 2017, 05:53 PM) *

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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Jonah
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 7 2017, 04:15 PM
Post #11


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(Jonah @ Feb 7 2017, 09:49 PM) *

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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 8 2017, 01:26 PM
Post #12


.
********

Group: WDG Moderators
Posts: 9,628
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]">';


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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



QUOTE(Christian J @ Feb 8 2017, 12:26 PM) *

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 ";
echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" .
Should be:
CODE
echo "><td><input name='found_one' type='button' ";
echo "onClick='myPopup($uuid);' value='$ctags[$z]'/>" .
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 9 2017, 08:21 AM
Post #14


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(CharlesEF @ Feb 9 2017, 08:19 AM) *

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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 9 2017, 04:25 PM
Post #16


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(CharlesEF @ Feb 9 2017, 06:41 PM) *

Yes, text values do need to be quoted but not numbers. From reading the threads I 'assumed' the $uuid value was numeric.

Good point. I assumed it was a string, but I guess only the OP knows for sure...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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 ";
echo "onClick=myPopup($uuid); value='$ctags[$z]'/>" .
When the OP wraps the $uuid value in quotes then the button will not fire the function because the button HTML is invalid.

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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 9 2017, 05:10 PM
Post #18


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(CharlesEF @ Feb 9 2017, 10:34 PM) *

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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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



QUOTE(Christian J @ Feb 9 2017, 04:10 PM) *

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.
Yes, that goes without saying.

$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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th March 2024 - 05:30 AM