Jump to content


Photo

control logic for 2 submit buttons on the one form


  • Please log in to reply
9 replies to this topic

#1 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 14 March 2006 - 09:24 PM

Hi All,

I have a form with two submit buttons on it:

<form method="POST" action="' . $page . '">

....

<input class="formbuttons" type="submit" value="Add Another Row" name="submit">
<input type="submit" value="Next" class="formbuttons" name="submit2">
</form>

I want it so that when a user clicks the Add Another Row the page refreshes and a new row appears. I have this working through some help I received previously.

i.e.

if (isset($_POST['submit']) && $_POST['submit'] == "Add More People")
{
$num_persons++;
$page = $_SERVER['PHP_SELF'];
}

However, I also want to allow the user the other option that if the other submit is clicked the $page variable is set to health3.php and the form submits and goes to this page.

I tried:

if (isset($_POST['submit2']) && $_POST['submit2'] == "Next") $page ="health3.php";

but this goes to health3.php when either of the submit buttons are clicked.

Any help would be greatly appreaciated.

Kind Regards,

Fergal.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 09:37 PM

you've got to keep in mind that input, hidden, submit, textarea, select fields (with the possible addition of a couple others) are ALWAYS set if your form has been submitted. so, if you click on one submit button, they will both always return true from the isset() function. your best bet to do something like this is with javascript. there are a couple options:

1) use javascript to actually create your next input field instead of reloading the page

2) use javascript to change the action of the form before submission to match the button that was pressed

beyond this, you'll have to come up with something a bit more intricate than simply checking to see which button isset().

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 March 2006 - 10:28 PM

You don't need to resort to javascript to solve this. I use multiple submit buttons on many of my forms with no problems at all. The trick is to give all the submit buttons the same name.

Then you could use the "if" statements or "switch()" statements to control what is done.

Ken

#4 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 14 March 2006 - 10:51 PM

Hi Ken

Do you mean something like this:


Form:

<form method="POST" action="' . $page . '">

<input class="formbuttons" type="submit" value="Add Another Row" name="submit">
<input type="button" value="Next" class="formbuttons" name="submit">
</form>

When form is submitted:

if (isset($_POST['submit']) && $_POST['submit'] == "Add More People")
{
$page = $_SERVER['PHP_SELF'];
}

if (isset($_POST['submit']) && $_POST['submit'] == "Add More People")
{
$page = 'health3.php';
}

Many thanks,

Fergal.

#5 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 14 March 2006 - 11:46 PM

<input type="submit" name="submit">
<input type="submit" name="addrow">

<?php
if ($_POST["addrow"]) {
   //add row
} else if ($_POST["submit"] {
   // form submitted
}
?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#6 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 15 March 2006 - 12:07 AM

Thanks KBee

I did the following:

if (isset($_POST['addrow']) && $_POST['addrow'] == "Add Row")
{
$page = $_SERVER['PHP_SELF'];
}
else if (isset($_POST['submit']) && $_POST['submit'] == "Next")
{
$page = "health3.php";
}

<form method="POST" action="' . $page . '">

....


<input class="formbuttons" type="submit" value="Add Row"name="addrow">
<input type="submit" value="Next" class="formbuttons" name="submit">
</form>

But the submit buttons are not functioning as they should. When I click either button I go to health3.php

Thanks,

Fergal.

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 15 March 2006 - 12:17 AM

I would use javascript for this. You can create the table and form elements as you go along, then when the submit button is pressed, the page is sent on. Then you aren't constantly going to the server for, really, no reason.

#8 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 15 March 2006 - 12:20 AM

Thought about this i.e. create the second submit as a type button and call a javascript function that will window.location the new page.

However, I had 2 reservations:

1. I wanted to try do this without Javascript and

2. If I use the javascript method i.e window.location='health3.php' can I then use post on the health3 page to pull all info from submitted page

Thanks,

Fergal.

#9 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 15 March 2006 - 01:01 AM

Don't redirect anywhere to add the new table rows. Use js to create them when the button is pressed.

<script type="text/javascript">
    function addrow() {
        var t = document.getElementById('testtable');
        var newrow = document.createElement("tr");
        var newrtd = document.createElement("td");
        var newltd = document.createElement("td");
        
        newrtd.appendChild(document.createTextNode("test row 2"));
        newltd.appendChild(document.createTextNode("test row 2"));
        newrow.appendChild(newltd);
        newrow.appendChild(newrtd);
        
        t.appendChild(newrow);
    }
</script>

<form name="testform" id="testform">
<table id="testtable">
    <tr>
        <td>sample row</td>
        <td>sample row</td>
    </tr>
</table>
<input type="button" value="add row" onclick="addrow();">
</form>

Not exactly what your asking for, but you get the idea.

#10 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 15 March 2006 - 01:17 AM

Understand where you are coming from.

Soved the problem using Javascript but would still be interested to find out how I could have solved it using 2 buttons on the form through php only.

Any ideas - any one?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users