Jump to content

Recommended Posts

i have tried to use it, but getting error

<form method="post" action="menu.php">
<?php
echo  '<select id="selectElemId" name="selectElem[]">';
echo  '<option value="ham">Ham</option>';
echo  '<option value="cheese">Cheese</option>';
echo  '<option value="hamcheese">Ham and Cheese</option>';
echo  '</select>';
$var=$_GET['selectElem'][];
?>
</form>

Link to comment
https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510635
Share on other sites

i have tried to use it, but getting error

<form method="post" action="menu.php">
<?php
echo  '<select id="selectElemId" name="selectElem[]">';
echo  '<option value="ham">Ham</option>';
echo  '<option value="cheese">Cheese</option>';
echo  '<option value="hamcheese">Ham and Cheese</option>';
echo  '</select>';
$var=$_GET['selectElem'][];
?>
</form>

Is that all your code? If it is there is a few issues:

1. There is no submit button. In order for PHP to retrieve data from the form it needs to be submitted.

2. Your form method is set to post so you'll use the $_POST superglobal instead, eg $_POST['selectElemId']

3. You should always check to see if user defined variables exists first before using them to prevent the "Underfined index notice" notices (these are not errors)

Link to comment
https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510641
Share on other sites

i have tried to use it, but getting error

<form method="post" action="menu.php">
<?php
echo  '<select id="selectElemId" name="selectElem[]">';
echo  '<option value="ham">Ham</option>';
echo  '<option value="cheese">Cheese</option>';
echo  '<option value="hamcheese">Ham and Cheese</option>';
echo  '</select>';
$var=$_GET['selectElem'][];
?>
</form>

Is that all your code? If it is there is a few issues:

1. There is no submit button. In order for PHP to retrieve data from the form it needs to be submitted.

2. Your form method is set to post so you'll use the $_POST superglobal instead, eg $_POST['selectElemId']

i'd like to do it without submit button.

Link to comment
https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510644
Share on other sites

Then you'll need use a bit of javascript to submit the form once a user has selected an item from the list

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="selectElem" onChange="this.form.submit()">
<option>Filling</option>
<option value="ham">Ham</option>
<option value="cheese">Cheese</option>
<option value="hamcheese">Ham and Cheese</option>
</select>
</form>

<?php

if(isset($_POST['selectElem']))
{
    echo $_POST['selectElem'];
}

?>

Link to comment
https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510651
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.