DeFActo Posted April 6, 2008 Share Posted April 6, 2008 Hello all, i have a code echo '<select name="menu">'; echo '<option>Artist</option>'; echo '<option>Song</option>'; echo '</select>'; could someone tell me how to do so that if i choose Artist my variable $value gets the value "artists" ? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/ Share on other sites More sharing options...
wildteen88 Posted April 6, 2008 Share Posted April 6, 2008 I guess you are submitting the form when the user chooses an item from the list, if so use $_POST['name'] or $_GET['name'] depending on your forms submit method. Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510605 Share on other sites More sharing options...
DeFActo Posted April 6, 2008 Author Share Posted April 6, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510635 Share on other sites More sharing options...
trq Posted April 6, 2008 Share Posted April 6, 2008 It would be $_GET['selectElem'], not $_GET['selectElem'][] Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510636 Share on other sites More sharing options...
wildteen88 Posted April 6, 2008 Share Posted April 6, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510641 Share on other sites More sharing options...
DeFActo Posted April 6, 2008 Author Share Posted April 6, 2008 thats what i got Notice: Undefined index: selectElem in menu.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510642 Share on other sites More sharing options...
wildteen88 Posted April 6, 2008 Share Posted April 6, 2008 Read my post above. Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510643 Share on other sites More sharing options...
DeFActo Posted April 6, 2008 Author Share Posted April 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510644 Share on other sites More sharing options...
wildteen88 Posted April 6, 2008 Share Posted April 6, 2008 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510651 Share on other sites More sharing options...
DeFActo Posted April 6, 2008 Author Share Posted April 6, 2008 thanks a lot, that what i needed Quote Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.