Javascript 将动态输入字段添加为数组并存储到数据库中
我有一个表单中的输入字段进入数据库Javascript 将动态输入字段添加为数组并存储到数据库中,javascript,php,mysql,arrays,input,Javascript,Php,Mysql,Arrays,Input,我有一个表单中的输入字段进入数据库 <div id="cities"> <input type="text" name="city[]" id="1" > <input type="button" id="add_city()" onClick="addCity()" value="+" /> </div> 我只得到一个结果,数据库只存储一行custId works State works Country works e
<div id="cities">
<input type="text" name="city[]" id="1" >
<input type="button" id="add_city()" onClick="addCity()" value="+" />
</div>
我只得到一个结果,数据库只存储一行custId works State works Country works exept$cities它只存储一次即使我输入了更多的城市,有人能帮我吗
我是从你那里抄来的
if(is_array($state)){
while(list($keys,$value) = each($state)){
$sql3 = "INSERT INTO states (custId,State,country,city)
VALUES ('$custId','$value','$country','$city')";
$q3 = mysqli_query($db_conx,$sql3) or die(mysqli_error($db_conx));
}
}
上面的代码以相同的形式来自TexBox,并且可以正常工作
<form enctype="multipart/form-data" action="sendInfo.php" method="POST">
Id: <input name="custId" id="custId">
Name: <input name="name" id="name">
Address: <input name="address" id="address">
City: <div id="cities">
<input type="text" name="city[]" id="1" >
<input type="button" id="add_city()" onClick="addCity()" value="+" />
</div>
State: <?php include ("../maps/states3.php");?>
Zip Code: <input type="text" name="zipCode">
Country: <select name="country" id="country" style="width: 150px;">
<option value="United States">United States</option>
</select>
Link: http:// <input name="url" id="url">
Category: <input name="category" id="category">
Phone: <input name="phone" id="phone">
Fax: <input name="fax" id="fax" >
e-mail: <input name="email" id="email">
Order Number: <input name="orderNumber" id="orderNumber" >
Image name: <input name="image" id="image">
Chose sites to be displayed:
<input type="radio" value="yes" name="site1"> Site1 <br />
<input type="radio" value="yes" name="site2"> Site2 <br />
<input type="radio" value="yes" name="site3"> Site3 <br />
Start time: <input type="date" name="start">
Stop time: <input type="date" name="stop">
Table: <?php include ("tables2.php");?>
Comments: <textarea name="comments" id="comments" title="comments" >
</textarea>
<input name"submit" type="submit" value="submit" />
</form>
身份证件:
姓名:
地址:
城市:
声明:
邮政编码:
国家:
美国
链接:http://
类别:
电话:
传真:
电邮:
订单号:
图像名称:
选择要显示的站点:
站点1
站点2
站点3
开始时间:
停止时间:
表:
评论:
我想您忘了为文本框编写value属性
<input type="text" name="city[]" id="1" > instead of this use
<input type="text" name="city[]" id="1" value="1" >
}你是如何让城市回归PHP的呢?@adeneo数据库只存储一个row@adeneo或者,它存储的不是一个值,而是单词array,这里一定有您没有向我们展示的内容。等等,正如@adeneo指出的,你是如何将城市提交给php的?这是一个大表单的一部分,分为几个部分。这是唯一一个不起作用的部分。所有这些都来自$u POST[“”]
<form enctype="multipart/form-data" action="sendInfo.php" method="POST">
Id: <input name="custId" id="custId">
Name: <input name="name" id="name">
Address: <input name="address" id="address">
City: <div id="cities">
<input type="text" name="city[]" id="1" >
<input type="button" id="add_city()" onClick="addCity()" value="+" />
</div>
State: <?php include ("../maps/states3.php");?>
Zip Code: <input type="text" name="zipCode">
Country: <select name="country" id="country" style="width: 150px;">
<option value="United States">United States</option>
</select>
Link: http:// <input name="url" id="url">
Category: <input name="category" id="category">
Phone: <input name="phone" id="phone">
Fax: <input name="fax" id="fax" >
e-mail: <input name="email" id="email">
Order Number: <input name="orderNumber" id="orderNumber" >
Image name: <input name="image" id="image">
Chose sites to be displayed:
<input type="radio" value="yes" name="site1"> Site1 <br />
<input type="radio" value="yes" name="site2"> Site2 <br />
<input type="radio" value="yes" name="site3"> Site3 <br />
Start time: <input type="date" name="start">
Stop time: <input type="date" name="stop">
Table: <?php include ("tables2.php");?>
Comments: <textarea name="comments" id="comments" title="comments" >
</textarea>
<input name"submit" type="submit" value="submit" />
</form>
<input type="text" name="city[]" id="1" > instead of this use
<input type="text" name="city[]" id="1" value="1" >
foreach($city as $key=>$value){
echo $key."-----".$value;
}
if(count($state)){
foreach($state as $key=>$val){//$val will contain state value while $key will contain key or index of array.
$sql3 = "INSERT INTO states (custId,State,country,city)
VALUES ('$custId','$val','$country','$city')";
$q3 = mysqli_query($db_conx,$sql3) or die(mysqli_error($db_conx));
}