Results 1 to 8 of 8

Thread: form submitting to 2 actions depending on radio button select

  1. #1
    Join Date
    Jul 2008
    Location
    boston, ma
    Posts
    88
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default form submitting to 2 actions depending on radio button select

    Hi,
    I am working in php(wordpress). I am ok, but not brilliant at it. So I have a form with some fields, 3 radio buttons and one submit button. The main action will be submitting to wordpress db. The second action will be submitting to a url depending on which radio button is selected.
    Example:
    Select Choice A and hit submit. The form will go to wordpress db and choice a's url. Select Choice B and hit submit. The form will go to wordpress db and choice b's url.

    I am finding out this is easier said than done. I have spent way too long trying to figure this out. I am hoping it is just my inexperience with php!

    I found a js script that changes the action depending on which radio button is selected. It works great, but it only sends to one action. I need it to send to 2 actions.

    Any suggestions, thoughts, would be greatly appreciated.

    Thanks
    Will

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can use Javascript to dynamically set the action based on the value of the field. For example, if (this.value=='something') { theaction = ... } (not real code, just an example). Alternatively you can do the same thing using PHP, but it must be a redirect that occurs after the page is submitting to the same page. Accordingly, the form data will not be re-sent in this redirect, so if you need to submit the data to multiple locations that won't be much help.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    you could use jquery also or possibly ajax. I am no pro at either but I know the jquery will do it just like javascript.

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Just to clarify, jQuery is javascript. Its a predefined library of javascript events and effects that makes writing your own functions much easier than doing it all from scratch. JQuery also includes its own set of Ajax routines
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I think something like this might work;

    Code:
    <html> 
    <head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript"> 
            $(document).ready(function()
    		{ 
    		$('#submit').click(function() 
    			{
        			var checked = $("input:checkbox:checked");
        			if (checked.length === 1 && checked[0].id === 'A') 
    				{
                   			 alert('This will submit to Google'); 
                    		$('#my_form').attr('action', 'http://www.google.com/'); 
        				}  
        			else if (checked.length === 1 && checked[0].id === 'B') 
    				{
                    		alert('This will submit to eBay'); 
                    		$('#my_form').attr('action', 'http://www.ebay.com/'); 
        				}
    			else
    				{
                    		alert('This will submit to FaceBook'); 
                    		$('#my_form').attr('action', 'http://www.facebook.com/'); 
    				}
    		});
            }); 
    </script> 
    </head> 
    <body> 
    <form id="my_form" name="my_form" action=""> 
    	<input type="checkbox" id="A" />
    	<input type="checkbox" id="B" />
    	<input type="button" value="Click Here" id="submit"/> 
    </form> 
    </body> 
    </html>

    ( adapted from: http://efreedom.com/Question/1-32171...ox-One-Checked )
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. #6
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    The page below will be useful
    Switching the form action field dynamically

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The main problem with all of this though is that it relies on Javascript. If it's disabled or unavailable, the page will submit to the original action. Is this a necessary feature? Do you have a backup?
    Considering the complexity of what you're suggesting, it might be worth rethinking the whole system if that is possible.

    If not, using Ajax to submit the form dynamically and never actually reloading the page might be a good option.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jul 2008
    Location
    boston, ma
    Posts
    88
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    Thank you all for your replies. I got it working with js!! But my concerns are the same as djr33's. What if they have js disabled, then the form is useless.
    This is the code that works
    PHP Code:
    <form name="registerform" id="registerform"  onsubmit="multiSubmit(this, '<?php echo site_url('wp-login.php?action=register''login_post'?>');" onClick="return ActionDeterminator();" method="post">
    <input type="hidden" name="check_submit" value="1" />

        <p>
            <label><?php _e('Username'?><br />
            <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" tabindex="1" /></label>
        </p>
        <p>
            <label><?php _e('E-mail'?><br />
            <input type="text" name="user_email" id="user_email" class="input" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" tabindex="2" /></label>
        </p>
        <p>
        <input type="radio" name="reason" ><label>Banking</label><br>
        </p>
        <p>
        <input type="radio" name="reason" ><label>Securities</label><br>
        </p>
        <p>
    <input type="radio" name="reason" ><label>Insurance</label><br>
    </p>
    <?php do_action('register_form'); ?>
        <p id="reg_passmail"><?php _e('A password will be e-mailed to you.'?></p>
        <br class="clear" />
        <input type="hidden" name="redirect_to" value="<?php echo esc_attr$redirect_to ); ?>" />
        
        <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Register'); ?>" tabindex="100" /></p>
    </form>
    Code:
    <script type="text/javascript">
    var secondActionStore = new Array();
    var secondActionForm = new Array();
    
    function multiSubmit(whichForm, secondAction){
        if(navigator.userAgent.indexOf('Opera') >= 0)
            window.open('about:blank', 'new_window', 'location=yes');
        else
            window.open('about:blank', 'new_window');
        whichForm.target = 'new_window';
        var i = secondActionStore.length;
        secondActionStore[i] = secondAction;
        secondActionForm[i] = whichForm;
        setTimeout('secondActionActivate('+i+')', 1);
    }
    
    function secondActionActivate(i){
        var whichForm = secondActionForm[i];
        var secondAction = secondActionStore[i];
        whichForm.target = '_self';
        var firstAction = whichForm.action;
        whichForm.action = secondAction;
        whichForm.submit();
        whichForm.action = firstAction;
    }
    </script>
    <script type="text/javascript" language="JavaScript">
    
    function ActionDeterminator()
    {
    if(document.registerform.reason[0].checked == true) {
       document.registerform.action = 'http://www.hotmail.com';
       
       }
    if(document.registerform.reason[1].checked == true) {
       document.registerform.action = 'http://www.yahoo.com';
    
       }
    if(document.registerform.reason[2].checked == true) {
       document.registerform.action = 'www.google.com';
       }
    return true;
    }
    // -->
    </script>
    The other issue is, is it secure to have the data pushed through the url of the browser? When I submit, for a split second I can see the form info in the url bar on the browser, then it goes to the thank you page.

    Thanks
    Will

Bookmarks

Posting Permissions

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