Jump to content

control logic for 2 submit buttons on the one form


fohanlon

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.
Link to comment
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
Link to comment
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
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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.
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.