daalouw Posted December 30, 2008 Share Posted December 30, 2008 I would appreciate any assitance you can offer please. I have data in a form that needs to respond to 1 of 2 buttons. There are 2 options... Update or Delete. Update will take whatever is typed and update the MySQL table. Delete will just delete the data from the MySQL table. I send all form responses to my index.php page and then decide which PHP source to include based on the name value pair I send it from my forms. Relevent Index.php code is below: <?php if (!isset($_REQUEST['content'])) include("main.inc"); else { $content = $_REQUEST['content']; $nextpage = $content . ".inc"; include($nextpage); } ?> However, I am unable to detect which button was selected (Update or Delete) on the original form. Here is my code: echo "<form action=\"index.php\" method=\"post\" target=\"_self\">"; echo "<strong>Title *</strong><br /><input type=\"text\" size=\"45\" value=\"$title\" name=\"title\"><br /><br />"; echo "<strong>News article *</strong><br /><textarea rows=\"5\" cols=\"50\" name=\"article\">$article</textarea><br /><br /><br />"; echo "<input type=\"submit\" value=\"Update\">"; echo "<input type=\"hidden\" name=\"content\" value=\"updatenews\">"; echo "<input type=\"submit\" value=\"Delete\">"; echo "<input type=\"hidden\" name=\"content\" value=\"deletenews\">"; echo "</form>"; In the code above, no matter what I click, it always goes to deletenews sicen thaty is the last hidden type definition. I do not know how to differentiate between the 2 buttons. I have seen other examples on how to do this but none of them use the index.php model where I am sending the 'content' and 'value' name value pair. Any help is much appreciated. Deon Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/ Share on other sites More sharing options...
peranha Posted December 30, 2008 Share Posted December 30, 2008 I think you will need to change the name of one of the buttons. name=\"content\" name=\"Delete\" Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725948 Share on other sites More sharing options...
daalouw Posted December 30, 2008 Author Share Posted December 30, 2008 Thank you for yoru quick response. If I change the name to Delete as you suggest, how will control be handed to my code to handle the delete function via the Index.php model I am using? Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725949 Share on other sites More sharing options...
peranha Posted December 30, 2008 Share Posted December 30, 2008 <?php if (isset($_POST['content'])) { // Stuff here if update is clicked } elseif (isset($_POST['Delete'])) { // Stuff here if delete is clicked } // Rest of the page goes here. ?> Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725954 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Get rid of the hidden fields and give the submit buttons the same name, then you can test to see the value of the submit button: <?php echo '<form action="index.php" method="post">'; echo '<strong>Title *</strong><br /><input type="text" size="45" value="' . $title .'" name="title"><br /><br />'; echo '<strong>News article *</strong><br /><textarea rows="5" cols="50" name="article">' . $article . '</textarea><br /><br /><br />'; echo '<input type="submit" value="Update" name="submit">'; echo '<input type="submit" value="Delete" name="submit">'; echo "</form>"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725959 Share on other sites More sharing options...
daalouw Posted December 30, 2008 Author Share Posted December 30, 2008 Thank you for your reply Ken. I am still rather new to PHP/HTML... if I make the change as you suggested, how do I then execute the relevant delete or update code I need to via index.php ? Index.php is expecting 'content' to determine the next code to execute. Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725962 Share on other sites More sharing options...
daalouw Posted December 30, 2008 Author Share Posted December 30, 2008 Peranha, if I look at your suggestion of changing the name, how will index.php know when to display main.inc as the default page if no 'delete' or 'conent' info exists. Deon Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725971 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 You can check the value of the submit button instead: <?php if (isset($_POST['submit'])) { switch ($_POST['submit']) { case 'Update': // // code for update here // break; case 'Delete': // // code for delete here // break; } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-725978 Share on other sites More sharing options...
daalouw Posted December 30, 2008 Author Share Posted December 30, 2008 Ken, if I check on the submit name, then all my regular navigation bars (menu) won't work because they all use 'content' in the href ? i.e. .../index.php/content=newpage.inc. I have really been breaking my brain over this... every possible solution I come up with breaks something else. Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-726143 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 No, you can program your scripts anyway you need to so they work. When checking for values passed on the URL, you need to check the $_GET array: <?php if (isset($_GET['content'])) { // // code here // } ?> Ken Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-726176 Share on other sites More sharing options...
daalouw Posted December 30, 2008 Author Share Posted December 30, 2008 Ok, this looks like it may work... is this what you are proposing for index.php? First check content, if not there, check submit button, if not there, execute default page. <?php if (isset($_GET['content'])) { $content = $_GET['content']; $nextpage = $content . ".inc"; include($nextpage); } elseif (isset($_POST['submit'])) { switch ($_POST['submit']) { case 'Update': include(updatedata.inc); break; case 'Delete': include(deletedata.inc); break; } } elseif { include("default.inc"); } ?> Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-726183 Share on other sites More sharing options...
daalouw Posted December 30, 2008 Author Share Posted December 30, 2008 Unfortunately I have several screens with DELETE and UPDATE options, so non of the solutions discussed here will work for me, but that is because of my specific scenario. I will just havwe to create separate UPDATE and DELETE forms for each funtion. Sucks!! I'm going to mark this topic as complete though. Sincerely. Deon Link to comment https://forums.phpfreaks.com/topic/138834-solved-need-2-buttons-on-a-form/#findComment-726613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.