AJAX not sending form data to PHP


Jerry

I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL.

$(document).ready(function(){   
    $('#my_form').submit(function(){    
        event.preventDefault();                 
        var form = $('form')[0];
        var formData = new FormData(form);                    
        $.ajax({            
            url: "signup.php",
            enctype: "multipart/form-data",
            cache: false,
            type: "post",
            dataType: "html",
            data: formData,
            processData: false,
            success: function(response){ 
                //do something
            },
            error: function (xhr, ajaxOptions, thrownError) {
               // some alert
            }
        });
    });
});

PHP

<?php
    //.......
    $fname=$_POST['fname']; 
    $lname=$_POST['lname']; 
    //.......
?>

HTML

<form id="my_form" method="post">
    <input type="text" id="fname" name="fname" class="inputname" placeholder="First Name" required/>
    <input type="text" id="lname" name="lname" class="inputname" placeholder="Last Name" required/>
    <input type="submit" value="BUTTON" class="submitbtn">
</form>

I get some undefined indexes: fname warning

Vijay P

You can use serialize() function in jquery .

$(document).ready(function(){   
    $('#my_form').submit(function(){    
        event.preventDefault();   
        var data = $(this).serialize();
        $.ajax({            
            url: "signup.php",            
            cache: false,
            type: "post",            
            data: data,            
            success: function(response){ 
                //do something
            },
            error: function (xhr, ajaxOptions, thrownError) {
               // some alert
            }
        });
    });
});

<?php   
    $fname=$_POST['fname']; 
    $lname=$_POST['lname']; 
    echo $fname;
    echo $lname;
?>

Related


AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

PHP Ajax form sending

midda25 I am using the jQuery serizalize function to post form data to a PHP script. My script is running fine and PHP is executing, although I am not receiving the posted value from the form in PHP. this is my code HTML: <form id="contact_form" action="proces

PHP Ajax form sending

midda25 I am using the jQuery serizalize function to post form data to a PHP script. My script is running fine and PHP is executing, although I am not receiving the posted value from the form in PHP. this is my code HTML: <form id="contact_form" action="proces

PHP Ajax form sending

midda25 I am using the jQuery serizalize function to post form data to a PHP script. My script is running fine and PHP is executing, although I am not receiving the posted value from the form in PHP. this is my code HTML: <form id="contact_form" action="proces

jQuery Ajax not sending data to PHP

user 882670 The following myscript.js output: [{"orcamento":"10","atual":"20","desvio":"","data":"2015-01-01","nome_conta":"BBB","nome_categoria":"abc","nome_entidade":"def"}] myscript.js: if (addList.length) { $.ajax($.extend({}, ajaxObj, {

jQuery Ajax not sending data to PHP

user 882670 The following myscript.js output: [{"orcamento":"10","atual":"20","desvio":"","data":"2015-01-01","nome_conta":"BBB","nome_categoria":"abc","nome_entidade":"def"}] myscript.js: if (addList.length) { $.ajax($.extend({}, ajaxObj, {

Ajax is sending data but Php file is not receiving data

awtthem I've spent the past 10 days or so trying to figure this out and I'm still stuck. I'm making an online budgeting system that sends Javascript values via Ajax to PHP which then sends them to a database. this is my javascript code $.ajax({ url: 'yup.ph

HTML form not sending data to MySQL using PHP

Tom G103 I have the form in HTML, but I guess it's not set up like normal. I will post the code as HTML(PHP) file and php to send it to db. <form action="upload.php" method="POST"> <!-- Name input--> <div class="form-group"> <label class="control

Error sending form data to mail using php

Timothy Maher I'm trying to send or write code for sending an email from a form, but everytime I submit, I get an "Error occurred" statement that I specified in the if else statement, so I can't send mail. What could be the problem? Here is my code. <!DOCTYPE

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

HTML form not sending data to MySQL using PHP

Tom G103 I have the form in HTML, but I guess it's not set up like normal. I will post the code as HTML(PHP) file and php to send it to db. <form action="upload.php" method="POST"> <!-- Name input--> <div class="form-group"> <label class="control

HTML form not sending data to MySQL using PHP

Tom G103 I have the form in HTML, but I guess it's not set up like normal. I will post the code as HTML(PHP) file and php to send it to db. <form action="upload.php" method="POST"> <!-- Name input--> <div class="form-group"> <label class="control

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

HTML form not sending data to MySQL using PHP

Tom G103 I have the form in HTML, but I guess it's not set up like normal. I will post the code as HTML(PHP) file and php to send it to db. <form action="upload.php" method="POST"> <!-- Name input--> <div class="form-group"> <label class="control

HTML form not sending data to MySQL using PHP

Tom G103 I have the form in HTML, but I guess it's not set up like normal. I will post the code as HTML(PHP) file and php to send it to db. <form action="upload.php" method="POST"> <!-- Name input--> <div class="form-group"> <label class="control

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

Dropdown box not sending form data via php

Douglas Littlefield It might be something simple I'm missing. I have an html page with a form: <form action="http://URL/mailer.php" id="contact_form" method="post"> Inside that table are your basic textboxes, except for the 1 dropdown. <select id="boardtype">

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Error sending data to php using Ajax in Wordpress

Bruno I have this form: <select id="mudar_produto"> <option></option> <option value="#produto_1">Novo Produto Higiene</option> </select> <section class="hide-section" id="produto_1"> <form class="form-validate" id="feedback_form"> <div class="ca

Error sending JSOn data to PHP via Ajax

User 123 I am able to send the JSON data to the server via Ajax as I can see it in the parameters of My Browser Developer Tool => Network but I get no response and even trying to do this Print $_REQUESTI $_POSTjust get the Cookie value instead of me data sent

PHP returns no data when sending ajax request

beans, lentils When I send an ajax post request to my getMessages.php file it returns nothing. I've tried manually setting the array values and printing them in the console and that seems to work. get message.php <?php require_once "mysqli.php"; $data = arra

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da