Log in

View Full Version : form submitting to 2 actions depending on radio button select



Irishcash33
01-25-2011, 09:18 PM
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

djr33
01-25-2011, 10:17 PM
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.

fastsol1
01-26-2011, 12:07 AM
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.

Beverleyh
01-26-2011, 07:05 AM
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

Beverleyh
01-26-2011, 09:36 AM
I think something like this might work;


<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-3217128/JQuery-Perform-Action-Checkbox-One-Checked )

prasanthmj
01-26-2011, 03:53 PM
The page below will be useful
Switching the form action field dynamically (http://www.javascript-coder.com/html-form/html-form-action.phtml)

djr33
01-26-2011, 04:20 PM
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.

Irishcash33
01-26-2011, 06:54 PM
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

<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>




<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