Concatening Php Variable And Strings Inside Double Quotes For Onclick Event
The button when pressed call a function that needs the sn value, but the code below fails with the Chrome debug message: Uncaught ReferenceError: Telephony is not defined Telepho
Solution 1:
- Assume your variable $sn is "Stack". Hence $sn is a string you need to pass this variable as a string.
- So rewrite myF($sn) as myF("'".$sn."'").
- When yu inspect the button you can see myF('Stack').
- then in javascript you can avoid the error.
Solution 2:
Why do you need double quotes?
echo"<button onclick=\"myF($sn)\">Vote</button>";
Do this instead:
echo'<button onclick="myF(\''. $sn . '\')">Vote</button>';
Or perhaps do it with sprintf
like this:
echo sprintf("<button onclick=\"myF('%s')\">Vote</button>", $sn);
Solution 3:
Does this work?
echo"<button onclick=\"myF('$sn')\">Vote</button>";
I tried out a simple test case, without any MySQL. You can find the code I used below:
<?php$sn='noname';
$registers = array(array('sname' => 'apple'), array('sname' => 'banana'), array('sname' => 'woodapple')); // Dummy "MySQL" Fetch Array (this might differ from what your 'while' loop receives)foreach ($registersas$reg){ // In your case, a 'while' loop works.echo"Service Name: ".$reg['sname']."<br>";
$sn = $reg['sname'];
echo"<button onclick=\"myF('$sn')\">Vote</button>"; // Added Quotes.echo"<hr>";
}
?>
HINT: Please refrain from using mysql_* functions! They are deprecated!
Solution 4:
Your concatenation need to change like this,
echo'<button onclick="myF("'.$sn.'")">Vote</button>';
Solution 5:
I think that when you are passing the PHP variable as an argument in to the onclick
event of button for calling javascript function
, you string breaks because it is not quoted. So it needs to be quoted. I have edited the code line where you need to edit you code. So try like:
...
......
........
........
echo"<button onclick=\"myF("$sn")\">Vote</button>";
.......
......
....
Post a Comment for "Concatening Php Variable And Strings Inside Double Quotes For Onclick Event"