Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP显示/隐藏元素以及如何将用户发送到其他页面 我正在为我的联系表格挑选PHP。我想做的是,如果表单没有填写,就会出现一条错误消息。我的html中有一个div,其中包含我要显示的错误_Php_Html - Fatal编程技术网

如何使用PHP显示/隐藏元素以及如何将用户发送到其他页面 我正在为我的联系表格挑选PHP。我想做的是,如果表单没有填写,就会出现一条错误消息。我的html中有一个div,其中包含我要显示的错误

如何使用PHP显示/隐藏元素以及如何将用户发送到其他页面 我正在为我的联系表格挑选PHP。我想做的是,如果表单没有填写,就会出现一条错误消息。我的html中有一个div,其中包含我要显示的错误,php,html,Php,Html,我知道我可以使用“echo”来显示消息,但我希望它准确地定位在我想要的位置。我已经看到了这样做的方法,但它涉及到将所有php代码放在html文件中…然后重命名file.php。我希望将大部分php代码与html分开 这是我的密码: HTML: 它仍然不起作用。就好像它不能从html文件中读取php文件中的代码,即使我包含了它 另一件事是,一旦表单提交,我将如何将用户发送到另一个html页面?是通过替换来完成的吗 // Print a message: echo '<p><em&

我知道我可以使用“echo”来显示消息,但我希望它准确地定位在我想要的位置。我已经看到了这样做的方法,但它涉及到将所有php代码放在html文件中…然后重命名file.php。我希望将大部分php代码与html分开

这是我的密码:

HTML:

它仍然不起作用。就好像它不能从html文件中读取php文件中的代码,即使我包含了它

另一件事是,一旦表单提交,我将如何将用户发送到另一个html页面?是通过替换来完成的吗

// Print a message:
echo '<p><em>Thank you.</em></p>';

提前感谢您的帮助。

您可以考虑使用javascript解决方案。首先将div设置为
display:none然后您可以使用:

document.getElementsByClassName('error')[0].style.display = "block";
当你想让它可见的时候。还有JQuery解决方案:(可能不正确,请朋友帮助plz!)

您还可以使用此处的说明设置速度/淡入淡出
同样对于重定向,是的,您可以替换它们。或者,您也可以使用JS解决方案。替换

echo '<p><em>Thank you.</em></p>';
echo'谢谢。

与:

echo'谢谢。

位置。替换(“你想去哪里!”);

希望这有帮助。

首先。要使用php代码,还需要将contact.html转换为.php文件。
考虑命名您的触发器.php> CONTACT.Script .php,并将CONTACT.HTML重命名为Copy.php.bR> 然后,在php代码中可以执行以下操作:

contact.script.php

if((!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
    ...
    $message = 'Thank you.';
} else {
#   put your error message into the same variable
    $message =  'Please fill out the form completely.';
}
contact.php:

/* at the very beginning of your code */
include 'path/to/contact.script.php';
/* Then, wherever you would like to show the message just add this at the end: */
<?=$message?>  //  this will echo out the message (same as <?php echo $message; ?> - as in:

<div class="error"><p><?=$message?></p></div>
/*在代码的开头*/
包括“path/to/contact.script.php”;
/*然后,无论您想在哪里显示消息,只需在末尾添加以下内容:*/
//这将回显消息(与中的-相同):


首先,您需要了解服务器如何处理PHP和HTML文件。服务器配置告诉它如何解释文件;有些文件按原样提供,有些文件被传递给处理程序作为脚本执行,等等。在您的服务器配置中,可能会有一行命令服务器使用ex处理文件张力<代码> .php <代码>作为使用PHP执行的脚本。HTML文件不需要执行;它们被发送到客户端,客户端的浏览器解释HTML、CSS和JS信息来呈现网页。因为HTML文件不是作为脚本执行的,如果您把代码(Perl、PHP、java、C++、SimalTalk……随便想做!)在文件中,它不会由服务器执行,也不会由浏览器执行,除非浏览器有该语言的解释器,就像HTML、CSS和javascript一样。这就是为什么HTML文件中的PHP不会被执行的原因:除非您指示web服务器将该文件解释为PHP脚本,否则它将它不像一个标准的html文件

在您的情况下,如果表单验证失败,您希望向用户显示一条错误消息。您还希望将代码和HTML文件分开(这是一个很好的习惯,IMHO!)。这是一个问题,因为您确实需要更改HTML页面的内容以包含错误消息,但HTML页面基本上是静态的。您可以将用户重定向到另一个具有相同内容的页面,但页面顶部会显示错误消息,但这是一个相当丑陋的解决方案。您还可以使用javascript执行表单设置如果用户提交的表单无效,请单击lidation并向用户显示错误消息,但是关闭JS的任何人都可以绕过该检查

针对您的问题,最灵活的解决方案是使用一个模板系统,该系统允许您自定义页面内容,但保持PHP代码不受HTML的影响。有许多PHP模板系统;google将为您提供大量的模板。模板包含可以在PHP脚本中设置并在模板中使用的变量;t然后,模板引擎解析模板,插入变量,并输出结果

在您的情况下,您可以将表单设置为在表单顶部显示错误消息,并可能突出显示错误填写的字段

下面是一个使用流行、灵活的模板系统的示例:

include('Smarty.class.php');

// create object
$smarty = new Smarty;

if (! empty($_POST)) {
    // validate your form input
    ...
    // Minimal form validation:
    if (!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
        // send your email
        // redirect user to a thank you page
        header("Location: thankyou.html");
    }
    else {
        // sets a variable $error in the template
        $smarty->assign('error', 'Please fill out the form completely');
    }
}

// display it
$smarty->display('form.tpl');
模板文件
form.tpl
应包含您的标准表单以及出现错误时需要添加的任何部分。以下是示例
form.tpl
文件中的一个片段:

<h2>Contact me!</h2>
{if isset($error)}
<div class="error">
    <p>
        {$error}
    </p>
</div>
{/if}

<form id="contactForm" action="contact.php" method="post">
(etc.)
联系我!
{if isset($error)}

{$error}

{/if} (等等)
如果contact.php检测到错误,则输出:

<h2>Contact me!</h2>
<div class="error">
    <p>
        Please fill out the form completely
    </p>
</p>
<form id="contactForm" action="contact.php" method="post">
(etc.)
联系我!

请完整填写这张表格

(等等)
如果没有错误(即未填写表单),则输出:

联系我!
(等等)

这是一个模板可以做什么的非常基本的例子。我希望这已经澄清了一些事情,并让您一窥HTML模板的奇妙世界。

如果您希望表单页面具有动态内容,您需要向其添加PHP。要将用户重定向到新页面,请使用以下函数:
函数到($url){if(headers_sent()){echo'parent.window.location(“.$url.””);}else{header('location:“.$url”);exit();}
@wavemode我理解为什么我需要在html文件中使用PHP,但这意味着我将“contact.html”改为“contact.PHP”,从我所读到的内容来看,这会影响google analytics。@Forrest不一定;您可以指示Apache(可能还有其他服务器)将某个文件视为PHP(或其他编程语言),而不考虑文件名。Re:redirection:header('Location:…')是正确的。谢谢,不过我已经
<input type="text" value="<?php echo $ta;?>">

<div id="content" style="display:<?php echo $ta==2 ? 'block':'none' ?>"></div>
echo '<p><em>Thank you.</em></p><script>location.replace("WHERE YOU WANNA GO!");</script>';
if((!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
    ...
    $message = 'Thank you.';
} else {
#   put your error message into the same variable
    $message =  'Please fill out the form completely.';
}
/* at the very beginning of your code */
include 'path/to/contact.script.php';
/* Then, wherever you would like to show the message just add this at the end: */
<?=$message?>  //  this will echo out the message (same as <?php echo $message; ?> - as in:

<div class="error"><p><?=$message?></p></div>
include('Smarty.class.php');

// create object
$smarty = new Smarty;

if (! empty($_POST)) {
    // validate your form input
    ...
    // Minimal form validation:
    if (!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
        // send your email
        // redirect user to a thank you page
        header("Location: thankyou.html");
    }
    else {
        // sets a variable $error in the template
        $smarty->assign('error', 'Please fill out the form completely');
    }
}

// display it
$smarty->display('form.tpl');
<h2>Contact me!</h2>
{if isset($error)}
<div class="error">
    <p>
        {$error}
    </p>
</div>
{/if}

<form id="contactForm" action="contact.php" method="post">
(etc.)
<h2>Contact me!</h2>
<div class="error">
    <p>
        Please fill out the form completely
    </p>
</p>
<form id="contactForm" action="contact.php" method="post">
(etc.)
<h2>Contact me!</h2>
<form id="contactForm" action="contact.php" method="post">
(etc.)
<input type="text" value="<?php echo $ta;?>">

<div id="content" style="display:<?php echo $ta==2 ? 'block':'none' ?>"></div>