-2

I am trying to bind a uniqid() to an onclick event in a string:

$UniquepreID = uniqid();
$selectbutton = '<input type="button" value="testabc" 
       onclick="selectElementContents( document.getElementById("$UniquepreID") );" />';

The $UniquepreID is parsed literally. How can I make this work correctly?

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
nuet
  • 219
  • 4
  • 12

2 Answers2

1

Your $UniquepreID is within single quotes so will not be expanded. See here

Try this:

$selectbutton = '<input type="button" value="testabc" onclick="selectElementContents( document.getElementById("' . $UniquepreID . '") );" />';
danjam
  • 1,064
  • 9
  • 13
1

Your are using single quotes to define your string variable $selectbutton. You have either to use double quotes or conat your string like this:

$selectbutton = 'some string with "quotes"' . $uniquepreID . 'more "quotes" here';
Marc Anton Dahmen
  • 1,071
  • 6
  • 6