Chamza Posted July 16, 2009 Share Posted July 16, 2009 First of all, I am very new to PHP and come from a background of designing, aka HTML and CSS. PHP has been blowing my mind these past several weeks and I am truly stuck. Basically, I have a very simple drop-down menu and a submit button made in HTML. What I want to do is so that when I choose something from the drop down menu, the "submit" button will execute a different PHP code depending on my choice. So, for example, if I choose "Everything" in the dropdown menu, the submit button's destination will turn in to everything.php without reloading the page, and if i choose "Jeans" the button will turn in to jeans.php behind the scenes. Here is my html: <form method="post"> <select name="category"> <option value="everything">Everything</option> <option value="jeans">Jeans</option> <option value="watches">Watches</option> </select> <input type="submit" /> </form> I've attempted to add functionality with php and I can post that code but it is completely wrong. So if anyone can give me some pointers or write some of the code themselves to show me, I'd appreciate it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/166135-html-noob-needs-help-adding-php-interactivity/ Share on other sites More sharing options...
J.Daniels Posted July 16, 2009 Share Posted July 16, 2009 PHP is a server side scripting language, so once content is rendered, PHP cannot modify it. You'll have to use JavaScript to change the action value when selecting an option. Quote Link to comment https://forums.phpfreaks.com/topic/166135-html-noob-needs-help-adding-php-interactivity/#findComment-876167 Share on other sites More sharing options...
phporcaffeine Posted July 16, 2009 Share Posted July 16, 2009 Doesn't sound like a php issue here unless I am missing something. You should be able to do what your talking about using the onChange event in the select form element. Quote Link to comment https://forums.phpfreaks.com/topic/166135-html-noob-needs-help-adding-php-interactivity/#findComment-876168 Share on other sites More sharing options...
MadTechie Posted July 16, 2009 Share Posted July 16, 2009 Okay.. first of all posting a form will reload the page.. if you don't want the page to reload your need to looking to frames or AJAX, with that said.. here a basic one which will require a reload, but should give you an idea (untested) <form method="post"> <select name="category"> <option value="everything">Everything</option> <option value="jeans">Jeans</option> <option value="watches">Watches</option> </select> <input type="submit" /> </form> <?php //default page $default = "default.php"; //check something has been selected if(!empty($_POST['category'])) { //convert to lowercase $cat = strtolower($_POST['category']).".php"; }else{ $cat = $default; } //an array of allowed files (for security reasons) $pages = array($default,"everything.php","jeans.php","watches.php"); //check the selected file is in the allowed list if (in_array($cat, $pages )) { //if so include it echo "loading $cat"; include $cat; }else{ //if not then use default.php include $default; } ?> EDIT: as a note you could also use header to redirect to a page .. Quote Link to comment https://forums.phpfreaks.com/topic/166135-html-noob-needs-help-adding-php-interactivity/#findComment-876169 Share on other sites More sharing options...
Amtran Posted July 16, 2009 Share Posted July 16, 2009 You could also just include something in the destination script that would change the action depending on the value of "category". i.e. <?php if ($_POST['category'] = 'everything') { //Do something... } elseif ($_POST['category'] = 'jeans') { //Do something else } elseif ($_POST['category'] = 'watches') { //Do something else entirely } else { echo 'Something screwed up...'; } ?> This would keep all your code in one file, and you won't need to fiddle around w/ JavaScript. The user wouldn't be any the wiser, either. Quote Link to comment https://forums.phpfreaks.com/topic/166135-html-noob-needs-help-adding-php-interactivity/#findComment-876209 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.