Jump to content

Recommended Posts

How can I have 2 submit buttons that goes to a php file and executes a different action?

Here is the form:

<form name="newsubmit" method="post" action="addbill.php?action=point1">
<form name="newsubmit2" method="post" action="addbill.php?action=point2">

<input name="dx1" type="text" size="20" />
<input name="dx2" type="text" size="20" />
<input name="dx3" type="text" size="20" />
<input name="dx4" type="text" size="20" />

<input type="hidden" name ="id_incr" value="<?php echo $id_incr; ?>"/>
<input type="submit" value="Add and continue" onClick="return check('newsubmit', this.name)"/>
<input type="submit" value="Add and return to main view" onClick="return check('newsubmit2', this.name)"/>

 

Here I want to set up a switch to point to a differnt location depending on what button they pressed in the form

addbill.php:

 

snipped lines that update the database based on the POSTed form
mysql_query($query) or die(mysql_error());


switch ($_GET['action']) {
case "point1":
header("Location: editdos.php?action=edit&id=".$pt_id);

break;

case "point2":
header("Location: ../billview.php");
break;
}
?>

 

When I run this, point1 case is always executed, even if I press the second submit button.

What am I doing wrong?  Thanks.

Yeah you can't really have a form in a form like that

<form name="newsubmit" method="post" action="addbill.php">

<input name="dx1" type="text" size="20" />
<input name="dx2" type="text" size="20" />
<input name="dx3" type="text" size="20" />
<input name="dx4" type="text" size="20" />

<input type="hidden" name ="id_incr" value="<?php echo $id_incr; ?>"/>
<input type="submit" name="point1" value="Add and continue" onClick="return check('newsubmit', this.name)"/>
<input type="submit" name="point2" value="Add and return to main view" onClick="return check('newsubmit2', this.name)"/>

 

 

snipped lines that update the database based on the POSTed form
mysql_query($query) or die(mysql_error());


if(isset($_POST['point1']))
{
header("Location: editdos.php?action=edit&id=".$pt_id);
}
if(isset($_POST['point2']))
{
header("Location: ../billview.php");
}

?>

Of course you can have a form like that....

<?php
if($_GET['button1']=="cake") {
echo 'Button1 was clicked, run this code';
} else if ($_GET['button2']=="cheese") {
echo 'Button2 was pressed, run that code';
} else {
echo 'No button was pressed, you should press one so they don\'t feel unloved';
}
?>
<form method="GET" action="/test.php">
<button name="button1" value="cake">Button1</button>
<button name="button2" value="cheese">Button2</button>
</form>

Easy do.

 

Whichever button you click will send it's value to the action="" page for the form, all the others will not send their value to the action="" page.

I don't mean to undermine MadTechie or anything, but personally I believe it's better practice to use a non-javascript solution when one exists.

Doesn't undermine mine as it mine doesn't use JS....

the javascript was in his existing code and probably does some validation..

 

The reason i didn't use the submit button value is due to the fact the values were are used to prompt the user and chaging those values would mean your need to update the php code.. when the button name are not seen by the (general) user..

thus updating the interface won't effect the code..

 

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.