javascript - HTML Special Characters and PHP

886

I have a span that contains and upper arrow:▲

Via an AJAX call, in PHP, I receive a POST parameter that contains this arrow.

$("#mySpan").click(function(){

      var arrow = $(this).html();
      alert(arrow); //displays the arrow the in an alert box
      $.post('Something.php',{ arrow: arrow },function(data){
            alert(data);
      });

});

In PHP, I am simply checking if the parameter is this arrow.

if(isset($_POST['arrow']){

      $arrow = $_POST['arrow'];
      if($arrow=='▲')
            echo 'Its an arrow';
      else
            echo 'Its not an arrow';
}

The JS returns an alert box 'Its not an arrow'. Am I comparing it in the right way?

491

Answer

Solution:

$(this).html(); returns the actual Unicode character instead of the character escape▲.

You can check the length to see this:

alert($(this).html().length); // => 1

Next, you send this character to the backend. And all AJAX data is encoded as UTF-8. UTF-8 representation of isE2 96 B2.

echo $_POST['arrow'] === "\xE2\x96\xB2"; // Its an arrow

People are also looking for solutions to the problem: php - Join 3 Tables with 3rd Table not associated to Table 2

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.