Submitting from jQuery to PHP

I sent the data to my jquery file and I checked the data string and everything will be saved where it should be, but when I send it to my php file, all the variables are zero. Can anybody help me?

$(document).ready(function() { $(".button").click(function() { //$('.error').hide(); var firstname = $("input#First_Name").val(); var lastname = $("input#Last").val(); var areacode = $("input#area_code").val(); var phonenumber = $("input#Phone_Number").val(); var emailaddress = $("input#emailaddress").val(); var confirmemail = $("input#confirm_email").val(); var password = $("input#Password_Input").val(); var confirmpassword = $("input#ConfirmPassword").val(); var streetaddress = $("input#StreetAddress_input").val(); var streetaddress2 = $("input#StreetAddress2_input").val(); var city = $("input#City_Input").val(); var state = $("input#StateInput").val(); var zipcode = $("input#ZipCode").val(); var month = $("input#month_input").val(); var day = $("input#day_input").val(); var year = $("input#year_input").val(); var services = $("input#services_input").val(); var agreement = $("input#agreement").val(); var dataString = 'firstname=' + firstname + '&lastname=' + lastname + '&areacode=' + areacode + '&phonenumber=' + phonenumber + '&emailaddress=' + emailaddress + '&confirmemail=' + confirmemail + '&password=' + password + '&streetaddress=' + streetaddress + '&streetaddress2=' + streetaddress2 + '&city=' + city + '&state=' + state + '&zipcode=' + zipcode + '&month=' + month + '&day=' + day + '&year=' + year + '&services=' + services + '&agreement=' + agreement; alert(dataString); $.ajax({ type: "POST", url: "http://www.vectorcreditsolution.com/js/process.php", data: dataString, success: function() { alert("Yay it was sent"); } }); return false; }); 

and then my php file

 <?php $FirstName = $_POST["firstname"]; $LastName = $_POST['lastname']; $AreaCode = $_POST['areacode']; $PhoneNumber = $_POST['phonenumber']; $EmailAddress = $_POST['emailaddress']; $Password = $_POST['password']; $StreetAddress = $_POST['streetaddress']; $StreetAddress2 = $_POST['streetaddress2']; $City= $_POST['city']; $State = $_POST['state']; $ZipCode = $_POST['zipcode']; $Month = $_POST['month']; $Day = $_POST['day']; $Year= $_POST['year']; $Service=$_POST['services']; var_dump($_POST["firstname"]); var_dump($_POST['firstname']); var_dump($_POST[firstname]); 
+6
source share
3 answers

You do not output the returned data from AJAX, and you do not use the tool to view it. Perhaps you expect var dumps to appear on the page. They will not, they will be returned to the function of success, where you can use the data as you see fit.

jQuery The AJAX method will pass the output from your PHP file to the 'ret' variable inside the success function. For example, if you want to return an HTML success message, you can add this HTML to the page without refreshing it. Or any of a million things.

You must read AJAX.

Change this:

 $.ajax({ type: "POST", url: "http://www.vectorcreditsolution.com/js/process.php", data: dataString, success: function() { alert("Yay it was sent"); } }); 

To that:

 $.ajax({ type: "POST", url: "http://www.vectorcreditsolution.com/js/process.php", data: dataString, success: function(ret) { alert(ret); } }); 

And then click the button on the page using jQuery.

Do not combine this with the other answers on this page. One of them is going to change your array keys to you (serialize).

+4
source

Try publishing the data as a Javascript dictionary, not as a large string:

 $.ajax({ // ... data: { firstname: firstname, lastname: lastname, // etc. } }); 
+5
source

I would use the jquery serialize () function. Instead of looking for the value of each field, serialize the entire form

 $("#form-id").serialize(); $.ajax({ type: "POST", url: "http://www.vectorcreditsolution.com/js/process.php", data: $("#form-id").serialize(), success: function() { alert("Yay it was sent"); } }); return false; }); 
0
source

Source: https://habr.com/ru/post/906213/


All Articles