I have created dynamic text boxes that can be added or removed by the user and I want to send whatever the user has input straight to the database. At the minute, it only seems to be sending whatever is in the first text box and ignoring the rest.
What I need is - for each brand name that is entered, send brandName, brandCategory, brandKeyword into the database as a new row.
Any suggestions on what I can do?
<form id="manage_brands" action="add_brands.php" method="post">
<form role="form" method="post">
<h2>Add Brand Information</h2>
<p class="text-box">
<label for="box1">Brand Name:<span class="box-number">1</span></label>
<input type="text" class="textbox" name="brandName[]" value="" id="brandName" />
<a class="add-box" href="#"><img id="icon2" src="images/plus.png"></a>
</p>
<p>
<label for="bCategory">Brand Category:</label>
<select class="textbox" id="bCategory" name="brandCategory">
<option value="">Select...</option>
<option value="Arts and Entertainment">Arts and Entertainment</option>
<option value="Automotive">Automotive</option>
<option value="Business">Business</option>
<option value="Careers">Careers</option>
<option value="Education">Education</option>
<option value="Family and Parenting">Family and Parenting</option>
<option value="Health and Fitness">Health and Fitness</option>
<option value="Food and Drink">Food and Drink</option>
<option value="Hobbies and Interests">Hobbies and Interests</option>
<option value="Home and Garden">Home and Garden</option>
<option value="Law, Government and Politics">Law, Government and Politics</option>
<option value="News">News</option>
<option value="Personal Finance">Personal Finance</option>
<option value="Pets">Pets</option>
<option value="Society">Society</option>
<option value="Science">Science</option>
<option value="Shopping">Shopping</option>
<option value="Sports">Sports</option>
<option value="Style and Fashion">Style and Fashion</option>
<option value="Technology and Computing">Technology and Computing</option>
<option value="Travel">Travel</option>
<option value="Real Estate">Real Estate</option>
<option value="Religion and Spirituality">Religion and Spirituality</option>
</select>
</p>
<p>
<label for="brandKeyword">Brand Keyword:</label>
<textarea class="FormElement" name="brandKeyword" id="brandKeyword" placeholder="Add relevant keywords"></textarea>
</p>
<br />
<input type="submit" class="button" name="submit" value="Store Brand" onclick="return confirm('Do you wish to add a new brand?');"/>
 
<a href="home.php" class="link">Cancel</a>
</form>
PHP
if (empty($errors)) {
// Perform Create
$brandName = mysql_prep($_POST["brandName[]"]);
$brandCategory = mysql_prep($_POST["brandCategory"]);
$brandKeyword = mysql_prep($_POST["brandKeyword"]);
$addedBy = mysql_prep($_SESSION['username']);
$keyword = array($brandKeyword);
$count = count($keyword);
//implode - the array to join to a string
//comma is the glue string between each string/displays contents of an array as a string using defined parameter ","
for ($i = 0; $i < $count; $i++) {
$brandKeyword = implode(',', $keyword);
}
$query = "INSERT INTO brands (";
$query .= " brandName, brandCategory, brandKeyword, addedBy ";
$query .= ") VALUES (";
$query .= " '{$brandName}', '{$brandCategory}', '{$brandKeyword}', '{$addedBy}' ";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["message"] = "Brand has been stored";
redirect_to("add_brands.php");
} else {
// Failure
$_SESSION["message"] = "Information could not be stored";
}
Javascript
jQuery(document).ready(function($){
$('#manage_brands .add-box').click(function(){
var n = $('.text-box').length + 1;
var box_html = $('<p class="text-box"><label for="box' + n + '">Brand Keyword <span class="box-number">' +
'' + n + '</span></label> <input type="text" class="textbox" name="brandName[]" value="" id="box' + n + '" />' +
' <a href="#" class="remove-box"><img id="icon2" src="images/minus.png"></a></p>');
box_html.hide();
$('#manage_brands p.text-box:last').after(box_html);
box_html.fadeIn('slow');
return false;
});
$('#manage_brands').on('click', '.remove-box', function(){
$(this).parent().css( 'background-color', '#ffffff' );
$(this).parent().fadeOut("slow", function() {
$(this).remove();
$('.box-number').each(function(index){
$(this).text( index + 1 );
});
});
return false;
});
});