Jump to content

Getting around the nested form police...


Recommended Posts

Hi all,

 

Okay, I know you can't nest forms within each other.  However, I need to understand how other pages get around the problem on my checkout page.  It's more visual than anything, so here's a screenshot:

 

checkout_form.jpg

 

My <forms> are marked in red.  The yellow boxes represent <tables>.

 

Form 1 contains some info in that textbox that I need to post to the next page.  The button that posts the form is wayyyyy down at the bottom of the page.  But in between, I need another small form to accomplish the adding of a gift certificate (form 2).  Where do I put the <form> tags to get around this?  Do I really have to move my textbox to the bottom of the page (or swap those forms left/right so I'm not overlapping).  How do other pages get past this?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/80680-getting-around-the-nested-form-police/
Share on other sites

I would just eliminate form2 and use a button with a onclick event to add the gift certificate value to a hidden field (use JavaScript in the onclick event of the button - I assume you are for your gift certificate).

 

example:

 

<script language="javascript">

function agv(amount)
{
document.getElementById('gca').value = document.getElementById(amount).value;
}

function add()
{
document.getElementById('giftrequest').value = document.getElementById('gca').value;
}

</script>

<form name="form1" action="whatever.php" method="post">
<input type="radio" id="gift1" value="$25" onclick="agv(this.id)" name="gift"> $25
<input type="radio" id="gift2" value="$50" onclick="agv(this.id)" name="gift"> $50
<input type="radio" id="gift3" value="$75" onclick="agv(this.id)" name="gift"> $75
<input type="radio" id="gift4" value="$100" onclick="agv(this.id)" name="gift"> $100
<input type="radio" id="gift5" value="$200" onclick="agv(this.id)" name="gift"> $200
<input type="hidden" id="gca">
<input type="hidden" id="giftrequest" name="giftcertificate">
<input type="button" value="Add to Order" onclick="add()">
</form>

 

Be sure you do not validate "gift" radio button value; it's just named for functionality purposes. The only field you should validate for the script above is "giftcertificate" hidden field value.

phpQuestioner's approach is not a bad one, but beware if your users do not have javascript enabled this will not work. What you might consider is just using the single form (as phpQuestioner mentioned) and then testing to see which button was clicked and then processing the form appropriately from there.

 


if( isset($_POST['update']) )
{
   //Process update cart code
}
else if( isset($_POST['checkout']) )
{
   //Process checkout code
}
else if( isset($_POST['add_to_order']) )
{
   //Process add to order code
}
else
{
   //Error
}

 

Of course if those are image buttons you'll have to do it a little differently, but that's the main idea.

dbo is correct - your viewers will have to have JavaScript enabled on their browser for my idea to work, but a vast majority or most internet users have JavaScript enabled on their browsers. if you think that maybe be a problem; that it might exclude a small amount of internet users - require the user to have JavaScript enabled on there browser or rediect them to an error page; that tells them they must have JavaScript enabled on their browser to view your website/web page.

 

example:

 

<noscript>
<meta http-equiv="refresh" content="1;error-page.html">
</noscript>

doing it with a single form would require you to put in an extra check to see which type of data has been sent and what to do with it - having 2 forms also means it is easier to direct the information to the correct script...

 

keep each form that requires different processing separate

 

SEPARATION HELPS MAINTENANCE...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.