Passing + in a php variable

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I have a textarea full of code that is submitted to a session variable through ajax and later echoed into a page to display the code. For some reason it is not echoing a + sign in the code and thus breaking my javascript. I have been pulling my hair out trying to figure this problem out im going to show my code and see if anyone here can see my error or what i need to do to fix it.


Txtarea contains the following code

Code:
function reloadImage(){
   img = document.getElementById("spam_image");
   img.src = "form_actions/image_creation/image.php?" + Math.random();
}

when i view the code on the display page it comes up as

Code:
function reloadImage(){
   img = document.getElementById("spam_image");
   img.src = "form_actions/image_creation/image.php?"    Math.random();
}

Ajax function
Code:
 function ajax(){
    var txtarea=document.code_form.txtarea.value;
    var passData = 'txtarea='+ txtarea;
    var AJAX = null;
    if (window.XMLHttpRequest) {
       AJAX=new XMLHttpRequest();
    } else {
       AJAX=new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (AJAX==null) {
       alert('Your browser doesn\'t support AJAX.');                                         
       return false
    } else {
       AJAX.open('POST', 'post.php', true);
       AJAX.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
       AJAX.onreadystatechange = function() {                      
          if (AJAX.readyState==4 || AJAX.readyState=='complete') { 
             callback(AJAX.responseText, AJAX.status); 
          }                               
       }                                  
       AJAX.send(passData);
    }
}";


part of post.php from ajax function
Code:
if (isset($_POST['txtarea'])){$_SESSION['txtarea'] = $_POST['txtarea'];}



page where it shows the code
Code:
 $test = $_SESSION['txtarea'];
  echo $test;

If anyone has any ideas on how i can get this to work it would be a big help.

Thanks
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Try adding the second line....

Code:
 function ajax(){
    var txtarea=document.code_form.txtarea.value;
    txtarea = encodeURIComponent(txtarea) ;  // ADD THIS LINE

Seems the + is being treated as a space.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
There are two relevant docs describing the behavior: the CGI primer (in the QUERY_STRING section) and URI Recommendations section of the Universal Resource Identifiers memo (specifically, the "Query strings" section).
 
Top