Let’s say that I send a JS array to a php page using an http post:
var tags = ['foo', 'bar'];
My goal is to do a new INSERT in my db table for each tag (because I suppose I can’t insert multiple rows with one query)? How do I loop through the array and insert the values in to the table using mysql_query?
Send the post parameters in form
tags[]=foo&tags[]=bar. PHP should receive them as a$_REQUEST['tags']array. Then iterate as normal in PHP.Alternately, use
JSON.stringify(thing)on clientside to get a string representation you can send as a single parameter, then restore it withjson_decode(param)on PHP side – and again you get a PHP array.You can iterate a PHP array using the
foreach ($array as $element) { ... }construct.Also, you can insert multiple rows with one INSERT: