简体   繁体   中英

Why won't my HTML/PHP form work?

Ok, so I made a form using HTML, and I'd like to get it to submit the information to my email, so I found and modified this PHP script:

<?php
$to = "me@myemail.com";
$subject = "R&R Form";
$firstname1 = $_REQUEST['firstname1'] ;
$lastname1 = $_REQUEST['lastname1'] ;
$firstname2 = $_REQUEST['firstname2'] ;
$lastname2 = $_REQUEST['lastname2'] ;
$department1 = $_REQUEST['department1'] ;
$department2 = $_REQUEST['department2'] ;
$reason = $_REQUEST['reason'] ;
$behaviour1 = $_REQUEST['behaviour1'] ;
$behaviour2 = $_REQUEST['behaviour2'] ;
$behaviour3 = $_REQUEST['behaviour3'] ;
$behaviour4 = $_REQUEST['behaviour4'] ;
$behaviour5 = $_REQUEST['behaviour5'] ;
$behaviour6 = $_REQUEST['behaviour6'] ;
$behaviour7 = $_REQUEST['behaviour7'] ;

$message = "Nominee: $firstname1 $lastname1 /n Department: $department1 /n /n Nominator: $firstname2 $lastname2 /n Department: $department2 /n /n Reason for nomination: $reason /n /n Additional reasons: $behaviour1 /n $behaviour2 /n $behaviour3 /n $behaviour4 /n $behaviour5 /n $behaviour6 /n $behaviour7 /n";

$headers = "Recognition and Reward Request for $firstname1 $lastname1";
$sent = mail($to, $subject, $message, $headers,) ;
if($sent)
{print "Your nomination was submitted successfully"; }
else
{print "We encountered an error submitting your nomination"; }
?>

It's not very well written, I know (I only started learning php today, and I just modified a script I copy and pasted.), but it doesn't seem to have any syntax errors or any other errors I can see. I'm not asking for someone to fix my code for me, I'm just asking for some pointers as to why the script isn't working as it should.

I uploaded it to a server with PHP installed, so that's not the problem. I've been trying to figure this out all day, and it's getting kinda frustrating. Someone please help?

Well, the script is using this for the headers, which is invalid:

$headers = "Recognition and Reward Request for $firstname1 $lastname1";

Maybe you meant for that to be the subject line?

The headers should be valid SMTP headers, like this:

$headers = 'From: webmaster@example.com' . "\r\n";

Look at the examples for the mail function for more info.

$sent = mail($to, $subject, $message, $headers,) ;

should look like this:

$sent = mail($to, $subject, $message, $headers) ;

(without the comma)

hope i helped

Since you are a starter I recommend you to use PEAR as much as possible.

Look at: pear html quickform

It will really make your life easier.

And for sending e-mails, I suggest you to use: PHPMailer

It comes with a lot of e-mail features just right out-of-the-box

The first problem I see is that $headers doesn't contain valid headers. Headers are things like From: name@example.com or CC: someoneelse@example.com , but you're treating it as part of the email.

Here's some info on email headers .

It does have syntax errors. Since you cannot see them, I suggest you enable full error reporting. There're many ways to do it; the simplest is probably adding this code on top of your script:

<?php

ini_set('display_errors', true);
error_reporting(E_ALL);

?>

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