简体   繁体   中英

HTML Special Characters and PHP

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?

$(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 is E2 96 B2 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM