简体   繁体   中英

PHP form action is redirecting to wrong page

I'm currently creating a PHP mail form for a website. The file is called "contactus.php". I need the page to be redirected to "thankyou.php", upon clicking the 'submit' button at the end of the form.

Currently it just takes me to a page called "contactus.php/contactus.php", and is completely blank save for the message, "Email sent successfully."

I see where it says "Email sent successfully" in the code work, but I'm confused how to change that, and how to change the form action in general, so that the email will still send, but the page will be directed instead to the page "thankyou.php", that I have created. I've tried just simply replacing the form action with "'thankyou.php'", but while it directed the page to "thankyou.php", the email no longer sent.

Here is the PHP at the top of the page:

<?php

require_once("./include/fgcontactform.php");
$formproc = new FGContactForm();


// config
$emailAddresses = array(
  ''=>'',
   'Service Department'=>'fakeemail1.com',
   'Sales Department'=>'fakeemail2.com',
   'Parts Department'=>'fakeemail3.com',
   'Customer Service Department'=>'fakeemail4.com',
   'Bids Department'=>'fakeemail5.com'
// etc etc
);
$emailSubject = 'Submitted from Online Form';

// If we are dealing with a form submission, send an email
   if (isset($_POST['name'])) {

// Check the email selected is valid
   if (!isset($emailAddresses[$_POST['destemail']])) {
   exit("Sorry, you have selected an invalid email option.");
}
// Create the body of the email message
      $emailBody = "Dear {$_POST['destemail']}, \n\n {$_POST['message']} \n\n 
      From: {$_POST['name']} \n Company: {$_POST['company']}  \n 
      Phone Number:   {$_POST['phone']} \n E-mail: {$_POST['email']}  
      \n Preferred method of contact: {$_POST['method']} \n\n Submitted  
      from Online 'Contact Us' Form";
//    Send the email and report the result

      if (mail($emailAddresses[$_POST['destemail']],$emailSubject,$emailBody,"From:  
      {$_POST['email']}")) {exit("Email sent successfully.");
      } else {exit("Email sending failed");
   }
 }

 // Output the html form

 ?>

And here is the form PHP:

<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id='contact_form_errorloc' class='err'></div>




<!-- Form Code Start -->
<form id='contactus' action='<?php echo $formproc->GetSelfScript(); echo    
htmlentities($_SERVER['PHP_SELF']);  ?>'  method='post' accept-charset='UTF-8'>

<fieldset >

<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php   
echo $formproc->GetFormIDInputValue(); ?>'/>


<div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>

<div class='container'>
   <label for='name' >Your Full Name*: </label><br/>
   <input type='text' name='name' id='name' value='<?php echo $formproc-
   >SafeDisplay('name') ?>' maxlength="50" /><br/>
   <span id='contactus_name_errorloc' class='error'></span>
</div>

<div class='container'>
   <label for='email' >Email Address*:</label><br/>
   <input type='text' name='email' id='email' value='<?php echo $formproc-
   >SafeDisplay('email') ?>' maxlength="50" /><br/>
   <span id='contactus_email_errorloc' class='error'></span>
</div>

<div class='container'>
   <label for='phone' >Phone*:</label><br/>
   <input type='text' name='phone' id='phone' value='<?php echo $formproc-  
   >SafeDisplay('phone') ?>' maxlength="50" /><br/>
   <span id='contactus_phone_errorloc' class='error'></span>
</div>

<div class='container'>
   <label for='company' >Company Name*:</label><br/>
   <input type='text' name='company' id='company' value='<?php echo $formproc-  
   >SafeDisplay('company') ?>' maxlength="50" /><br/>
   <span id='contactus_company_errorloc' class='error'></span>
</div>

<div class='container'>
   <label for='message' >Message*:</label><br/>
   <textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc-  
   >SafeDisplay('message') ?></textarea>
   <span id='contactus_message_errorloc' class='error'></span>
</div> 

<div class='container'>
   <label for='method' >
   How would you prefer we contact you?*</label></br>
   <select name="method" id="method"><?php echo $formproc->SafeDisplay('method') ?>
   <option value=""></option>
   <option value="Phone" name="phone">Phone</option>
   <option value="E-mail" name="email">E-Mail</option>
   </select></br>
   <span id='contactus_method_errorloc' class='error'></span>
</div>

<div class='container'>
   <label for='destemail' > Which department are you trying to reach?*</label></br>
   <select name="destemail" id="destemail">
   <?php foreach ($emailAddresses as $name => $email) { ?>
   <option value="<?php echo htmlspecialchars($name); ?>"><?php echo  
   htmlspecialchars($name) ; ?></option>
   <?php } ?></select></br>
   <span id='contactus_destemail_errorloc' class='error'></span>
</div>

<input type="submit" />
</fieldset>
</form>

Help is much appreciated! Also, I'm a relatively new coder, so putting everything as simply as possible would help a lot. Thanks!

You can redirect user after sending email by replacing

exit("Email sent successfully.");

with

header('Location: /thankyou.php');
exit();

It'll process form via your current script and then redirect user to specified page (you can also specify absolute url, eg. http://example.org/thankyou.php ).

change

exit("Email sent successfully.");

to

header('Location: http://www.site.com/thank_you.php');
exit();

or:

include_once('thank_you.php');

In contactus.php After storing / mailing the contact us detaisl redirect to thankyou.php

Using header("location:thankyou.php"); exit(); header("location:thankyou.php"); exit();

You must change this section of code:

  if (mail($emailAddresses[$_POST['destemail']],$emailSubject,$emailBody,"From:  
  {$_POST['email']}")) {exit("Email sent successfully.");
  } else {exit("Email sending failed");

to

  if (mail($emailAddresses[$_POST['destemail']],$emailSubject,$emailBody,"From:  
  {$_POST['email']}")) {header("Location: thankyou.php");
  } else {exit("Email sending failed");

Because your form uses action='<?php echo $formproc->GetSelfScript(); echo
htmlentities($_SERVER['PHP_SELF']); ?>'
action='<?php echo $formproc->GetSelfScript(); echo
htmlentities($_SERVER['PHP_SELF']); ?>'
action='<?php echo $formproc->GetSelfScript(); echo
htmlentities($_SERVER['PHP_SELF']); ?>'
, it is posting back to itself to validate all the form data and then send the email.
The other way to fix this would be to validate everything via Javascript and then make the decision whether to flag the sections of the form that need attention or move on to the thankyou.php script.

if (mail($emailAddresses[$_POST['destemail']],$emailSubject,$emailBody,"From:  
  {$_POST['email']}")) {
   require_once('...path/thankyou.php');
   exit('Email sent successfully'););
}    

I think it's best to understand what's going on in a form submission, and dispel the idea that some magic is happening here.

With form submissions, you're working with a work flow of sorts:

  1. HTML Form
  2. Processing Script
  3. Thank You Page.

Each is a page, and the browser must be told to go to each page. Knowing this, if there is ever a problem, you can mentally walk through each step. You can also see this activity by looking a debug tools like Firefox Web Console (CTRL + SHIFT + K), Chrome Developer Tools (CTRL + SHIFT + J), and IE Developer Tools (F12).

Step 1 -> Step 2

The loading of the page in Step 2, the processing script, is accomplished through the action attribute of the form you have, which it looks like you may already know. However, the URL the form was originally submitting to looked a little funny. It should be pointing to a file on the server, I would expect "contactus.php". With "contactus.php/contactus.php", Apache Web Server must be translating the URL, moving the second "contactus.php" into the querystring.

In your example, you changed this action to "thankyou.php", which would effectively skip loading the processing script in step 2 entirely, as was experienced.

Step 2 -> Step 3

As others have suggested, using PHP's header method ( header("LOCATION: thankyou.php") ) in place of exit("Email sent successfully."); is a good solution. This is telling the browser to go to "thankyou.php" by setting the HTTP header. This is a key: value format, with LOCATION being a special key the browser acts upon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM