How to get php page to receive ajax post from html page

后端 未结 1 473
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 05:53

I have a very simple form that has an input field for first name. I captured the form data and transmitted it via ajax to a PHP page using the standard jQuery posting method. Ho

相关标签:
1条回答
  • 2021-01-24 06:08

    In your Ajax function, you're passing the contents of formData to the server, though not as formData but as their original input name.

    In this case, you have:

    <input type="text" class="form-control" name="firstName" id="firstName" placeholder="First name">
    

    The input's name is firstName, so you need to call $_POST['firstName'] instead of $_POST['formData'].

    if (isset($_POST['firstName'])) {
        $ajaxData = $_POST['firstName'];
        echo $ajaxData;
    }
    

    The same applies for any other field you would have in your form, so for example, having another input with the name lastName means you'd have to call $_POST['lastName'] to access it.

    There were also some misplaced brackets and parentheses in the PHP code which I accommodated above.

    0 讨论(0)
提交回复
热议问题