+ Reply to Thread
Results 1 to 4 of 4

Thread: Passing + in a php variable

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Passing + in a php variable

    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

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Passing + in a php variable

    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.
    Nothing is always absolutely so.

  3. #3
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: Passing + in a php variable

    Thanks the the help it was driving me crazy.

    +REP
    +Credits

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Passing + in a php variable

    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).
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. My 2cents on Ruby vs PHP.
    By jwillia in forum Programming Help
    Replies: 0
    Last Post: 03-15-2008, 11:18 PM
  4. PHP Easter Eggs
    By dragoneye_xp in forum Off Topic
    Replies: 3
    Last Post: 06-14-2006, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers