?
Solved

multiple submit buttons in one form

Posted on 2006-05-26
5
Medium Priority
?
2,715 Views
Last Modified: 2012-08-13
I would like to have a form that would have two submit buttons but I have no idea how to do that.

basically I have a form that a user fills out and i want them to have an option to "preview" their entry or "post" their entry.

so it would require two "actions" but I don't know if that's possible.

<form action="/post.php" method="post">
<input type="text" name="title" />
<textarea name="body"></textarea>
<input type="submit" value="preview" />       # i want this submit button to go to preview.php instead of post.php
<input type="submit" value="submit" />         # i want this submit button to go to post.php
</form>

HELP!

Thanks.
0
Comment
Question by:mstdinc
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:jalalmegadeth
ID: 16774253
Hi

I don'T think that there's something called preview, but here's my suggestion


the 2 buttons will be submit buttons, but the action will be different

1st one : preview : action preview.html  
   here you can post your values so the user checks what's going on with the form, with two buttons : Submit or BACK
   if the user submits the form, then the action is gonna be post.php
   if the user wants to edit something, then he clicks the button BACK
the user should be able to get back the values that he entered before, and that's easy to do



that's an idea, probably you can get a better one ;)
good luck
0
 
LVL 5

Expert Comment

by:jalalmegadeth
ID: 16774262
since it's PHP

here's what you can do too :


 if(isset($_POST['submit']))
{
    if$_POST['submit'] == 'Submit')
    {
        /* submit code */
    }
    elseif($_POST['submit'] == 'Preview')
    {
        /* preview code */
    }
    else
    {
        echo "invalid function";
    }
}
0
 
LVL 7

Expert Comment

by:bubbledragon
ID: 16774959
Try script language?
You can use onClick event to change the action location before submit.

<form action="#" method="post">
<input type="text" name="title" />
<textarea name="body"></textarea>
<input type="submit" value="preview" onClick="this.form.action='/preview.php'"/>       # i want this submit button to go to preview.php instead of post.php
<input type="submit" value="submit" onClick="this.form.action='/post.php'" />         # i want this submit button to go to post.php
</form>
0
 
LVL 28

Accepted Solution

by:
gamebits earned 2000 total points
ID: 16775081
In the form you name your 2 buttons the same but with a different value

<input type="submit" name="process" value="Preview">
<input type="submit" nmae="process value="Post">

In your post.php script you use a switch statement to know what to do with the submited form.

switch ($process)
                {  
                      case "Preview":

                               Here you put your preview script

                       break;
         
                      case "Post":

                              Here you put your post script

                       break;
                }

Gamebits
0
 
LVL 28

Expert Comment

by:gamebits
ID: 16775726
Thanks for the points and the grade.

Gamebits
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question