2

I' sending some form data to PHP via AJAX. The value of the input field is being displayed before the data is sent using AJAX, 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 index:fname warning

4
  • Show complete code. Your form mainly. Commented Oct 24, 2015 at 11:25
  • 1
    why you dont use serialize() ?? Commented Oct 24, 2015 at 11:39
  • What are you returning your data as? JSON? What is the value of response in success? Commented Oct 24, 2015 at 11:41
  • I just echo values such as 0 or 1 Commented Oct 24, 2015 at 11:43

3 Answers 3

2

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; ?> 
Sign up to request clarification or add additional context in comments.

Comments

1

Change enctype to x-www-form-urlencoded. This is the default for forms that don't specify an encoding type, like yours.

1 Comment

I did ... but still data is not passed to PHP.
0

Ok you can try

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

in php

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

2 Comments

I get "Undefined index:fname Undefined index:lname" in my php
Serializing the data helped. At first i didn't serialize the data cuz i had sign_in form and it works fine without serialization....so i wanted to know what i was doig wrong with this sign_up form.....any way thanks for the comment above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.