Jump to content

Archived

This topic is now archived and is closed to further replies.

fohanlon

control logic for 2 submit buttons on the one form

Recommended Posts

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]<input type="submit" name="submit">
<input type="submit" name="addrow">

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Don't redirect anywhere to add the new table rows. Use js to create them when the button is pressed.

[code]<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>
[/code]

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

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.