Skip to content Skip to sidebar Skip to footer

How To Get Php Page To Receive Ajax Post From Html Page

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. Howev

Solution 1:

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:

<inputtype="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.

Post a Comment for "How To Get Php Page To Receive Ajax Post From Html Page"