0

I am having a issue while passing php string to JavaScript function. When i pass the numeric value it works fine. But when i pass the string it doesn't work

Here is my JavaScript code:

function updateButton(custID, custEmail)
{
alert (custID);
alert (custEmail);
}

And this is my php code:

echo '<td><input id="btnUpdate" type="submit" name="btnUpdate" value = "Update" onclick="updateButton(' . $row['CustomerID']  . ',' .  $row['custEmail'] . ')" /> </td>';

In the above php code $row['CustomerID'] has the numeric value and $row['custEmail'] has the string value.

2
  • 1
    You seem to have forgotten to include a question :) Commented Jan 29, 2015 at 22:21
  • 2
    You have to be aware of the context your output will be used in. You're dumping text from PHP into a javascript-inside-html context. Your text is NAKED - no quotes around, so it'll be treated by JS as variable names or whatever. Commented Jan 29, 2015 at 22:25

2 Answers 2

3

Try this:

echo '<td><input id="btnUpdate" type="submit" name="btnUpdate" value = "Update" onclick="updateButton("' . $row['CustomerID']  . '","' .  $row['custEmail'] . '")" /> </td>';
Sign up to request clarification or add additional context in comments.

Comments

1

You need quotes around the string

echo '<td><input id="btnUpdate" type="submit" name="btnUpdate" value = "Update" onclick="updateButton(' . $row['CustomerID']  . ',\'' .  $row['custEmail'] . '\')" /> </td>';

1 Comment

If so, then accept his answer, that is the spirit of this website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.