Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two Forms On Same Page With Same Action Submitting Each Other

Status
Not open for further replies.

MYQUE

Technical User
May 22, 2002
25
0
0
GB

Hi, thanks for checking out my post...

I have managed to get two freely available scripts running on my site. One is a poll script the other is a tell-a-friend script. Both scripts use forms and both currently work. However, I have run in to an issue I am unable to solve...

The problem is that whenever the poll script is submitted with a vote the tell-a-friend script becomes submitted as well. So they are both submitting at the same time even though they are completely separate forms. This becomes an issue because when someone submits a vote on the poll they do not enter information in to the other script, the tell-a-friend script, and so because the tell-a-friend script gets submitted empty all the red error messages (no email, no names, etc.) appear for the tell-a-friend script which just looks odd.

I'm not that knowledgeable with html forms but I've noticed both forms have their 'action' attribute pointing to the current webpage url. i.e on the homepage of the site both scripts form actions point to 'index.php'. Is this where the issue lies that is causing both forms to submit at the same time?

Is it possible to get around this issue?

A big thank you in advance for any help with this. :)

Mike
 
You can have two forms submitting to the same server side script - usually they would each have a hidden field that tells the script which action to take.

I would make sure the forms have different names and check that the submit button are correctly placed...

Code:
<form name="tell_friends" action="index.php" method="post">
  <input type="hidden" name="to_do" value="tell_friends" />
  <!-- some form elements here ... -->
  <input type="submit" value="Tell Friends" />
</form>

<form name="poll_form" action="index.php" method="post">
  <input type="hidden" name="to_do" value="poll" />
  <!-- some form elements here ... -->
  <input type="submit" value="Tell Friends" />
</form>

Regards
 
... woops! The latter submit should have the value "poll"... sorry :)
 

Thanks for the super quick reply dkdude :)

I'll try it out and post back how it goes.

Thanks.
 
Hi again dkdude, I've just tried the fix you posted but it doesn't appear to have worked.

If it helps have a look at the page code at the site:
The poll and tell-a-friend scripts show on the right hand menu one above the other.

Try submitting a vote to the poll whilst leaving the tell-a-friend form empty and you will see the problem that is arising.

Is there anything else that can be tried?

Thanks again for your help.
 
My suggestion about adding a hidden field was just to explain how it is possible. So just adding the hiddin fields in my example won't fix the problem...

To tell what goes wrong I need to see your server side script (ie. index.php).

Regards


Jakob
 
... having said that, the question should be moved to forum434 as it is a PHP issue now :)
 
Thanks for your help Jakob, I'll move over to the php forum with this. :)
 
I know this has moved but there is no problem at all having two forms on the same page submitting to the same script page. You should make sure that the forms have different names and if you decide to use the hidden feild then all you have to do it do a check to see which form has been submitted and then only run the php code for that form.
 
It seems like the action= portion of the two forms should be different. The above suggestions assume that you using index.php for processing commands, and it will then dynamically include whatever appropriate scripts based on the contents of the "to_do" field, but I can tell from browing your site that you are not using index.php in this manner, so it seems that your two forms should simply submit directly to the appropriate php script on the server. Beyond that, you may want to consider modifying the <form> tag to use target="_blank" or something similar (hint: intelligent sizing) to pop up a new window to the script and then have it finish by displaying a thank you message such as "thank you for contributing to the poll" etc..
 
since you haven;'t been to the PHP forum, i'll like you to it.

The PHP forum right here on Tek-tips is : forum434

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi guys,

Thanks for all the help on this. I've managed to get the forms working happily together now using the hidden input fields and a little PHP code surrounding each PHP script.

Cheers again for the help :)

Mike
 
I'm facing the same problem here and was wondering if you could help a newbie out and post some code...?

Thanks in advance!
 
2 things, post some code, and i think you shopuld create a new thread for it. IF it is in PHP then you should post in the PHP forum.

But as a simple reference it might be smething like this:

Code:
[green]// in PHP:[/green]
<?
if(isset($_POST['submit1'])){ [green]//this checks to see wether the first form was submitted.[/green]

[blue]do_ something_with _the_first_form's _Information[/blue]

}
if(isset($_POST['submit2'])){ [green]//this checks to see wether the second form was submitted.[/green]

[blue]do_ something_with _the_second_form's _Information[/blue]

}

else{
[green]//If no form was submitted then close the PHP tags and just display the form.[/green]
?>

<form name=form1 action=mypage.php method=post>
<input type=text name=text_from_form1>
<input type=submit value="Send" name="submit1">
</form>

<form name=form2 action=mypage.php method=post>
<input type=text name=text_from_form2>
<input type=submit value="Send" name="submit2">
</form>


<?
} [green]//close the curly brace from the else statement.[/green]

?>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top