Jump to content

2 buttons 1 form .. kinda


severndigital

Recommended Posts

ok .. so, i have this form and it has two buttons i'll strip down the code to make it easier to read.

 

<form name="myform" action="index.php?view=main&action=update" method="post">
//the form is populated with varible information from a php function
$showForm = formContents();

//first button which will basically submit the form
<input type="submit" name="Update" id="Update" value="Update Quantities" />

//second button does some javascript checks and then send the user to a new page
<input type="button" name="checkout" id="checkout" value="Proceed To Checkout" onclick="checkTheBox(); return false;" />
</form>
//here is the javascript associated with the second button
<script>
function checkTheBox() {
if (document.myform.MaxQtyApproval && !document.myform.MaxQtyApproval.checked){
	alert("Please approve or change quantity");
}
else {
	window.location.href='index.php?view=main&action=finalize';
}
}
</script>

 

This work fine, however I need the second button to "SUBMIT" the form to the new location, not just take the user there.

 

 

Any ideas or places i can learn what to do would be appreciated.

 

Thanks,

-C

 

 

Link to comment
https://forums.phpfreaks.com/topic/122861-2-buttons-1-form-kinda/
Share on other sites

make the second button a submit button too (so your form has 2 submit buttons)

 

then in the php file the form is submitted too put some loging to work out what it should be doing:

 

if(isset($_POST['update']))

{

  //do somthing

}

 

if(isset($_POST['checkout']))

{

  //do somthing else

}

 

is this what you mean?

 

If you want 2 buttons, each sumitting to separate php files then you will need to have 2 separate forms on your page

I think the better way to do this would be submitting the forms via Javascript.

 

Write a javascript function that'll change the action attribute of the form, and then submit it. Give both 'submit' (you're going to want to use type="button" rather than type="submit" ) buttons an onmouseup event referencing that function with an argument telling it if it's button a or button b.

If you're living in 1995 maybe. Many large-scale websites rely heavily on javascript.

 

Not only that, but you're relying on a client side effect... some versions of Internet Explorer ( I'm not sure if 7 does this ) will not include the 'Submit' button in the POST query if you hit 'Enter' to submit the form. That's why it's recommended to check the size of the post variable, or specific inputs rather than the button used to submit the form.

 

This brings another problem, what happens when a user hits the 'Enter' button with multiple submit buttons? A javascript snippet would be the ideal solution to this... especially because it allows you to not HAVE to use submit buttons.

 

Just because a 'server-side only' solution is available, it doesn't mean it's the best. From what it looks like he already has Javascript implemented and required for his form.

Javascript is overkill for this problem...

 

plenty of people have javascript disabled anyway, lots of corporate environments force it to be disabled for security reasons. All the big sites that rely on javascript usually provide a html only version for this reason.

 

And in any case, this is the php forum.

 

I guess a few lines of javascript is overkill :-\ If this site's primary visitors are going to be connecting through a corporate computer, then you have a very targeted market. If this is the case, you may want to look at their browser policies before making the decision.

 

If you want to bend over backwards to those left in the stone age, generate the scripted buttons using javascript and include the regular ones in a <noscript> element. Have your default action point to a PHP file that can interpret these results and include your action pages ( would only be a couple lines of code )

 

That way you have the scripted solution for the ~95% of people with JS enabled, and a safer solution for those with their heads in the sand :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.