AIGS
asked on
Creating strings using PHP including calls to javascript functions - having problems where javascript function does not accept the data passed.
Using PHP to access data from database (MYSQL) - this part is not a problem.
have PHP function that use database and create html code (as strings) that are passed to javascript, which include calls to alert().
Check the image supplied: When I click on CLICK the alert dialog displays - so far so good.
Unfortunately when I pass data as string to the alert() function is when I get problems - nothing happens.
Here is a sample of my code:
$wString = "<a href=\'javascript:aler t();\' >CLICK</a> ";
works fine, shows empty alert dialog (as per image attached) - as soon as I try and put data inside the alert function - problems.
I have tried all manner of combinations to encase the data inside the () e.g. \\\\\\', \\\', \\\', \', \", etc etc - driving me nuts. I am obviously missing something - any help appreciated.
Gary
error.png
have PHP function that use database and create html code (as strings) that are passed to javascript, which include calls to alert().
Check the image supplied: When I click on CLICK the alert dialog displays - so far so good.
Unfortunately when I pass data as string to the alert() function is when I get problems - nothing happens.
Here is a sample of my code:
$wString = "<a href=\'javascript:aler
works fine, shows empty alert dialog (as per image attached) - as soon as I try and put data inside the alert function - problems.
I have tried all manner of combinations to encase the data inside the () e.g. \\\\\\', \\\', \\\', \', \", etc etc - driving me nuts. I am obviously missing something - any help appreciated.
Gary
error.png
That should work. I checked this and it does work. If your text from your database has ' in it, you can't use '\' to escape it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP/JS Alert</title>
</head>
<body>
<h1>PHP/JS Alert</h1>
<?php
$wString = "<a href=\"javascript:alert('This is the way');\">CLICK</a>";
echo $wString;
?>
</body>
</html>
ASKER
I tried:
$wString = "<a href='javascript:alert(\"H ello\");'> CLICK</a>" ; - doesn't work
$wString = "<a href='javascript:alert(\"H
It works here.
<html>
<head></head>
<body>
<?php
$wString = "<a href='javascript:alert(\"Hello\");'>CLICK</a>";
echo $wString;
?>
</body>
</html>
ASKER
The string that I create which incorporates the alert() is passed back through two other functions as far as I can tell which make up the final line of code.
echo "insDoc(aux" . $auxCount . ", gLnk('S', 'Responses: " . $row["mf_threads"] . " - Last: " . writeDate($row["mf_lastthr ead"],"S") . "', 'javascript:loadMe("" . getDetails() . "")'));\r\n";
The line I am having trouble with, is created in the function getDetails() as per the code above.
I hope this helps to clarify my problem. The echo above is used to write the code to browser as javascript.
echo "insDoc(aux" . $auxCount . ", gLnk('S', 'Responses: " . $row["mf_threads"] . " - Last: " . writeDate($row["mf_lastthr
The line I am having trouble with, is created in the function getDetails() as per the code above.
I hope this helps to clarify my problem. The echo above is used to write the code to browser as javascript.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Statements like this leave professional programmers SMH:
echo "insDoc(aux" . $auxCount . ", gLnk('S', 'Responses: " . $row["mf_threads"] . " - Last: " . writeDate($row["mf_lastthr ead"],"S") . "', 'javascript:loadMe("" . getDetails() . "")'));\r\n";
This is PHP AntiPractice #9. Don't write code like that - you'll never be able to debug it.
It looks like you have many layers of overlapping output formatting and perhaps one or more of these introduce errors of some sort into the process. I would try to simplify the question to the point that you get the SSCCE. Once you see that, the issues will almost always become obvious and easy to fix.
You might also want to try using HEREDOC notation (heed, but do not be put off by the Warning). I find that it greatly simplifies the way PHP handles string variables. Quotes can be confusing and stacked quotes can be confusinger :-)
Please see a simple example of HEREDOC at http://iconoun.com/demo/temp_aigs.php
HTH, ~Ray
echo "insDoc(aux" . $auxCount . ", gLnk('S', 'Responses: " . $row["mf_threads"] . " - Last: " . writeDate($row["mf_lastthr
This is PHP AntiPractice #9. Don't write code like that - you'll never be able to debug it.
It looks like you have many layers of overlapping output formatting and perhaps one or more of these introduce errors of some sort into the process. I would try to simplify the question to the point that you get the SSCCE. Once you see that, the issues will almost always become obvious and easy to fix.
You might also want to try using HEREDOC notation (heed, but do not be put off by the Warning). I find that it greatly simplifies the way PHP handles string variables. Quotes can be confusing and stacked quotes can be confusinger :-)
Please see a simple example of HEREDOC at http://iconoun.com/demo/temp_aigs.php
<?php // demo/temp_aigs.php
error_reporting(E_ALL);
// A VARIABLE IN THE PHP SCRIPT
$d = date('r');
$m = 'When was this page created? ' . $d;
// A HEREDOC BLOCK OF HTML WITH JAVASCRIPT AND VARIABLE SUBSTUTION
$html = <<<ENDHTML
<script>
alert('$m');
</script>
ENDHTML;
echo $html;
The reason this works well is that quote marks have no meaning inside a HEREDOC block. Variable substitution takes place without regard to the quotes and apostrophes. So your only consideration of the meaning of the quotes is given to the meaning of the quotes in the resulting string, not the PHP variable substitution process. In this case alert('$m'); contains quotes that are meaningful in the resulting JavaScript command.HTH, ~Ray
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help. Breaking it up into smaller pieces instead of on one line allowed me to see it more clearly and rectify where needed - now working fine.
Again Thanks.
Again Thanks.
$wString = "<a href='javascript:alert(\"H