简体   繁体   中英

post data using ajax and js

Every time i try to use my classes below to post the array i made (also below) the ajax request doesn't pass the input as $_POST values but as $_REQUEST values seen in the web address bar at the top of the screen. I'm very new to Ajax and javascript (only been working with it about a month) so any tips and trick to fix anything below is greatly appreciated.

var formErrors=["Passage","FirstName","Zip"];
var formInput=["EventID","Passage","FirstName","LastName","Email","Organization","Location","Zip"];

Head of HTML

    $(function () {
        $("#signUp").click(function() {
            if(formArrayValidation(formErrors) != false) {      
                formPostAjax(formInput, 'join-event.php');
            }
            return false;                   
        });          
     });

Basics.js

formArrayValidation = function(errorArray) {
for(var i = 0; i < errorArray.length; i++) {
    $('.error').hide();  
    var name = $("input#" + errorArray[i]).val();  
    if (name == "") {  
        $("label#" + errorArray[i] + "_error").show();  
        $("input#" + errorArray[i]).focus();  
        return false;
    }
}
}
formPostAjax = function(inputArray, form) {
var dataString = "";
for(var i = 0; i < inputArray.length; i++)
{
    var data = inputArray[i];
    var dataInput = $("input#" + data).val();
    if(i = 0) {
        dataString = dataString + data + '=' + dataInput;
    }
    else {
        dataString = dataString + '&' + data + '=' + dataInput;
    }
}
$.ajax ({  
    type: "POST",  
    url: form,  
    data: dataString,  
    success: function() { 
    } 
}); 
}

Your event listener should be on the form and it should be:

$('#form_identifier').submit(...);

Additionally, jQuery provides a nice shortcut method for serializing form data:

$('#form_identifier').submit(function(){
   var post_data = $(this).serialize()
   // ....
   return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM