Issue with submitting an array php form

I have a form that displays some information from a database that puts everything in a table and you are able to edit all the tables. Is issue i am having is when i click submit it says it can't find the index data_id, says its undefined. Code is below.

Table:
<form id="form" class="form-horizontal" role="form" data-toggle="validator" method="post" name="edit_interface.php">
			
			<div class="form-group">
					<label class="control-label col-sm-3" for="type">Type:</label>
					<div class="col-sm-6">
					<input name="type" class="form-control" id="type" value="<?php echo $data_interface['interface_type']; ?>">
					</div>
			</div>
			<div class="form-group">
					<label class="control-label col-sm-3" for="ip_address">IP Address:</label>
					<div class="col-sm-6">
					<input name="ip_address" class="form-control" id="ip_address" value="<?php echo $data_interface['ip_address']; ?>">
					</div>
			</div>
</div>
</div>
</div>
			<div class="col-sm-12">	
<div class="panel panel-primary">
                        <div class="panel-heading">
                             ATS Sites
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="edit_interface">
								<?php
$query_interface_data = mysql_query("SELECT * FROM site_router_interface_info WHERE router_interface_id = '$interface_id'") or die (mysql_error());

echo "<thead>";
echo "<tr>";
echo "<th>";
echo "<b>Delete Interface:</b>";
echo "</th>";
echo "<th>";
echo "<b>ID:</b>";
echo "</th>";
echo "</th>";
echo "<th>";
echo "<b>Name:</b>";
echo "</th>";
echo "<th>";
echo "<b>Speed:</b>";
echo "</th>";
echo "<th>";
echo "<b>Vendor:</b>";
echo "</th>";
echo "<th>";
echo "<b>LEC ID:</b>";
echo "</th>";
echo "<th>";
echo "<b>DID:</b>";
echo "</th>";
echo "<th>";
echo "<b>Account ID:</b>";
echo "</th>";
echo "<th>";
echo "<b>Circuit ID:</b>";
echo "</th>";
echo "<th>";
echo "<b>Support Number:</b>";
echo "</th>";
echo "<th>";
echo "<b>Notes:</b>";
echo "</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

while ($info = mysql_fetch_assoc($query_interface_data))
{

$data_id = $info['id'];
$name = $info['name'];
$speed = $info['speed'];
$vendor = $info['vendor'];
$lec = $info['lec'];
$did = $info['did'];
$account_id = $info['account_id'];
$support_number = $info['support_number'];
$circuit_id = $info['circuit_id'];
$notes = $info['notes'];

echo "<tr>";
?>
<td align='middle'>
<a href=remove_router_interface_data.php?id=<?php echo $id; ?>&interface_id=<?php echo $interface_id ?>&router_id=<?php echo $router_id ?>&data_id=<?php echo $data_id; ?> onclick="return confirm('Are you sure you want to Delete this Item?')"><img src='assets/img/decommission_asset.png' width='30' height='30' align='middle'/></a>
</td>
<td><input size="10" class="form-control" type="text" name="data_id[]" value="<?php echo $data_id ?>" readonly> 
</td>
<td><input size="10" class="form-control" type="text" name="name[]" value="<?php echo $name ?>"> 
</td>
<td><input size="15" class="form-control" type="text" name="speed[]" value="<?php echo $speed ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="vendor[]" value="<?php echo $vendor ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="lec[]" value="<?php echo $lec ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="did[]" value="<?php echo $did ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="account_id[]" value="<?php echo $account_id ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="circuit_id[]" value="<?php echo $circuit_id ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="support_number[]" value="<?php echo $support_number ?>"> 
</td>
<td><input size="10" class="form-control" type="text" name="notes[]" value="<?php echo $notes ?>"> 
</td>
<?php
echo "</tr>";

}
?>

</tbody>
			</table>
                        </div>
                        </div>
						
                    </div>
					<button name='form' type="submit" class="btn btn-primary" id="submit"> Edit Interface</button>	
            </form>

Open in new window


Here is the code when you click the button:

if($_SERVER['REQUEST_METHOD'] == "POST") {

$type = $_POST['type'];
$ip_address = $_POST['ip_address'];

mysql_query("UPDATE site_router_interface SET 
interface_type='".$type."',
ip_address='".$ip_address."'
WHERE id = '".$interface_id."'") or die (mysql_error()); 

for ($i=0; $i < count($_POST['data_id']); $i++){

$data_id = $_POST['data_id'][$i];
$name = $_POST['name'][$i];
$speed = $_POST['speed'][$i];
$vendor = $_POST['vendor'][$i];
$lec = $_POST['lec'][$i];
$did = $_POST['did'][$i];
$account_id = $_POST['account_id'][$i];
$support_number = $_POST['support_number'][$i];
$notes = $_POST['notes'][$i];


mysql_query("UPDATE site_router_interface_info SET 
name='".$name."',
speed='".$speed."',
vendor='".$vendor."',
lec='".$lec."',
did='".$did."',
account_id='".$account_id."',
support_number='".$support_number."',
notes='".$notes."'
WHERE id = '".$data_id."'") or die (mysql_error());

}
echo "Interface Saved";


/*
	$server = $_SERVER['HTTP_HOST'];
$current_folder = basename(dirname(__FILE__));

header( "Location: http://$server/$current_folder/router_information.php?id=$id&router_id=$router_id" ) ;
*/
}

Open in new window

ats2012Asked:
Who is Participating?
 
Mark BradyPrincipal Data EngineerCommented:
Your form is not sending anything called "data_id"

your php is trying to loop through an array that does not exist. Also, if this information needs to be an array of values you will need to JSON encode it on the javascript side then decode it into an object or an array on the php side before you can loop through it.

A better solution would be to comma separate these values ie:  "mark,john,lucy" could be the value for a field named "names"

In php you would do this to get them all:
if (isset($_POST['names']) {
   $names = array_map('trim', explode(',', $_POST['names']));
    foreach ($names as $name) {
        // do something with each one here
        echo $name."\n";   
    }
}

Open in new window

0
 
ats2012Author Commented:
Thanks. I got it working
0
 
Mark BradyPrincipal Data EngineerCommented:
B Grade? I showed you where you issue was. How is that a B grade?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.