Dimension variable transfer

I have a form with a variable size (length) that is populated from MySQL db. There are 4 fields that make up the information used to create the button (id, button #, name and price). When the form is submitted, I want to save all the values ​​in the MySQl database and update the div at the bottom of the page with a success message. For all my other pages, I used something like ...

xmlhttp.open("GET","myfile.php?a="+val1+"&b="+val2+"&c="+val3+"&d="+val4,true); xmlhttp.send(); 

PHP files save data and generate a message for the div. and write in a div ...

 document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 

This works well for all my other pages, but since I don’t know how many fields there will be, I cannot hardcode the xmlhttp.open instruction.

I am new to ajax and jQuery and I know that there must be a way to make this easy, but I have not been able to do anything. I was told that I can use this

 $.each($('#yourform').serializeArray(), function() { console.log(" <" +this.name+ '>' + this.value + "</" + this.name + "> " ); }); 

and it prints out every element of the form, but is not sure how to get this information in my PHP file and how to create a return message for the div. I'm new to ajax and jquery again, so if I could get some explanation, I'm sure this will help me figure this out.

+4
source share
3 answers

Hope this helps:

 $('form').submit(function() { $.post("myfile.php", $(this).serialize(), function(response) { console.log(response); }); }); 
+1
source

How about something like that? http://jsfiddle.net/r0k3t/e6W3Z/ , as you can see, you can add any number of fields, and all of them will be reset to qString.

 $('#yourform').submit(function() { var qString = ""; $('#yourform input[type=text]').each(function() { qString += $(this).attr("id") + "=" + $(this).val() + "?"; }); console.log(qString); return false; }); 

The question arises from you that you can capture the values ​​of a button because you are doing it elsewhere. When you are happy with the query string, you can use .post as suggested by Joe Brown.

+1
source

You can assign variable names, for example, from a00 to a99, b00 to b99, etc., which are sent to the server in a POST request. Then, just on the server side, check what values ​​have been set, and then process accordingly.

This is a little rude, but it should work.

0
source

All Articles