Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript string passing to PHP via AJAX

    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:
    PHP Code:
            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:
    Code:
    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:
    PHP Code:
            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>'


  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    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

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    url=url+"&songspy_name="+encodeURIComponent(name);
    http://xkr.us/articles/javascript/encode-compare/
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •