Jump to content

Form with multiple fields with the same names.


Ryflex

Recommended Posts

Hi all,

 

I'm trying to build a menu which can be edited in the browser and will be loaded from the server.

I have created a form which loads al the menu items from the DB and gives the field an array based name. I have tried several ways to get the info out of the database but not one of them works.

Also when I use print_r($_POST); it gives the information like this:

Array ( [name] => Array ( [0] => Home [1] => Buildings [2] => Log-out ) [link] => Array ( [0] => home.php [1] => buildings.php [2] => ../process.php ) [n] => 4 [submit] => Adjust )

 

Below is the code of the form:

<form name="menu-edit" action="include/process.php" method="POST">
				<ul id="level3">

				<?php	
				//set $n at 1
				$n			= 1;				

				//query database for all level 3 parent items
				$sql 		= $database->connection->prepare("SELECT * FROM ".TBL_MENU." WHERE `userlevel` = '3' AND `parent` = '0' ORDER by `order` ASC");
     			$sql->execute();

				//while loop to iterate the menu items
				while($row = $sql->fetch()) {
					echo "<li id='listItem_".$row['id']."'><img src='images/arrow.png' alt='move' width='16' height='16' class='handle' /><input type='text' name='name[]' value='".$row['name']."' /><input type='text' name='link[]' value='".$row['link']."' /></li>";
					$n++;
				}
				?>
				<input type="hidden" name="n" value="<?php echo $n; ?>" />
				</ul>
				<input type="submit" name="submit" value="Adjust" />
			</form>

 

Below is what I have now as process.php:

<?php 
//check if user has admin priveleges
if(!$session->isUserlevel(9)) {
header("Location:../main.php");
}

if(!$_POST['submit']) {
header("Location:..menu.php");
}
print_r($_POST);
$n			= $_POST['n'];
$s			= 1;
echo "<br>".$_POST['name'.$s];

$name[] = $_POST['name'];

foreach($name[] as $key => $value)
{
echo $key."<br>".$value."<br><br>";
}
?>

 

Hope you can help

I couldnt understand what you need...

 

In your form you will create 2 arrays with theirs keys numerics ($name and $link). You want to do one array with the two informations? Something like that:

 

$menu = array(
                      0 => array(
                                      'name' => 'Menu Name',
                                      'link'     => 'Menu link',
                       )
                  );

 

If yes, you can do something like that:

 

<?php
// your other stuff here

while($row = $sql->fetch()) {
    echo "<li id='listItem_".$row['id']."'>
                      <img src='images/arrow.png' alt='move' width='16' height='16' class='handle' />
                      <input type='text' name='menu[".$row['id']."][name]' value='".$row['name']."' />
                      <input type='text' name='menu[".$row['id']."][link]' value='".$row['link']."' />
               </li>";
    $n++;
}
?>

For others to use with the same problems in the future.

 

The script that creates the form:

<form name="menu-edit" action="include/process.php" method="POST">
				<ul id="level3">

				<?php	
				//set $n at 1
				$n			= 1;				

				//query database for all level 3 parent items
				$sql 		= $database->connection->prepare("SELECT * FROM ".TBL_MENU." WHERE `userlevel` = '3' AND `parent` = '0' ORDER by `order` ASC");
     			$sql->execute();

				//while loop to iterate the menu items
				while($row = $sql->fetch()) {
					echo "<li id='listItem_".$row['id']."'>
					<img src='images/arrow.png' alt='move' width='16' height='16' class='handle' />
					<input type='text' name='menu[".$n."][name]' value='".$row['name']."' />
					<input type='text' name='menu[".$n."][link]' value='".$row['link']."' />
					<input type='hidden' name='menu[".$n."][id]' value='".$row['id']."' /></li>";
					$n++;
				}
				?>
				<input type="hidden" name="n" value="<?php echo $n; ?>" />
				</ul>
				<input type="submit" name="submit" value="Adjust" />
			</form>

 

The script that extracts the information to insert into the database:

 

<?php
if(!$_POST['submit']) {
header("Location:..menu.php");
}
//get the value of $n represents the number of rows
$n			= $_POST['n'];

//loop through every row to update
for($s = 1; $s < $n; $s++) {
$name		= $_POST['menu'][$s]['name'];
$link		= $_POST['menu'][$s]['link'];
$id			= $_POST['menu'][$s]['id'];
echo "UPDATE ".TBL_MENU." SET `name` = '$name', `link` = '$link' WHERE `id` = '$id'"."<br>";
}
?>

 

Have a good weekend!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.