Do not redirect

Hi there I am not that well versed in PHP and need some help please. I have set up a contact form and that submits ok. Just that upon submission the visitor will be left with this ugly screen.

Ideally I would like them to be left as to where they were when they filled out the form.

The HTML for the code is:-

                    <div class="form-group">
                        <input class="form-control" id="name" name="name" placeholder="Your Name" type="text" required>
                    </div>

                    <div class="form-group">
                        <input class="form-control" id="email" name="email" placeholder="Email Address" type="email" required>
                    </div>

                    <div class="form-group">
                        <textarea class="form-control" id="message" name="massage" placeholder="Type your message here" rows="5" required></textarea>
                    </div>

                    <div id="contactFormResponse"></div>

                    <button id="cfsubmit" type="submit" class="btn btn-dark btn-block contact-submit">Say Hello</button>

                </form>

The PHP is:-

//send the email
$result = '';
if (mail ($to, $subject, $body)) {
	$result .= '<div class="alert alert-success alert-dismissible" role="alert">';
	$result .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
	$result .= 'Thank You! I will be in touch';
	$result .= '</div>';

	echo $result;
	die();
}

$result = '';
$result .= '<div class="alert alert-danger alert-dismissible" role="alert">';
$result .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
$result .= 'Something bad happend during sending this message. Please try again later';
$result .= '</div>';

echo $result;

I hope someone could please help. Thanks JP

Either use JS/Ajax to submit the form to the PHP script or add a redirect header to the PHP script so it redirects back to the page you have the contact form on.

You can add a URL variable to the redirrect to display the result.

$result = 'error';
if (mail ($to, $subject, $body)) 
{
  $result = 'ok';
}
header("Location: /contactForm.php?result=$result");

Then on the contact form to display that message:

if ( isset($_GET['result'])
{
   $results = filter_input(INPUT_POST, 'result')
   switch ($result){
   case 'ok':
     $result = 'Thank You! I will be in touch'
   break;
   case 'something else':
     $result = 'your custom message';
   break;
   default:
      $result = 'Something bad happend during sending this message. Please try again later'
   }
}
1 Like

Thanks for that @djsmithme

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.