Get all form values ​​- working with jquery ajax in PHP

I use this jquery to serialize my form and submit it to a PHP script called write.php;

$("form").submit(function(){ var_form_data = $(this).serialize(); $.ajax({ type: "POST", url: "write.php", data: var_form_data, success: function(msg){ alert( "Data Saved: " + msg ); } }); }); 

Usually, let's say I had a form with the Address F_name and S_name fields, I would do something like this to get the values ​​in PHP vars;

 $Address = $_POST["Address"]; $F_name = $_POST["F_name"]; $S_name = $_POST["S_name"]; 

I would continue this work with DB

However, this particular form may change regularly. Therefore, I would like to get all the data that was transmitted through the ajax request as a string that I can explode or an array.

Then I can iterate over the elements in the array and store them in the database one by one (I think!).

I hope this makes sense, if I missed something or you would like me to explain further, please let me know.

As always - all help is greatly appreciated.

+4
source share
3 answers
 var $results = ''; foreach ($_POST as $key => $value) { echo $results .= "$key = $value;"; } // $results holds the posted values 
+1
source
  foreach($_POST as $form_key => $form_val){ } 

You will not want to query every time in this loop. Instead, add the line you request later each time.

+2
source
 foreach($_POST as $k => $v) 

but this is quite risky, as a malicious user can manually add message values ​​to the request and do crappy things with his code. It is possible to make a list of valid values;)

0
source

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


All Articles