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. 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. 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> 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'][] 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) 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 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. 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. 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']; } ?> 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 Link to comment https://forums.phpfreaks.com/topic/99830-solved-menu/#findComment-510656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.