0

I checked out this post but no luck : PHP - concatenate or directly insert variables in string

The suspect in question: showUser(3, change, " . $name . ");'> $name breaks the code; the second $name does work. The variable does not get passed to the function. I have been testing for hours to get this to work! It seems like it should work. I tried checking other places in the application to see what the problem could be and this seems to be the culprit.

<?php 
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
    if (stristr($q, substr($name, 0, $len))) {
        if ($hint === "") {

            $hint = "<p onClick='DoIt(); showUser(3, change, " . $name . ");'>" . $name . "</p>";
        } else {
            $hint .= "\n" . "$name";
        }
    }
} 
?>
Dipz
  • 15
  • 4

2 Answers2

0

JavaScript probably is requiring $name to be quoted.

$hint = "<p onClick='DoIt(); showUser(3, change, \"" . $name . "\");'>" . $name . "</p>";

Escaping the quotes like this will cause them to appear in the JS function call.

Quotes are often a thorny problem when mixing PHP, JavaScript and HTML on one line.

Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
0

You can quote your variable like this

if ($q !== "") {
  $q = strtolower($q);
  $len = strlen($q);
  foreach($a as $name) {
    if (stristr($q, substr($name, 0, $len))) {
      if ($hint === "") {
       $name = "'" . $name . "'";
       $hint = '<p onClick="DoIt(); showUser(3, change, ' . $name . ');">' . $name . '</p>';
      } else {
       $hint .= "\n" . "$name";
      }
    }
  } 
}
Muhammad Usman
  • 1,403
  • 13
  • 24