MEE
MEE

Reputation: 85

Can't see "+" sign, passing data from AJAX to PHP

I'm not sure how to explain this, so I'll do my best. I have a function on a javascript page that looks like this:

function updateDiceTracker(){
  //  var htmldata = $results.html();
  var htmldata = "2" + "\+" + "3";
    $.ajax({ url: 'actions.php',
            data: '&task=track.dice.roll&htmldata='+htmldata,
            type: 'post',
            success: function(output) {
              alert(output);
            },
            error: function(xhr, e){ message(e, "e"); }
        });
  }

As you can see, it's pretty standard. There is nothing wrong with it. The purpose of this function is to grab the .html() stored in $results (Which is a div where ID=results).

Notice the commented out portion. When I pass this data to actions.php, I get all of the HTML from that div, EXCEPT for the "+" signs. Visibly, on this page, you can see the "+" signs. I can send this data to a textarea and see the "+" signs. I can send it to another div and see the "+" signs - but when I send it to actions.php... there are no "+" signs.

Notice the line right below it. I've tried manually changing variable htmldata to different variations of 2+3 (for simplicity sake). Such as "2" + "+" + "3" and "2+3" and "2+3", etc -

Every single time, in the success function, the output is always: "2 3" (no plus sign)

I can see "2-3", "2*3", etc, but not the plus sign. I'm a little stumped because I feel like it's either something so simple I'm overlooking it - or I'm going crazy

The real version of the html from the results div would look something like (also includes formatting - but as you can see, I need those plus signs). This is what appears to the user BEFORE calling updateDiceTracker()

"Pixii Stix attempts to attack the enemy with Dagger: (1d20+5)
Dice Roll: [Hit]: 13 (+5) = 18"

Also, for simplicity sake, I've taken out ALL of the code on the actions.php page. Literally, this is what it looks like as of right now:

<?php
  $task = isset($_POST['task']) ? $_POST['task'] : "";

  if ($task){
    switch($task){
      case "track.dice.roll":
        $htmldata = isset($_POST['htmldata']) ? $_POST['htmldata'] : "";
        echo $htmldata;
        break;
    }
  }

?>

Upvotes: 1

Views: 54

Answers (2)

David
David

Reputation: 219037

Always URL-encode values being added to the query string, especially if they might contain various "special characters":

data: '&task=track.dice.roll&htmldata=' + encodeURIComponent(htmldata)

This will replace special characters which may otherwise have meanings in a URL so that they would be considered part of the string value.

If necessary, you can also URL-decode in PHP (if it doesn't do it for you automatically).

Upvotes: 1

Avinash Kumar
Avinash Kumar

Reputation: 93

Actually url might being parsed and you are not geting the result. Give this a try.

Replace + with %2B

Look this page for your other symbols.

https://www.w3schools.com/tags/ref_urlencode.asp

Upvotes: 0

Related Questions