我有多个输入
<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)
在表单提交(使用$ _POST)时,我想要获取所有输入(顺便说一下,我们现在永远不会有多少[动态jquery添加/删除输入表单])并将它们组合成一个变量,都被^^^很好地分开。
B.T.W,我使用这种格式插入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());
所以最后我想将所有动态输入打包成一个变量,每个变量用^^^分隔。下面的例子是我想要变量$ allmixtapetracks的样子,所以当我插入它时,它看起来非常像。
输入值1 ^^^输入值2 ^^^输入值3 ^^^输入值4 ^^^
此编码导致我的错误。当动态创建输入时,php $ _POST会忽略它们。为什么?我不确定。
<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>
<form action="add-mixtape.php" name="addmixtapeForm" class="addmixtapeForm" id="addmixtapeForm" autocomplete="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>
答案 0 :(得分:2)
$alltracks = implode('^^^', $_POST['track']) . '^^^';
测试代码:
<?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>
答案 1 :(得分:2)
我强烈建议不要在一列中存储多个值。正确的策略是创建一个将mixtapes链接到轨道的规范化表:
CREATE TABLE tapetracks (
mixtapeid INT NOT NULL,
trackname VARCHAR(256) NOT NULL
);
然后循环输入值并将每个值插入表中:
$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;
}
}
在一个列中存储多个值时,您无法在查询时使用列索引,查询单个值会变得很麻烦,需要调用REPLACE()
和FIND_IN_SET()
如果曲目名称包含逗号,则可能会失败。这确实是错误的策略。
答案 2 :(得分:0)
是的,确定你可以:
$combined = "";
foreach ( $_POST['track'] as $t ){
$combined .= $t . "^^^";
}
$combined = substr(0,-3, combined) // only if you want to strip last 3 "^^^" symbols
这应该做到....