简体   繁体   中英

Ajax submit doesn't work (can't read $_POST in php)

I am trying to submit my form using Ajax. When the button is clicked the hit() function gets called and passes the contents of the textbox back to test.php

$_POST seems to be empty, since I get the alert from ajax (form was submitted) but I don't get to see the echo ( echo $_POST['textbox'] )

test.php

<?php
    echo "test";
    if($_POST)
    {
        echo $_POST['textbox'];
    }
?>
<html>
    <body>
            <form action="index.php" method="post" align="center" id="form" name="form">

                <script type="text/javascript" src="test2.js"> </script>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>


                <input type="text" class="form-control" id="input" name="input" oninput="check();">
                <input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
            </form>
    </body>
    </div>
</html>

test2.js

function hit() {
        var inputText = $("#input").val();
        var inputTextString = "textbox=" + inputText;
        $.ajax({
            type: 'post',
            url: 'test.php',
            data: inputTextString,
            success: function () {
              alert('form was submitted');
            }
        });

}

you will get the desired results in the response.

function hit() {

        var inputText = $("#input").val();


        $.ajax({
            type: 'post',
            url: 'test.php',
            data: {textbox : inputText},
            success: function (res) {
              alert(res);
            }
        });

}

And you need to change in your test.php file.

<?php
    echo "test";
    if($_POST)
    {
        echo $_POST['textbox'];exit;
    }
?>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="test2.js"> </script>
</head>

    <body>
            <form action="index.php" method="post" align="center" id="form" name="form">
                <input type="text" class="form-control" id="input" name="input" oninput="check();">
                <input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
            </form>
    </body>
    </div>
</html>

There're more of problems here

In test.php

  • how can you include your js file before you include jquery .. first is first .. After Tested Yes you can use jquery $() inside a function without getting $ undefined error while you'll run the function after include jquery .. sorry my bad
  • Scripts should be on the <head></head> or before </body>
  • while you using just button click why you're using <form>

your code should be something like that

<?php
    echo "test";
    if($_POST)
    {
        echo $_POST['textbox'];
        return false;
    }
?>
<html>
    <head>
    </head>
    <body>
         <input type="text" class="form-control" id="input" name="input" oninput="check();">
          <input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">

      <script type="text/javascript" src="test2.js"> </script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
    </body>
    </div>
</html>

On test2.js

function hit() {
    var inputText = $("#input").val();
    var inputTextString = {textbox : inputText}; // use this
    $.ajax({
        type: 'post',
        url: 'test.php',
        data: inputTextString,
        success: function () {
           alert('form was submitted');
        }
    });
}

Note: for me I prefer to use separated php file to use it with ajax .. it'll make it easier for outputs

If you need to use a form .. you can use your form code including my notes above and make your submit button type="submit" and remove onclick="hit()" from it then on your js file you can use

$(document).ready(function(){
     $('form').on('submit',function(e){
        e.preventDefault(); // to prevent form reload
        var inputText = $("#input").val();
        var inputTextString = {textbox : inputText}; // use this
        $.ajax({
           type: 'post',
           url: 'test.php',
           data: inputTextString,
           success: function () {
              alert('form was submitted');
           }
        });
     });
});

you never test if you see the reponse from echo - hence you don't alert the response from php at all.

To see what your php script returns you have to alert (or log, or do something usefull with) the passed in parameter to the success callback:

....
success: function (response) { 
     alert(response);
     console.log(response); 
},
....

Anyway you should make sure to not send additional data (like unneeded html in your case) back to ajax, but only the value/json. So in your case an exit; after echo would help.

Also follow @Mohammed-Yousef's instructions for the other issues!!

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