Capture Array Values from Dynamic input Fields using PHP

Well in my previous post, you were able to add multiple dynamic fields using jQuery. In this tutorial, we will collect values from those dynamically generated input fields, which can be displayed on user browser or store in MySql database. Let's assume you have a HTML form with multiple input fields like example shown below. This fields are generated by jQuery code in my previous post.
HTML
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<form method="post" action="collect_vals.php"> <div class="input_fields_wrap"> <button class="add_field_button">Add More Fields</button> <div><input type="text" name="mytext[]"></div> <div><input type="text" name="mytext[]"></div> <div><input type="text" name="mytext[]"></div> <div><input type="text" name="mytext[]"></div> <div><input type="text" name="mytext[]"></div> </div> </form>
Notice the name attribute mytext[] in input fields? they all have same name. The brackets indicate that the value of such input field will be posted as an array, so another input field means just another value in existing array.

Capture Array Values using PHP

Collecting values from above input fields is pretty simple, take a look at example below. Once the value is captured from input fields, you can output it on the browser.We can directly access array values using index number, but this will cause error if indexed value doesn't exist in array.
PHP
  • 1
  • 2
echo $_POST["mytext"][0]; echo $_POST["mytext"][1];
Another way is using PHP implode(), it converts array into string, separated by commas or any character. Just make sure POST variable is not empty:
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
if(isset($_POST["mytext"]) && is_array($_POST["mytext"])){ $subject = implode(", ", $_POST["mytext"]); echo $text; } /* returns value1, value2, value3 */
You can also iterate POST variable using foreach like so, result is same as above.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
<?php if(isset($_POST["mytext"]) && is_array($_POST["mytext"])){ $capture_field_vals =""; foreach($_POST["mytext"] as $key => $text_field){ $capture_field_vals .= $text_field .", "; } echo $capture_field_vals; } /* returns value1, value2, value3 */ ?>
How about converting the values into JSON string, just use PHP json_encode(), you can easily decode it back to array using json_decode():
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
<?php if(isset($_POST["mytext"]) && is_array($_POST["mytext"])){ echo json_encode($_POST["mytext"]); } /* returns ["value1","value2","value3"] */ ?>

Save values into Database

Save strings on your database using MySqli.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
<?php //Open a new connection to the MySQL server $mysqli = new mysqli('host','username','password','database_name'); //Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); } $capture_field_vals =""; if(isset($_POST["mytext"]) && is_array($_POST["mytext"])){ $capture_field_vals = implode(",", $_POST["mytext"]); } //MySqli Insert Query $insert_row = $mysqli->query("INSERT INTO table ( captured_fields ) VALUES( $capture_field_vals )"); if($insert_row){ print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />'; } ?>
  • I have html table and in 1 td i have a name and other td is a button called update. now i want to update the name according to button.
  • /MySqli Update Query $update_row = $mysqli->query("update table set captured_field = $capture_field_vals where captured_field_id=$capture_field_id_vals");if($update_row){ print 'Success! ID of last updated record is : ' .$mysqli->updated_id .''; }What is capture_field_id_vals ???