我想添加一个回到上一页的php文档的链接

时间:2013-03-09 15:25:45

标签: php

我正在使用PHP制作一封电子邮件表单,当您不填写必填字段时,您将被发送到另一个页面,其中包含您需要更正的错误。

在这些错误的底部,我想制作返回链接,但我无法弄清楚我做错了什么,因为Dreamweaver在我编写的行中向我显示错误。所以这是代码,我将用注释标记该行。

<?php
if(isset($_POST['email'])) {

$email_to = "example@example.com";
$email_subject = "website html form submissions";

function died($error) {
    echo "Atsiprašome, bet yra klaidų Jūsų užpildytoje formoje.<br /><br /> ";
    echo $error."<br /><br />";
    echo "Grįžkite ir ištaisykite klaidas.<br /><br />";
    echo "<a href="contacts.html">Atgal</a>";  //this is the line
    die();
}



if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) {
    died('Atsiprašome, bet yra klaidų Jūsų užpildytoje formoje.');       
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name'];   // required
$email_from = $_POST['email'];      // required
$telephone = $_POST['telephone'];   // not required
$comments = $_POST['comments'];     // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'Neteisingai įvestas el. paštas.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'Neteisingai įvestas vardas.<br />';
}
if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'Neteisingai įvesta pavardė.<br />';
}
if(strlen($comments) < 2) {
    $error_message .= 'Neteisingai įvesta žinutė.<br />';
}
if(strlen($error_message) > 0) {
    died($error_message);
}
$email_message = "Form details below.\n\n";

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

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


$headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- place your own success html below -->

Ačiū už Jūsų žinutę.

<?php
}
die();
?>

2 个答案:

答案 0 :(得分:0)

echo "<a href="contacts.html">Atgal</a>";  //this is the line

你在这里做的是在<a href=结束字符串文字。

您需要做的是将其更改为:

echo "<a href='contacts.html'>Atgal</a>";

答案 1 :(得分:0)

在PHP中,引号用于标记字符串的开头和结尾。您可以使用单引号或双引号。

'This is a string'

"This too!"

如您所见,您可以使用单引号或双引号。但是,我们还希望引号在字符串中显示。那我们该怎么处理呢?

好吧,一个选择是使用Chris Cooney的答案,并确保字符串中的引号与标记开头和结尾的引号不同:

"I'm a string"

'"Murder," she wrote'

这些都是有效的。

但是当你需要在你打开它的字符串中使用相同的类型的引用时呢?例如,您可能需要在字符串中使用这两种类型。在这种情况下,您必须转义引用。所有这一切都意味着在他们面前添加\

'I\'m a valid string!'

基于此,您可以在此行修复您的问题:

echo "<a href="contacts.html">Atgal</a>";

写下任何一个:

echo "<a href='contacts.html'>Atgal</a>";

echo "<a href=\"contacts.html\">Atgal</a>";

echo '<a href=\'contacts.html\'>Atgal</a>';

仅举几例。