user469453
user469453

Reputation: 349

Get all Form values - working with jquery ajax in PHP

I'm using this jquery to serialize my form and pass 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 );
       }
    });

});

Normally, lets say I had a form with the fields Address F_name and S_name, I'd do something like this to get the values into PHP vars;

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

I'd then go onto persist these to the DB

However, this particular form is liable to change on a regular basis. Therefore, I'd like to be able to get at all of the data that was passed through the ajax request in the form of a string that I can explode or an array.

I can then loop through the items in the array and persist them to the DB one by one (i think!).

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

As always - all help is very much appreciated.

Upvotes: 4

Views: 11340

Answers (3)

tsadiq
tsadiq

Reputation: 402

foreach($_POST as $k => $v)

but this is pretty risky as malicious user could manually add post values to the request and do crappy stuff with your code. Make a list of allowed value maybe ;)

Upvotes: 0

cusimar9
cusimar9

Reputation: 5259

var $results = '';

foreach ($_POST as $key => $value) {
    echo $results .= "$key = $value;";
}

// $results holds the posted values

Upvotes: 1

Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

  foreach($_POST as $form_key => $form_val){ }

you will not want to query each time inside this loop, however. Instead append each time to a string that you will query with afterwards.

Upvotes: 2

Related Questions