此联系人表单的代码中存在PHP错误

此联系人表单的代码中存在PHP错误,php,html,forms,Php,Html,Forms,我是PHP新手,最近一直在学习构建表单。我能够让它们启动并运行,严格遵循教程网站上的说明。但这个表单是我试图通过键入我记得在w3c网站上学到的代码来构建的。我很难让它运行。请帮帮我 这是表单的HTML代码: <form name="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactform"> <label for=

我是PHP新手,最近一直在学习构建表单。我能够让它们启动并运行,严格遵循教程网站上的说明。但这个表单是我试图通过键入我记得在w3c网站上学到的代码来构建的。我很难让它运行。请帮帮我

这是表单的HTML代码:

<form name="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactform">

<label for="first_name">First Name<span class="error">*<?php echo $first_nameErr;?></span></label><br>

<input type="text" name="first_name" maxlength="50" size="20" value="<?php echo $first_name;?>"><br>

<label for="last_name">Last Name<span class="error">*<?php echo $last_nameErr;?></span></label><br>

<input type="text" name="last_name" maxlength="50" size="20" value="<?php echo $last_name;?>"><br>

<label for="email">Email Address<span class="error"><?php echo $emailErr;?>*</span></label><br>

<input type="text" name="email" maxlength="80" size="35" value="<?php echo $email;?>"><br>

<label for="overview">Overview of project <span class="error">*&nbsp;<?php echo $overviewErr;?></span></label><br>

<textarea name="overview" maxlength="1000" cols="25" rows="5"><?php echo $overview;?></textarea><br>

<br>&nbsp;<input type="submit" value="Submit" name="submit" class="art-button" style="zoom: 1;">&nbsp;
</form>
<?php

function test_input($data){

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
}

function clean_string($string) {

    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);

}


// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";

if(isset($_POST['email'])) {

$email_to = "myself@mydomain.com";
$email_subject = "Contact us - My company's name";
{

if (empty($_POST["first_name"]))
{$first_nameErr = "(First Name is required)";}

else
{$first_name = test_input($_POST["first_name"]);

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
      {
      $first_name = "(Only letters and white space allowed)"; 
      }
    }

if (empty($_POST["last_name"]))
{$last_nameErr = "(Last Name is required)";}

else
    {$last_name = test_input($_POST["last_name"]);

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
      {
      $last_name = "(Only letters and white space allowed)"; 
      }
    }

if (empty($_POST["email"]))
    {$emailErr = "(Email ID is required)";}

else
    {$email = test_input($_POST["email"]);

// check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
      {
      $emailErr = "(Invalid email format)"; 
      }
    }

if (empty($_POST["overview"]))
    {$overviewErr = "(Overview is required)";}

else
    {$overview = test_input($_POST["overview"]);

// check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$overview))
      {
      $overview = "(Only letters and white space allowed)"; 
      }
    }

}

//Email & SEND INFO
$email_message = "Form details below.\n\n";

    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "Services: ".clean_string(implode(', ', $service))."\n";

    $email_message .= "Overview: ".clean_string($overview)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- Success HTML -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}

?>

另外,请告诉我,在我的电子邮件中,我没有收到发件人,这是哪里出了问题。在我以前的所有脚本中,它都将发送者显示为“我”

在PHP中,就像在许多语言中一样,事情的顺序很重要。因此,当脚本执行时,它到达一个函数调用,该函数必须已经在某处定义。首先,定义一个函数。然后,调用函数。你不能把一个函数放在任何地方(比如在一个if块中),然后期望代码搜索并使用它。确保所有函数都是在任何if块之外定义的,因为如果条件的计算结果为false,则永远不会定义函数。在学习过程中,只需将所有函数放在脚本顶部,任何逻辑块之外。按以下方式启动脚本:

function test_input($data){

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
}

function clean_string($string) {

    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);

}

// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "sriprabhav@ymail.com";
$email_from = $_POST['email'];

$email_subject = "Contact us - Origin web designers";
//{ why are there stray brackets everywhere? if(condition){code} else{code}

if (empty($_POST["first_name"]))
    {$first_nameErr = "(First Name is required)";}
else 
    {$first_name = test_input($_POST["first_name"]);}

    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
    $first_name = "(Only letters and white space allowed)"; 
}

 if (empty($_POST["last_name"]))
等等。。。和以前一样

要处理复选框,您需要这样的内容。这将定义一个名为$service的数组:

if (isset($_POST['service'])) {
    $service = $_POST['service'];
}
它可以放在这条线的正前方:

//Email & SEND INFO

然后,当您的代码点击$email_消息时服务:行,它将有一个名为$service的数组来内爆

为什么你的函数被限制在if语句中?我不明白你在说什么。对不起,我的PHP新手技能。你能说得更具体一点吗?如果可能的话,你能告诉我需要更正什么吗?我该如何更正?做一个
函数测试输入($variable){//some code}
,或者干脆把测试输入一起删除
函数测试输入($input){//do some to test input}
@Daedalus我知道,这是一个可爱的小演示,但是为了一个新手,假装一下。保持简单。你不可能在第一天就学会所有东西。我将编辑我的帖子,以解决真正的问题,这是如果块。一个不是'假装为了新手',一个给出正确的例子第一次。如果你知道它是错的,不要说它是对的,这是公平的。我尊重这一立场。尤其是代码。但是,如果你还没有,那就找个时间到战壕里来,和初学者面对面地上几节课。然后我们就可以进行一次真正的对话,讨论为新手做什么和不做什么。这行:@mail($email\u to,$email\u subject,$email\u message,$headers);应该是邮件($email\u to、$email\u subject、$email\u message、$headers);对于电子邮件发件人地址,您需要定义变量$email\u from somewhere。所以就在这行前面:$email_to=”myself@mydomain.com"; 您需要将变量$email\u from设置为与发布的电子邮件地址相等,如:$email\u from=$\u POST['email'];对于您的复选框,名称很好。他们将生成一个称为服务的数组。但要处理该数组,需要一条语句。我将编辑我的答案,以包括一行用于处理所有这些问题。另外,听听@Daedalus说的“不要在不知道代码首先做什么的情况下使用代码。”这是至关重要的。
if (isset($_POST['service'])) {
    $service = $_POST['service'];
}
//Email & SEND INFO