3.2.0

Php variable with javascript

Hello all,

Can anyone help?

I have a php variable with a value from a mySQl db. The value has to be passed to a submit button and also to a textfield. I have used javascript to do some of the work.

This creates the buttons using a loop with contents of db.

echo "<td ><input type='button' value='$servName' class='submit' onclick=fieldWrite('$servName')>";

On submit this JS bit fills a textfield with th users choice of button.

function fieldWrite(t)
{

with (document.Calc) {
textField.value = t;
}
}

However as some of the values have whitespace or apostrophes etc they are not working with js. Is there a way to use JS but reatain the whitespaces and punctuation marks?









Anthony T
February 27,
Try php's rawurlencode and rawurldecode:
http://us3.php.net/rawurlencode
http://us3.php.net/rawurldecode

In javascript try the unescape function (on the data your sent from php using rawurlencode) before loading the text box:
http://www.javascripter.net/faq/unescape.htm

ex.
function fieldWrite(t)
{

with (document.Calc) {
textField.value = unescape(t);
}
}
Rob Francis
February 27,
Thanks for replying Rob but it still wont work. I think the variable will have to be amended prior to being passed to the function.

If I assign the php variable with one word to test the function it works fine. It wont work if php variable has whitepace in between for two or more words or apostrophe's etc.

I need some method that will allow the JS script to accept the string retaining all whitespace and punctuation marks. Sorry if there's a simple answer buyt I am new to JS.
Anthony T
February 27,
Try this test php code:

<?php

$datafromdb='this is a test with single (\') and double (") quotes';
$datafromdb=rawurlencode($datafromdb);

$htmloutput='
<html>
<body>

Text Field (for display): <input id=textField type=text size="60" value=""><br>
Text Field (for submit): <input id=textField2 type=text size="60" value="">
';

$htmloutput.='<script>
var jsdata="'.$datafromdb.'";
textField.value = unescape(jsdata);
textField2.value = jsdata;
</script>

</body>
</html>
';

echo $htmloutput;
?>


JS also has an escape function to match the unescape
Rob Francis
February 27,
Cheers Rob,
You're a gent!
Anthony T
February 27,
how can i pass java script variables to php page
sucharitha
May 27,

This topic is archived.

See also:


Back to support forum