...

View Full Version : Javascript string passing to PHP via AJAX



Demon
10-21-2008, 11:19 PM
I'm most certain that this has been mentioned before, however as it's most likely such a simple fix, I couldn't seem to find what I needed anywhere.

The problem I am experiencing is when I try to parse a value through javascript to PHP via AJAX. I have posted an example of what I mean below.

If in the name I type something like: A name with + and & and some other symbols, it does not get parsed through correctly, and the output from request.php is wrong. I hope somebody understands what I mean.

By the way, I have only posted parts of the script that I thought would be necessary to be seen in order to help.

Many thanks.

index.php:

echo '
<form name="makeRequestForm">
<table width="380" cellpadding="0" cellspacing="0" border="0" style="background-color: #cccccc;border: 1px #000 dotted; padding: 5px">
<tr>
<td>
<strong>Request Form</strong><br /><br />
What is your name?<br />
<input type="text" name="request_name" value="Test Value" /><br /><br /></td>
</tr>
<tr>
<td>
<input type="submit" value="Request" onclick="makeRequest(document.forms[\'makeRequestForm\'].request_name.value);return false;" />
<input type="submit" value="Cancel" onclick="requestBoxClose();return false;">
</td>
</tr>
</table>
</form>';

AJAX script:

function makeRequest(name) {

//Returns the correct value
alert('Sending with name: '+name);

ajaxHttp2=GetXmlHttpObject()

if (ajaxHttp2==null) {
alert ("Browser does not support HTTP Request")
return
}

var url="request.php"
url=url+"&songspy_name="+name
ajaxHttp2.onreadystatechange=completeRequest
ajaxHttp2.open("GET",url,true)
ajaxHttp2.send(null)
}

request.php:

echo '
<table width="380" cellpadding="0" cellspacing="0" border="0" style="background-color: #cccccc;border: 1px #000 dotted; padding: 5px">
<tr>
<td>
<strong>GWRadio Song Request</strong><br /><br />
Thank you, <strong>'.stripslashes($songspy_name).'</strong>.<br /><br /></td>
<tr>
</table>';

ohgod
10-22-2008, 01:34 PM
can you post what does make it to the request page? toss an echo\exit in there paste it here, or use firebug or something like that to watch the transaction.

my initial guess would be url encoding with the symbols. for examples see:

http://us.php.net/manual/en/function.rawurlencode.php
http://us.php.net/manual/en/function.rawurldecode.php

glenngv
10-28-2008, 08:50 AM
url=url+"&songspy_name="+encodeURIComponent(name);

http://xkr.us/articles/javascript/encode-compare/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum