Hi to all, If I have a form such as: <form id="form_items" action="" method="post" enctype="multipart/form-data"> <input type="submit" name="btn_add_items" value="Add Items" /> <input type="submit" name="btn_del_items" value="Delete Selected Items" /> <input type="submit" name="btn_add_custom" value="Add Custom Items" /> </form> Code (markup): I want the following conditions to be possible: If "btn_add_items" is selected then post to add_items.php If "btn_del_items" is selected then post to del_items.php If "btn_add_custom" is selected then post to add_custom_items.php How do I code that into the form action? Thanks!
You can do it on the processing page with php, or you can use javascript to do it on the same page. Doing it on a form processing page is probably going to be the easiest way to do it.
Simple. if (isset($_POST['btn_add_items'])) { // do this block of codin } elseif (isset($_POST['btn_del_items'])) { // do this block of coding } elseif (isset($_POST['btn_add_custom'])){ // do this block of coding } PHP: Hope this helps.
Thanks West, I understand the php part of this. Perhaps I should have been more clear. I need to know how to apply the conditional code to the form action. How do I put the code in the <form action="">? Make sense? Thanks!
Usually the page can do more then one job. <form action="filename.php"> if (isset($_POST['btn_add_items'])) { // do this block of codin } elseif (isset($_POST['btn_del_items'])) { // do this block of coding } elseif (isset($_POST['btn_add_custom'])){ // do this block of coding } //if we fall through, display the form. PHP: this is a simplistic example.
Somehow my question isn't being understood... I want to know how to make the form's ACTION be conditional. <form action=""> What goes in between the "" to accomplish the conditional submittal depending on the button selected?? Can it be a php function perhaps?
Oh, then you need javascript perhaps. I'm no JS expert. West, The example I posted in whole was simplistic to what I referred to. Your code was fine.
Noppid, I got you. I read it wrong. Apologies. Also, I agree with you. This is definitely not a PHP job. Sounds more like a javascript job. This needs to happen before post, but after load and that's not PHP.
<script language="javascript"> function formaction( str ) { switch( str ) { case "add": document.form_items.action = 'add_items.php'; document.form_items.submit(); break; case "del": document.form_items.action = 'del_items.php'; document.form_items.submit(); break; case "cus": document.form_items.action = 'add_custom_items.php'; document.form_items.submit(); break; } } </script> <form id="form_items" name="form_items" action="" method="post" enctype="multipart/form-data"> <input type="button" value="Add Items" onClick="formaction('add')"/> <input type="button" value="Delete Items" onClick="formaction('del')"/> <input type="button" value="Add Custom Items" onClick="formaction('cus')"/> </form> HTML:
We have actually shown you how to do this in both PHP and JS now. Perhaps there is still a communication gap?
php is a server side language, it cannot interact that way with html, there are two ways to do it, and you now have both, so choose ....