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

share
1  
does not try it anyone? :( – Nadishan Oct 21 '14 at 6:46
    
Add some debugging code to the PHP to show what it is actually receiving. If you're trying to filter something that isn't present, or is in the wrong format, then you're going to run into problems. Create some logging functions in PHP which you can use to log the result of var_export($_POST,true) then go from there – DaveyBoy Oct 21 '14 at 15:03
up vote 2 down vote accepted

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);
  }

?>
share
1  
This supposedly isn't as secure as filter_input. I really would like to know why the php team made the filter_input function ignore the data posted through ajax. This has been driving me nuts for 10 straight hours. And why isn't this bugging anyone? Does everyone just accept the ajax-posted data and builds them into their sql-queries? – doABarrelRoll721 Feb 20 '16 at 22:41

It seems like you are not outputting the data.

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

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

});
share
    
Nope, I've used firebug for see output.... Because this file is for testing – Nadishan Oct 21 '14 at 6:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.