I am working with dynamic text boxes.
I want to add the data that is entered in the text boxes to my database.
My markup:
<form name="reaction" id="reaction" method="post" action="./send.php">
<input type="text" name="number[]" id="number1" value="15" placeholder="Number 1" /> <br />
<input type="text" name="name[]" id="name1" value="aaaa" placeholder="Name 1" /> <br />
<input type="text" name="price[]" id="price1" value="10" placeholder="Price 1" /> <br />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<input type="text" placeholder="Number '+count+'" name="number[]" id="number'+count+'" data-srno="'+count+'" /><br />';
html_code += '<input type="text" placeholder="Name '+count+'" name="name[]" id="name'+count+'" data-srno="'+count+'" /><br />';
html_code += '<input type="text" placeholder="Price '+count+'" name="price[]" id="price'+count+'" data-srno="'+count+'" /><br />';
html_code += '<button type="button" name="remove_row" id="'+count+'" class="btn btn-danger btn-xs remove_row">X</button><br />';
});
</script>
<button type="submit" class="btn btn-primary" name="send">Save</button>
</form>
Which results in the following document fragment:
<form name="reaction" id="reaction" method="post" action="./send.php">
<input type="text" name="number[]" id="number1" value="15" placeholder="Number 1" /> <br />
<input type="text" name="name[]" id="name1" value="aaaa" placeholder="Name 1" /> <br />
<input type="text" name="price[]" id="price1" value="10" placeholder="Price 1" /> <br />
<input type="text" name="number[]" id="number2" value="12" placeholder="Number 2" /> <br />
<input type="text" name="name[]" id="name2" value="bbbb" placeholder="Name 2" /> <br />
<input type="text" name="price[]" id="price2" value="15" placeholder="Price 2" /> <br />
<input type="text" name="number[]" id="number3" value="38" placeholder="Number 3" /> <br />
<input type="text" name="name[]" id="name3" value="cccc" placeholder="Name 3" /> <br />
<input type="text" name="price[]" id="price3" value="29" placeholder="Price 3" /> <br />
<button type="submit" class="btn btn-primary" name="send">Save</button>
</form>
When submitting the form I want to add the following data to the database:
| session_id | number | name | price |
|-------------|----------|----------|----------|
| 1 | 15 | aaaa | 10 |
| 1 | 12 | bbbb | 15 |
| 1 | 38 | cccc | 29 |
In my PHP code I am using the following to define the text boxes:
foreach($_POST['number'] as $i => $item) {
When I execute the script I only get the data of the first three textboxes. I get in my database:
| session_id | number | name | price |
|-------------|----------|----------|----------|
| 1 | 15 | aaaa | 10 |
After weeks of doing research I have discovered that there is something wrong with the JavaScript part of the code. When I send the data with the text boxes in the second example I get the result I want. When I use JavaScript to create the dynamic text boxes the PHP script will only post the first row (that is not created with JavaScript) to the database.
What is wrong with my script?
Here is the script I am using to add data to my database:
<?php
$correct = true;
$_SESSION['session_id'];
$number = $_POST['number'] ;
$name = $_POST['name'] ;
$price = $_POST['price'] ;
if($correct){
foreach($_POST['number'] as $i => $item) {
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pass');
$query= "INSERT INTO products(session_id, number, name, price) VALUES (:session_id, :number, :name, :price)";
$stmt = $db->prepare($query);
$exec = $stmt->execute(array(
':session_id' => $_SESSION['session_id'],
':number' => $_POST["number"][$i],
':name' => $_POST["name"][$i],
':price' => $_POST["price"][$i]
));
}
}
else
{
header('Location: ../error.php');
}
?>
Result of var_dump on $_POST['number']:
array(1) { [0]=> string(2) "15" }