Jump to content

[SOLVED] Need 2 buttons on a form


daalouw

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.