Page 1 of 1

[SOLVED] Escaping Quotes for Javascript

PostPosted: 02. July 2009 21:23
by Gadrin
Hello, I'm trying to ouput an INPUT with some built in Javascript. Unfortunately PHP requires either a
double-quote or single-quote around the string to delimit it.

However I can't get PHP to build the same string and output it using:

Code: Select all
<?php

$exp = "document.getElementsByName(\"Route\")[0].textContent=\"+\";";
echo $exp;

echo "<form name='LoopForm' onsubmit='' action='Bump-Up.php' method='post'>
      Level Up/Down Test <br><br>
      <input type='text' name='Level' value='1' /> <br><br>"
echo "<input type='submit' name='PlusButton' value= ' + '  onclick='$exp' />
      <input type='submit' name='MinusButton' value= ' - ' onclick='SetMinus();' />
      <span name='Route'></span></form>
   "
?>


Anyone have a suggestion on how to get it to work ?

Final Output I want:

Code: Select all
<input type='submit' name='PlusButton' value= ' + '  onclick='document.getElementsByName("Route")[0].textContent="+";' />


Which works fine if just in HTML.



.

Re: Escaping Quotes for Javascript

PostPosted: 03. July 2009 14:06
by Gadrin
Well, this works. Turns out the ECHO statement wasn't constructed properly.
Also needed a TEXTAREA rather than a SPAN to hold the value of ROUTE for
the php action page.

Code: Select all
<style>
   textarea {display:none;}
</style>


<?php

$plus  = "document.getElementsByName(\"Route\")[0].textContent=\"+\";document.forms[\"LoopForm\"].submit();";
$minus = "document.getElementsByName(\"Route\")[0].textContent=\"-\";";

echo $plus;

echo "<form name='LoopForm' onsubmit='' action='Bump-Up.php' method='post'>
      Level Up/Down Test <br><br>
      <input type='text' name='Level' value='1' /> <br><br>";
echo    "<input type='submit' name='PlusButton' value= ' + '  onclick='$plus' />
      <input type='submit' name='MinusButton' value= ' - '  onclick='$minus' /><textarea name='Route'></textarea></form>";
?>


>