So in PHP I have this variable
$studID
which can have formatted int values as "00-00000".
I want to pass this through to a Javascript function so I did this:
<?php ...
onclick="MyFunction('.$studID.')";
... ?>
This is the Javascript file:
function MyFunction(id1){
alert("You have entered: " + id1);
}
My problem is that it returns an integer value of the first half minus the second half instead of the ID itself!
Example:
Passed through: 15-00788
Result from function: -773 (instead of 15-00788)
Any help would be appreciated!
Hard to tell you exactly without the full context of the code, but the problem is that you need to surround the data with quotes to pass it into the JavaScript function as a string. Something like:
<?php ...
onclick="MyFunction(\''.$studID.'\')";
... ?>
Should probably work. (Have to be sure to properly escape the quote characters)
Also, you should consider using double quotes in php, which supports inline variable expansion (instead of doing concatenation). See the docs
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments