I have this input field in a form that goes into database
<div id="cities">
<input type="text" name="city[]" id="1" >
<input type="button" id="add_city()" onClick="addCity()" value="+" />
</div>
This is a javascript that adds or deletes extra fields
var i = 1;
function addCity(){
if (i <= 20){
i++;
var div = document.createElement('div');
div.style.color = "white";
div.setAttribute('class', 'myclass');
div.innerHTML = '<input type="text" name="city[]" id="'i'" ><input type="button" id="add_city()" onClick="addCity()" value="+" /><input type="button" value="-" onclick="removeCity(this)">';
document.getElementById('cities').appendChild(div);
}
}
function removeCity(div) {
document.getElementById('cities').removeChild( div.parentNode );
i--;
}
up to here it all seems ok but when I run this code
if(is_array($city)){
while(list($key,$cities) = each($city)){
$sql4 = "INSERT INTO states (custId,State,country,city)
VALUES ('$custId','$value','$country','$cities')";
$q4 = mysqli_query($db_conx,$sql4) or die(mysqli_error($db_conx));
}
}
I only get one result, the database stores only one row custId works State works Country works exept $cities it only gets store one time even if I input more cities can anyone help me
I copied it from
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));
}
}
the above code comes from texboxes in the same form and it works
<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>
I think you forget to write value property for text box
<input type="text" name="city[]" id="1" > instead of this use
<input type="text" name="city[]" id="1" value="1" >
and before query you can see all values posted by using print_r($array) and better use foreach like this
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));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.