toolman Posted June 23, 2015 Share Posted June 23, 2015 Hi, I have the following form on my page which works fine. if($_POST){ $to = 'me@me.com'; $subject = 'Signup'; $from_name = $_POST['name']; $company = $_POST['company']; $from_email = $_POST['email']; $telephone = $_POST['telephone']; $message = "New Signup Detials" . "\r\n\r\n" . "Name :" . $_POST['name'] . "\r\n\r\n" . "Email: " . $_POST['email'] . "\r\n\r\n" . "Telephone: " . $_POST['telephone']"; $robotest = $_POST['robotest']; if($robotest) $error = "You are a gutless robot."; else{ if($from_name && $from_email && $message){ $header = "From: $from_name <$from_email>"; if(mail($to, $subject, $message, $header)) $success = "You are human and your message was sent!"; else $error = "You are human but there was a problem sending the e-mail."; }else $error = "All fields are required."; } if($error) echo '<div class="msg error">'.$error.'</div>'; elseif($success) header ("location: contactthanks.php"); } I have also have another form on my page with the following: if($_POST){ $to_s = 'me@me.com'; $subject_s = 'Support Request'; $from_name_s = $_POST['name_s']; $company_s = $_POST['company_s']; $from_email_s = $_POST['email_s']; $telephone_s = $_POST['telephone_s']; $message_s = "New Signup Detials" . "\r\n\r\n" . "Name :" . $_POST['name_s'] . "\r\n\r\n" . "Email: " . $_POST['email_s'] . "\r\n\r\n" . "Telephone: " . $_POST['telephone_s'] . "\r\n\r\n" . "Comments: " . $_POST['message_s']; $robotest_s = $_POST['robotest_s']; if($robotest_s) $error = "You are a gutless robot."; else{ if($from_name_s && $from_email_s && $message_s){ $header_s = "From: $from_name_s <$from_email_s>"; if(mail($to_s, $subject_s, $message_s, $header_s)) $success = "You are human and your message was sent!"; else $error = "You are human but there was a problem sending the e-mail."; }else $error = "All fields are required."; } if($error) echo '<div class="msg error">'.$error.'</div>'; elseif($success) header ("location: contactthanks.php"); } However, I am getting a duplicate "All fields are required" message appear and the form is not submitting. Any ideas how can the code similar for each form? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/ Share on other sites More sharing options...
cyberRobot Posted June 23, 2015 Share Posted June 23, 2015 Have you checked to see if $from_name_s, $from_email_s, and $message_s contain expected values? Note that you can use var_dump() to check. For example $from_name_s = $_POST['name_s']; var_dump($from_name_s); If you're still unable to figure out the issue, it may help if you post the form code. Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/#findComment-1514711 Share on other sites More sharing options...
toolman Posted June 23, 2015 Author Share Posted June 23, 2015 They all seem to have values. This is form code: Form 1: <form method="post" action="" id="signup"> <fieldset> <label>Name:</label> <input name="name" type="text" id="name" /> <label>Company:</label> <input name="company" type="text" id="company" /> <label>E-mail:</label> <input name="email" type="text" id="email" /> <label>Telephone:</label> <input name="telephone" type="text" id="telephone" /> <!-- The following field is for robots only, invisible to humans: --> <p class="robotic" id="pot"> <label>If you're human leave this blank:</label> <input name="robotest" type="text" id="robotest" class="robotest" /> </p> <input type="submit" value="Send Message" class="submit" /> </fieldset> </form> Form 2: <form method="post" action=""> <fieldset> <label>Name:</label> <input name="name_s" type="text" id="name_s" /> <label>Company:</label> <input name="company_s" type="text" id="company_s" /> <label>E-mail:</label> <input name="email_s" type="text" id="email_s" /> <label>Telephone:</label> <input name="telephone_s" type="text" id="telephone_s" /> <label>Message:</label> <textarea name="message_s" id="message_s"></textarea> <!-- The following field is for robots only, invisible to humans: --> <p class="robotic" id="pot"> <label>If you're human leave this blank:</label> <input name="robotest_s" type="text" id="robotest_s" class="robotest" /> </p> <input type="submit" value="Send Message" class="submit" /> </fieldset> </form> Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/#findComment-1514712 Share on other sites More sharing options...
cyberRobot Posted June 23, 2015 Share Posted June 23, 2015 Ah, I wonder if the error is coming from the code that's supposed to process the first form. Try adding some debugging statements after the "All fields are required" string is saved to $error in both blocks of code. For example }else $error = "All fields are required."; echo '<div>form 1 submitted</div>'; And }else $error = "All fields are required."; echo '<div>form 2 submitted</div>'; As far as I can tell, the script that processes the form submissions doesn't check to see which form is submitted before executing the corresponding code. Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/#findComment-1514715 Share on other sites More sharing options...
mac_gyver Posted June 23, 2015 Share Posted June 23, 2015 (edited) each different form processing code needs to test for a unique field name in the form, such as a hidden field, so that only the form processing code that corresponds to the submitted form will run. // post method form processing code if($_SERVER['REQUEST_METHOD'] == 'POST'){ if(isset($_POST['a_field_that_uniquely_identifies_form_1'])){ // all the form processing code for form 1 } if(isset($_POST['a_field_that_uniquely_identifies_form_2'])){ // all the form processing code for form 2 } // any other form processing code // if there are no errors, you should do a header() redirect to the exact same url of this page so that the browser won't try to resubmit the form data // to pass any 'success'/'thank you' message to be displayed when the page gets displayed after this redirect, pass it/them in $_SESSION variables // you should also exit;/die; after any header redirect to prevent the remainder of the code on the page from running // if there are errors, don't redirect and let the code on this page display the error messages and redisplay the form } // end of post method form processing code // any get request - display code starts here... the above code should set error messages in an array - $errors['form1_name'][] = 'some message'; or $errors['form2_name'][] = 'some message'; at any point to detect if there are errors, you can just test if the $errors array is empty or not. to display the errors for any form, just test for and loop over the $errors['form1_name'] array elements. Edited June 23, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/#findComment-1514716 Share on other sites More sharing options...
fastsol Posted June 23, 2015 Share Posted June 23, 2015 In order to process multiple forms on a single page you need to tell php how to distinguish between the different postings. In your case it's pretty easy. 1. You need to give the submit buttons an actual name="" attribute and make them different for each form. 2. Don't ever just use if($_POST). $_POST is always present cause it's a php superglobal. So everytime the page is loaded, this code is run. 3. Use if(isset($_POST['submit-button-name-for-the-form-posting'])) Obviously change the "submit-button-name-for-the-form-posting" to the actual name of the submit button Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/#findComment-1514717 Share on other sites More sharing options...
toolman Posted June 23, 2015 Author Share Posted June 23, 2015 Many thanks for the help. It worked when I used the if(isset($_POST['submit-button-name-for-the-form-posting'])) method. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/#findComment-1514719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.