两种形式相互冲突

I have placed two forms on one page. Bothe forms work fine separately but when they are placed on one page at the same time they conflict with each other. Here are both forms:

Contact Form:

<form name="contactform" id="contactform" method="post" action="#targetAnchorPage2">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
 <td colspan="2" class="error">
 <?php
 require_once("contact_send.php");
 ?>
 </td>
</tr>
<?php
}
?>

<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input  type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input  type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr><td colspan="2"><label id="tworows" for="message" class="<?=$message_class?>">your message:</label></td></tr><tr><td colspan="2"><textarea  name="message" cols="22" rows="6" value="<?=$message_class?>"></textarea>
 </td></tr>

<tr>
 <td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">   
 </td>
</tr>

</table>
</form>

Subscribe Form:

<form name="subscribeform" id="subscribeform" method="post" action="#targetAnchorPage3">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
 <td colspan="2" class="error">
 <?php
 require_once("subscribe_send.php");
 ?>
 </td>
</tr>
<?php
}
?>

<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>

<tr>
 <td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">   
 </td>
</tr>

</table>
</form>

How can this be solved? Is it caused by the "required_once" command?

I am guessing that since you are showing the required files based on the same criteria isset($_POST['name']) and since both forms have the name field you end up showing the code in both requires regardless of which form is submitted. You should simply change the form field names on on of the forms such that they are different.

Both forms have the same action attribute, they both point back to the same page (note that the hash is not sent to the server). As they both have a field called name and you are checking for that, both actions get executed regardless of which form was sent in.

You can do either:

  • use different scripts / form processors (don't post back to the same page)
  • use a different check for each form, for example by adding a hidden input that will allow you to distinguish between the forms.