Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: multi submit form href links/buttons

  1. #1

    multi submit form href links/buttons

    Hi,

    Ok, I have a form which is displaying client confirmation information. I have two buttons; one to accept, and the other to decline.

    When using traditional buttons, I can just include a "name", and have my php do the rest.

    For example:
    Code:
    <input type="submit" name="accept" value="Accept">
    <input type="submit" name="decline" value="Decline">
    The problem I'm having is that I don't want to use a button. I want to use a href hyperlink. So naturally I tried to do it like this:

    Code:
    <a name="accept" href="javascript:confirmForm.submit()"> Accept </a>
    <a name="decline" href="javascript:confirmForm.submit()">Decline</a>
    But it's just not working. How can I send that "name" variable into my submit function so that my action php file can pick it up?

    I hope that makes sense, and that this thread is in the correct place.

    Thanks,

    Mark

  2. #2
    Use a hidden field:
    HTML Code:
    <input type='hidden' name='dothis'>
    <a href="javascript:document.confirmForm.dothis.value='a'; document.confirmForm.submit()"> Accept </a>
    <a href="javascript:document.confirmForm.dothis.value='d'; document.confirmForm.submit()">Decline</a>
    There are some other examples here:
    Switch the action field in an HTML form

  3. #3
    Hey, thanks for that.

    I found two different solutions in the end. I'll explain what I did just in case someone else has the same issues in the future.

    If I wanted to used the $_GET method I would do this:

    I'd create two href's like so:
    Code:
    <a href="javascript:submitForm('accept')">Accept</a>
    <a href="javascript:submitForm('decline')">Decline</a>
    This calls a function called "submitForm", with a value passed into it. The function looks something like this:

    Code:
    function submitForm(action) {
    	if (action == 'accept'){
    		document.confirmForm.action = "sendemail.php?emailcmd=accept";	
    	} else {
    		document.confirmForm.action = "sendemail.php?emailcmd=decline";	
    	}
     	document.confirmForm.submit();
    };
    All this function is doing, is adjusting the action of the form. The problem is you have to hardcode the parameters into the url.

    So I came up with a $_POST method too:

    Like prasanthmj suggested, I used a hidden field. My method here is probably more complicated than it needs to be, but I figured I'd share the tail anyway.

    So next, I added the hidden field anywhere within the form (my form name was "confirmForm"):

    Code:
    <input type="hidden" name="emailcmd">
    Then I put the href's in. The href's I added are the same as shown above. It's the submitForm that was a little different:

    Code:
    function submitForm(action) {
        	document.confirmForm.emailcmd.value = action;
        	document.confirmForm.submit();
    }
    This changed the hidden fields value to the appropriate button.

    And that done the trick.

    I've only started learning javascript last week, and php yesterday. Something I found quite frustrating as a beginner was that I could find solutions easily, but just didn't know how to actually use the solution. So to my fellow noobs this is what I did in the php action file:

    Code:
    // This gets the "emailcmd" parameter from the url.
    if (isset($_GET['emailcmd'])) {
    	$emailcmd = $_GET['emailcmd'];
    };
    
    // This gets the "emailcmd" parameter from the forms post.
    if (isset($_POST['emailcmd'])) {
    	$emailcmd = $_POST['emailcmd'];
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012