1

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>
6
  • And how exactly are you getting the cities back to PHP ? Commented Sep 7, 2014 at 9:59
  • @adeneo the database stores only one row Commented Sep 7, 2014 at 10:01
  • @adeneo or instead of a value it stores the word array Commented Sep 7, 2014 at 10:02
  • There must be something you are not showing us. Etc as @adeneo points out, how are you submitting the city to the php? Commented Sep 7, 2014 at 10:05
  • this is a part of a big form is divided into sections this is the only section that does not work it all comes from $_POST[''] Commented Sep 7, 2014 at 10:10

1 Answer 1

1

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));
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.