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

Link to comment
Share on other sites

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++;
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.