Search code examples
phpjqueryajaxjquery-ajaxq

Jquery ajax with PHP filter_input function


I tried to catch HTML form data with PHP, which are sent from jquery ajax. I'm use jquery serializeArray() and $.post method to send data. After that I tried to catch my data with php. But my php code is not get that data. Why is that? What are the errors? here my code

html file

<html>
    <head>
        <script src='http://code.jquery.com/jquery-1.11.1.min.js'></script>
        <script src="js.js" type="text/javascript"></script>
    </head>

    <body>
        <form>
            <input type='text' name='name' />
           <input type='button' id='btn' value='but'/>


        </form>

    </body>
</html>

js file

$("document").ready(function(){

$('#btn').click(function(){
    var a = $('form').serializeArray();
    $.post('catch.php',{a:a});
});

});

php file

<?php
$a = $_POST['a'];

echo filter_input(INPUT_POST, $a[0]['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);



?>

** I've used firebug for inspecting output


Solution

  • In this situation I think it could be simpler to use filter_var.

    <?php
      $a = $_POST['a'];
    
      foreach ($a as $key => $value){
        echo filter_var($a[$key]['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
      }
    
    ?>