-2

So like loads of people on here, I'm new and know just enough coding to be break everything.

I grabbed a contact form template (jscript & PHP mix) and it works, and even only broke the rest of my HTML contact page only a little.

But my concern is that the result of the validation is that it produces just one of two sentences with no link or navigation to return to the contact or index page. As a PHP noob, I'd just like to try to tackle this the right way, which several hours of Googling hasn't helped. (My programming skills are barely beyond if/then statements). So any input or suggestions would be most welcome!

Here's the section that I think leads to the dead end, in the contact.php Thanks!

// an email address that will be in the From field of the email.
$from = '<insert email>';

// an email address that will receive the email with the output of the form
$sendTo = '<insert email>';

// subject of the email
$subject = 'New message from contact form';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message'); 

$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
SGarceau
  • 1
  • 1
  • 1
    So, you searched for several hours for solutions related to "php page redirect" for example, and didn't come across anything like [this](https://stackoverflow.com/questions/2112373/php-page-redirect) or [this](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php)? – Patrick Q Jan 14 '20 at 14:52
  • You can add button if mail is done with a simple `echo '` or header directly to a page like `header("location: page.php");` – Simone Rossaini Jan 14 '20 at 14:54
  • @PatrickQ Wow, are all the old guard on here so snide? And yes I found those, but I can barely understand PHP and those pieces of code made no sense to me even after reading more on w3schools and php.net didn't help. Hence the request for more help. – SGarceau Jan 14 '20 at 15:56
  • @SGarceau You are expected to show a certain level of effort to solve your own problem when posting a question here. You haven't done that. The code that you posted shows what you have, but that's not what you want. We need to see what you've tried to accomplish what you want. Those linked questions have _very_ basic answers. I'm afraid that if you weren't able to make enough sense of them to even make an attempt, then no additional answer provided here will make any more sense to you. If you "can barely understand PHP", now is the time to stop and take the time to _learn_ what you're doing. – Patrick Q Jan 14 '20 at 16:15
  • @SGarceau A question that looked like the following likely would have been better received: I am try to do XYZ. In trying to accomplish this, I searched "search term here" which led me to these solutions (link, link). I attempted to implement these solutions into my code by doing this: `code here`. The result was ABC, but I expected/wanted the result to be CBA. I did the following debugging (...) and narrowed to the issue to (blah blah). – Patrick Q Jan 14 '20 at 16:24
  • @PatrickQ And yet, three people were able to provide enough information for me to make sense of things to at least try. I've already had to teach myself CSS, Flexbox, Grid and Sass in the last week just to try and get a personal web site up and going. So I apologize if my competency level is too low to warrant actual help. Thankfully I got something I can at least work with from the others. Have a great day. – SGarceau Jan 14 '20 at 16:35

1 Answers1

0

1- You can use html code in your $okMessage:

$okMessage = "Contact form successfully submitted. Thank you, I will get back to you soon!<br><button onclick='history.go(-1);'>Back </button>";

2- You can redirect the visitor to your page;

$responseArray = array('type' => 'success', 'message' => $okMessage);

//strcmp checks if strings are equal, if they are it will redirect the user to the page
if(strcmp($responseArray['type'],'success') == 0)
header("Location: myPage.php");

//or you can give 5 seconds to the user to read your success message then redirect:

header( "Refresh:5; url=http://www.example.com/page2.php");
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119