Bk Razor
Bk Razor

Reputation: 1532

Trying to pass multiple parameters from php to javascript

Heres my current code in php with one parameter which works

 <?php
  $sid = "012";
  echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.')">';

?>

Now im trying to pass two parameters or more, but cant seem to get it to work, here is my attempt:

 <?php
  $name="abc
  $sid = "012";
  echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.','.$name.')">';

?>

Upvotes: 0

Views: 78

Answers (2)

Matthew Peltzer
Matthew Peltzer

Reputation: 138

If one of your JavaScript function parameters is a string, you'll have to correctly enclose it in quotes:

<?php
  $name="abc";
  $sid = "012";
  echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.',\''.$name.'\')">';
?>

You get away with $sid not enclosed in quotes because it looks like a number.

Upvotes: 2

DiddleDot
DiddleDot

Reputation: 744

You need quotes around the parameters or javascript treats them like variables instead of strings.

echo '<input type="button" value="Submit" onclick="changeConfirmed(\''.$sid.'\',\''.$name.'\')">';

Upvotes: 3

Related Questions