简体   繁体   中英

PHP contact form script won't run/ page opens instead

I'm stuck and I hope someone can help me. I'm trying to add a simple contact form to my website using PHP action=" " method="post". When I click the "submit" button, instead of running the script, the "send_contact.php" page opens and you see all the code. I've checked and tested my server (blue.host) and it is set up to run php scripts. I've tried everything and I'm at a loss. Here is the HTML:

<div class="form">
<form id="myform" name="myform" action="send_contact.php" method="post">
<label>Name</label>

<input type="text" name="name" id="name" />                 
<label>E-mail</label>
<input type="text" name="email" id="email" />

<label>Phone</label>
<input type="text" name="phone" id="phone" />

<label>Message</label>
<textarea name="message" rows="5" cols="60"></textarea>

<button name="send" type="submit">send</button>
<button name="reset" type="reset">reset</button>
<div class="spacer"></div>
</form><!--end myform div-->
</div><!--end form div-->                       

Here's the PHP script:

<?php

// From
$header="from: $name <$mail_from>";

// Mail of sender
$mail_from="$customer_mail";

// Contact phone
$phone ="$phone"; 

// Details
$message="$message";


// Enter your email address
$to ='jacine.arias@gmail.com';

$send_contact=mail($to,$header,$mail,$phone,$message);


// Check, if message sent to your email
// display message "Thank you! Your message has been recieved."
if($send_contact){
echo "Thank you! Your message has been recieved.";
}
else {
echo "ERROR";
}
?>

Here's the CSS:

/*-----------FORM---------------*/

.form {
    border: 1px solid #262223;
    margin: 0 auto;
    padding: 14px;
    width: 375px;

}

.form label {
    display: block;
    text-align: right;
    width: 80px;
    float: left;
}

.form input {
    float: left;
    font-size: 12px;
    padding: 4px 2px;
    border: 1px solid #262223;
    width: 270px;
    margin: 2px 0 10px 10px;
}

textarea {
    float: left;
    font-size: 12px;
    padding: 4px 2px;
    border: 1px solid #262223;
    width: 270px;
    margin: 2px 0 20px 10px;
}

button[type="submit"] {
    clear: both;
    color: #fff;
    width: 100px;
    height: 31px;
    text-align: center;
    background: #F20F4B;
    margin-right: 5px;
    float: left;
}

button[type="reset"] {
    clear: both;
    color: #fff;
    width: 100px;
    height: 31px;
    text-align: center;
    background: #F20F4B;
    display: inline;
}

Here the actual page from my website: http://jacineariasdesign.jacineariasweb.com/contact.html Any help to make this work would be appreciated! Thank you!

I looked at your site, and see that your file does end in .php. I think that your server has not been set up to execute .php files as php.

You'll have to work with the administrator of your server to make sure that .php files are being run through the php handler.

The specifics of the configuration are going to depend on your server platform ... There are a lot of sites out there to walk you through the steps. I don't want to recommend one specifically because I don't know what your starting point is.

Here, give this a try. Tested and could stand to be modified with extra security features, but it works with your form supplied.

Added a bit of error checking also.

If it doesn't work for you, then you have a PHP issue on "your" server.

Your original PHP script never had a Subject entry to start with.

<?php

$headersep = "\r\n";
$header = "From: $name <$email>" . $headersep . "Reply-To: $name <$email> . $headersep";
$email = $_POST['email'];
$subject="Your subject here";
$phone = $_POST['phone']; 
$message = "From: $name\n\nMessage: $message\n\nEmail: $email\n\nTelephone: $phone";
$to ='youremail@example.com';

if (!empty ($_POST['email']) && ($_POST['message'])) {
  mail($to, $subject, $message, $header);
echo "Thank you $name, your message has been received.";
exit;
}

if ( (empty ($_POST['email'])) && (empty ($_POST['message'])) ) {
echo "ERROR, you did not fill in the <b>Email</b> and the <b>message</b> body.";
exit;
}

elseif (empty ($_POST['email'])) {
echo "ERROR, you did not fill in your Email address.";
exit;
}

elseif (empty ($_POST['message'])) {
echo "ERROR, you did not fill in the message body.";
exit;
}

?>

please see my php script below. I have set up SSMTP and echo "hello " | mail -s "test" mail@gmail.com echo "hello " | mail -s "test" mail@gmail.com from Ubuntu command line sends mail.

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'Contact form <www.sabinamortgages.ca>';

// an email address that will receive the email with the output of the form
$sendTo = 'Sabina <sabina.kandik@premieremortgage.ca>';

// 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'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
 *  LET'S DO THE SENDING
 */


// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

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'];
}

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