How To Capture Array Values From Dynamic Input Fields Using Php?
In my previous post, y'all were able to add together multiple dynamic fields using jQuery. In this tutorial, nosotros volition collect values from those dynamically generated input fields, which tin give notice live on displayed on user browser or shop inwards MySql database.
Let's assume y'all induce got a HTML shape amongst multiple input fields similar event shown below. This fields are generated past times jQuery code inwards my previous post.
Capture Array Values using PHP
Collecting values from to a higher house input fields is pretty simple, induce got a await at event below. Once the value is captured from input fields, y'all tin give notice output it on the browser.
We tin give notice straight access array values using array's indexed number, simply this volition drive mistake if indexed value doesn't be inwards array.
Save values into Database
You tin give notice salve to a higher house captured strings on your database using MySql/MySqli.
Source: Source: Capture Array Values from Dynamic input Fields using PHP
Sumber http://developer-paradize.blogspot.com
Let's assume y'all induce got a HTML shape amongst multiple input fields similar event shown below. This fields are generated past times jQuery code inwards my previous post.
<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 bring upwards attribute mytext[] of each input fields? they all induce got same name. The brackets betoken that the value of such input champaign volition live on posted every mo an array, then about other input champaign way about other value to existing array.
Capture Array Values using PHP
Collecting values from to a higher house input fields is pretty simple, induce got a await at event below. Once the value is captured from input fields, y'all tin give notice output it on the browser.
We tin give notice straight access array values using array's indexed number, simply this volition drive mistake if indexed value doesn't be inwards array.
echo $_POST["mytext"][0]; echo $_POST["mytext"][1];Another vogue is using PHP implode, it converts POST array into string, separated past times commas. Just brand certain POST variable is non empty:
if($_POST){ $subject = implode(",", $_POST["mytext"]); echo $text; }You tin give notice likewise loop through each POST array similar so, number is same every mo above.
<?php if(isset($_POST["mytext"])){ $capture_field_vals =""; foreach($_POST["mytext"] every mo $key => $text_field){ $capture_field_vals .= $text_field .", "; } echo $capture_field_vals; } ?>
Save values into Database
You tin give notice salve to a higher house captured strings on your database using MySql/MySqli.
<?php //MySqli Insert Query $insert_row = $mysqli->query("INSERT INTO tabular array ( captured_fields ) VALUES( $capture_field_vals )"); if($insert_row){ impress 'Success! ID of end inserted tape is : ' .$mysqli->insert_id .'<br />'; } ?>
Source: Source: Capture Array Values from Dynamic input Fields using PHP
Sumber http://developer-paradize.blogspot.com
Comments
Post a Comment