I have multiple inputs
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
...and so on (depending on how many they want)
On form submit (using $_POST), i want to take all of the inputs (which by the way, we'll never now how many will be there [dynamic jquery add/remove input form] ) and bine them into one variable, all nicely separated by ^^^.
B.T.W, I am using this format to insert into mysql
$sql = mysql_query("INSERT INTO mixtapes (title, songs, posted_by_id, description, date)
VALUES('$mixtapetitle','$allmixtapetracks','$posted_by_id','$mixtapedescription', now())")
or die (mysql_error());
So in the end i want to pack all of the dynamic inputs into one single variable, each one separated by ^^^. The example below is how i want the variable $allmixtapetracks to look, so when I insert it, it looks EXACTLY like that.
Value of input 1^^^Value Of Input 2^^^Value of Input 3^^^Value of input 4^^^
EDIT:
This coding is causing my error. When the inputs are dynamicly created, the php $_POST ignores them. Why? I am not sure.
JavaScript:
<script type="text/javascript">
$(document).ready(function(){
var counter = 3;
$("#addButton").click(function () {
if(counter>40){
alert("The maximum allowed tracks is 40");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.html('<table><tr><td width="88"><label>Track #'+ counter + ' : </label></td><td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td></tr></table>');
newTextBoxDiv.appendTo("#TextBoxesGroup").hide().show(50);
counter++;
});
$("#removeButton").click(function () {
if(counter==3){
alert("Your mixtape must contain at least two tracks!");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove().show().hide(40);
});
});
</script>
HTML:
<form action="add-mixtape.php" name="addmixtapeForm" class="addmixtapeForm" id="addmixtapeForm" autoplete="off" method="post" enctype="multipart/form-data">
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<table>
<tr>
<td width="88"><label>Track #1: </label></td>
<td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td>
</tr>
</table>
<table>
<tr>
<td width="88"><label>Track #2: </label></td>
<td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td>
</tr>
</table>
</div>
</div>
<input type="button" value="Add Track" id="addButton" class="addREMOVEmixtapetrack">
<input type="button" value="Remove Track" id="removeButton" class="addREMOVEmixtapetrack">
</form>
I have multiple inputs
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
...and so on (depending on how many they want)
On form submit (using $_POST), i want to take all of the inputs (which by the way, we'll never now how many will be there [dynamic jquery add/remove input form] ) and bine them into one variable, all nicely separated by ^^^.
B.T.W, I am using this format to insert into mysql
$sql = mysql_query("INSERT INTO mixtapes (title, songs, posted_by_id, description, date)
VALUES('$mixtapetitle','$allmixtapetracks','$posted_by_id','$mixtapedescription', now())")
or die (mysql_error());
So in the end i want to pack all of the dynamic inputs into one single variable, each one separated by ^^^. The example below is how i want the variable $allmixtapetracks to look, so when I insert it, it looks EXACTLY like that.
Value of input 1^^^Value Of Input 2^^^Value of Input 3^^^Value of input 4^^^
EDIT:
This coding is causing my error. When the inputs are dynamicly created, the php $_POST ignores them. Why? I am not sure.
JavaScript:
<script type="text/javascript">
$(document).ready(function(){
var counter = 3;
$("#addButton").click(function () {
if(counter>40){
alert("The maximum allowed tracks is 40");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.html('<table><tr><td width="88"><label>Track #'+ counter + ' : </label></td><td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td></tr></table>');
newTextBoxDiv.appendTo("#TextBoxesGroup").hide().show(50);
counter++;
});
$("#removeButton").click(function () {
if(counter==3){
alert("Your mixtape must contain at least two tracks!");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove().show().hide(40);
});
});
</script>
HTML:
<form action="add-mixtape.php" name="addmixtapeForm" class="addmixtapeForm" id="addmixtapeForm" autoplete="off" method="post" enctype="multipart/form-data">
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<table>
<tr>
<td width="88"><label>Track #1: </label></td>
<td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td>
</tr>
</table>
<table>
<tr>
<td width="88"><label>Track #2: </label></td>
<td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td>
</tr>
</table>
</div>
</div>
<input type="button" value="Add Track" id="addButton" class="addREMOVEmixtapetrack">
<input type="button" value="Remove Track" id="removeButton" class="addREMOVEmixtapetrack">
</form>
Share
Improve this question
edited Apr 9, 2012 at 2:36
Alex Sarnowski
asked Apr 8, 2012 at 17:17
Alex SarnowskiAlex Sarnowski
7391 gold badge11 silver badges21 bronze badges
1
- 1 Note that this breaks even the first normal form of database structures. You should avoid storing multiple values in the same field since it's very hard to query the data that way (but who needs to search on track names anyway, right?). Even more problematic is that you have no question! What do you need and what have you tried? – Emil Vikström Commented Apr 8, 2012 at 17:21
3 Answers
Reset to default 2$alltracks = implode('^^^', $_POST['track']) . '^^^';
Test code:
<?php
$alltracks = implode('^^^', $_POST['track']) . '^^^';
echo $alltracks;
?>
<form action="test.php" method="post">
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input type="submit">
</form>
I strongly remend against storing multiple values in one column. The correct strategy is to create a normalized table which links mixtapes to tracks:
CREATE TABLE tapetracks (
mixtapeid INT NOT NULL,
trackname VARCHAR(256) NOT NULL
);
Then loop over the input values and insert each into the table:
$mixtapeid = <the current tape you are adding to>;
foreach ($_POST['track'] AS $track) {
$track = mysql_real_escape_string($track);
$result = mysql_query("INSERT INTO tapetracks (mixtapeid, trackname) VALUES ($mixtapeid, '$track')");
if (!$result) {
echo mysql_error();
break;
}
}
When storing multiple values in one column, you lose the ability to utilize a column index when querying, and it bees a big hassle to query for individual values, requiring calls to REPLACE()
and FIND_IN_SET()
which may fail if the track names contain mas. It really is the wrong strategy.
Yes, sure you can:
$bined = "";
foreach ( $_POST['track'] as $t ){
$bined .= $t . "^^^";
}
$bined = substr(0,-3, bined) // only if you want to strip last 3 "^^^" symbols
This should do it....