Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Oct 2013
    Location
    Bangalore, Karnataka
    Posts
    43
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Html from function

    How can I use two or more Submit buttons in the same form?

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,779
    Thanks
    6
    Thanked 1,022 Times in 995 Posts
    By just putting two or more submit buttons into the same form.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,448
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Just in case OP meant he wanted to submit a form to one of two pages:

    Code:
    <form action="First.php" method="post" id="my_form">
    
    <!--GET YOUR INFO HERE-->
    
    
    <input type="button" onclick="formsubmit('first.php')" value="Submit to First" />
    <input type="button" onclick="formsubmit('second.php')" value="Submit to Second" />
    </form>
    
    <script>
        function formsubmit(destination)
        {
            document.getElementById('my_form').action = destination;
            document.getElementById('my_form').submit();
        }
    </script>
    Last edited by sunfighter; 10-17-2013 at 06:19 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,779
    Thanks
    6
    Thanked 1,022 Times in 995 Posts
    No, that’s not a proper solution. Of course, multiple submit buttons should do different things but then, as with a single submit button, it’s the task of the server-side script to determine what to do. In PHP you can just check which button was clicked (if(isset['button_name']) {…}) and then do a header('Location: …') redirect.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,448
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Why is this not 'proper'? I ask cause I have used that before.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,187
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Quote Originally Posted by sudhakar1 View Post
    How can I use two or more Submit buttons in the same form?
    Why not just add a checkbox to handle the alternative processing ?
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #7
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,779
    Thanks
    6
    Thanked 1,022 Times in 995 Posts
    Quote Originally Posted by sunfighter View Post
    Why is this not 'proper'? I ask cause I have used that before.
    Because form submission with JavaScript is … well … let me say it’s “suboptimal”. Submit buttons submit a form to a server-side script. So why use (unreliable) JS at all if the server-side script can already handle this? This is not even an enhancement, it’s just a long winded detour (that’s unreliable, as I stated).

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,448
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Doesn't that kinda say no to JS validation which will submit the form if it validates?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by sunfighter View Post
    Doesn't that kinda say no to JS validation which will submit the form if it validates?
    Validating with JavaScript is entirely different - that's there entirely for the convenience of the person filling out the form. You still have to validate it again on the server since JavaScript may not be available.

    Sunfighters code will fail completely for anyone with JavaScript disabled as the form doesn't have ANY submit buttons.


    The way I handle multiple submit buttons on the one form is to give them all the same name but different values. The server side script the form is passed to can then check which value was passed with the form in order to determine what to do with the form. If none of the submit buttons have the focus when the enter key is pressed to submit the form then the last submit button's value will be passed so that needs to be the most common choice.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,779
    Thanks
    6
    Thanked 1,022 Times in 995 Posts
    Also, by default a form submission triggers a page reload. So the only advantage of JS in that process is that one can submit the form without page reload through AJAX. However, this brings a totally new layer of complexity to the form in terms of validation, error handling, and browsing history entry, which is totally over the top if it’s just about submitting a form to redirect to different pages by clicking different submit buttons. All this can be done on the server (and it’s safer and more reliable) so there is just no need to do it with JS.

  • Users who have thanked VIPStephan for this post:

    sudhakar1 (10-18-2013)

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,448
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Haha felgall, good one. Anyone that disables JavaScript today won't be able to do anything on the internet(slight exaggeration). No one, with few exceptions, would do that.

    Your idea and VIPStephan's is good. Use a server side script to get the passed value of the button. I agree with it 95%. I still like mine(cause it's mine), because you do evaluate the form before submission and most scripts that do submit the form with JS. So why not just re-write the form action? And those five people that surf with JS turned off will shake their first to the sky, say some magic words under their breath, and turn it on. 'Cause they know.

    OK. You got here first. Just leaving this for a laugh.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #12
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,779
    Thanks
    6
    Thanked 1,022 Times in 995 Posts
    I don’t understand why people show such an arrogant attitude? The point is not that people who have no JS are stupid and deserve a bad experience, the point is that by showing such an attitude you are not contributing to making the web (or the world) any better, you just add more clutter because you think you can. And if I may be that direct: Your statement that scripts that evaluate the form with JS before submission also submit with JS is plain wrong. First of all: Any real professional developer will always do server side validation first and only do JS validation for the convenience of the end user. Secondly: There is no causal relationship between JS validation and JS submission. If a JS validation fails the form submission can be cancelled but if it validates the submission can still be done with the default behavior because JS doesn’t have to do anything in that case. Simple example:
    Code:
    if(form_data_valid === false) {
      return false; // prevent form submission
    }
    // no else statement required because if not false (i. e. form data is valid) the form is submitted as it is (without JS)
    JS form validation is and can always just be an enhancement of the user experience. It’s not so much a matter of people “accidentally” having JS disabled or whatever, it’s also a matter of security. If you relied upon JS form validation smart people would be able to modify or circumvent it and might inject bad scripts into your form, and boom, you’ve got malware.

    Also, by relying on JS for crucial functionality you are effectively counteracting accessibility and usability. Or are you showing the same attitude towards people that are handicapped in some way and therefore have a different way to access the information on the internet? “If they use a program other than the latest common web browser then they deserve a non-working website.”?
    Last edited by VIPStephan; 10-18-2013 at 02:13 PM. Reason: typo

  • #13
    Regular Coder Nerevarine's Avatar
    Join Date
    Jan 2013
    Location
    Phendrana Drifts, Tallon IV, W-Class
    Posts
    285
    Thanks
    0
    Thanked 17 Times in 17 Posts
    I'm just laughing at the fact that this entire discussion stemmed from some dude asking if he could put two buttons in one form.
    Time kills us in our sleep and we watch it happen in our dreams. -K.K.
    THE END-ALL PROGRAMMING REFERENCE: CLICK HERE (Courtesy of Major Payne)
    My username was previously L0adOpt1c. :: Please read this before posting in the HTML/CSS forum.
    Validate your HTML here and your CSS here. :: Need basic HTML/CSS tutorials? Click here, don't post about it.

  • #14
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,448
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Stephan, I never said
    .....that people who have no JS are stupid and deserve a bad experience
    Yahoo says 2.06% of Americans have JS off. Anyone that does this knows what the internet is like and has made a conscious decision to do this. It's a bear to turn off JS in chrome or FF, so it's not by a misplaced click that this happens. The 2.0 web is brought to you compliments of JS, ajax, and their ilk. Can you view a blog or write a comment on one? Can you shop and add items to your check out cart? Can you pay by paypal? Without having JS active?

    IE6 has 4.9% users world wide and .29% in the US. IE7 has less then 1%. IE8 has 10.3%. Why would you waste your time making IE6/7 look like IE10? When will you stop doing that for IE8? Why would you berate the person that does not program a website for IE6 users?

    Decisions have to be made. If someone using IE8 can not see my beautiful rounded corners or stylish border on my pull quote; is it a crime? Do I have to waist my time making them see what others see? I don't think so.(Yes I know there is an app for that) If you program using notepad does MicroSoft have to make it so you can collapse paragraphs or color code the php portions? So I say again I don't program for people that have JS turned off. They made that decision, they are happy with it and so am I.

    The point of this thread is/was...
    Two solutions to a question were given one in javascript and one in php. One programming language is NOT superior to the other. Although a few people have tried to make that point, it just don't exist. I can see why some people think it is:
    JS form validation is and can always just be an enhancement of the user experience.
    It looks to me that your idea of JS is something to make the web page pretty. That is wrong. JS is a powerful language. It doesn't need a web page nor a internet connection to present powerful applications. It is as good as any other programming language out there. I do not see where doing this in php is better then using js to do it.

    are you showing the same attitude towards people that are handicapped in some way and therefore have a different way to access the information on the internet?
    Stephan, Stop, stop, just stop. You can't put me or anybody else into an inferior box and then berate us. I re-read your post because I must have missed the part where you accuse me of molesting little boys and selling my wife into slavery, but I did find where you said I wasn't a real professional developer. I do not kick people in wheelchairs over. Why would talking about JS have anything to do with this.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    I don't know how anyone can make any meaningful estimate of what percentage of people have JavaScript turned off since it can be set at the site level. There are quite a few sites where the JavaScript is broken where I have JavaScript turned off simply because I can't use the site with it on. There are also lots of sites where I have JavaScript turned on.

    Also even if 2% of people have JavaScript turned off then a significant fraction of that 2% will be people with disabilities who need JavaScript off in order to be able to use the web at all. If an essential site were to fail to work for them because it requires JavaScript then they might very well get a multi million settlement in court for being discriminated against.

    There is a significant difference between ordinary web pages and a web application. Ordinary web pages should work without JavaScript and JavaScript should be part of the progressive enhancement. With web applications there may be no alternative to using JavaScript and so it might be reasonable to expect that anyone wanting to use the application will at least have it on for that site. Of course then there are the web applications where the JavaScript is broken where it is impossible for some groups of people to use the application regardless of whether they have JavaScript on or off.

    There are lots of badly written scripts out there that will only work for some visitors and where for many people the page will work better if you turn JavaScript off for that site.

    To return to the original question about multiple submit buttons in a form. It would be almost certain that the intended use of the form would be in an ordinary web page where the form should be expected to work without JavaScript. Forms intended to work only with JavaScript are more likely to have zero buttons than they are to have two or more - with the JavaScript submitting the form once it is filled out without needing any button presses.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •