获取要在HTML Div中显示的PHP变量
尝试获取$missing的错误消息以显示在id为error的表单div中,如果表单填写正确,则在id为success的表单div中显示$complete消息获取要在HTML Div中显示的PHP变量,php,jquery,html,Php,Jquery,Html,尝试获取$missing的错误消息以显示在id为error的表单div中,如果表单填写正确,则在id为success的表单div中显示$complete消息 <?php $error = false; $missing = ""; if ($_POST) { $complete = "<p><strong>Thank you!</strong> Your message was sent, we'll get back to yo
<?php
$error = false;
$missing = "";
if ($_POST) {
$complete = "<p><strong>Thank you!</strong> Your message was sent,
we'll get back to you ASAP!</p>";
if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false {
$missing .= "A vaild email address is required.<br>";
$error = true;
}
if (!$_POST['subject']) {
$missing .= "A subject field is required.<br>";
$error = true;
}
if (!$_POST['content']) {
$missing .= "A content field is required.<br>";
$error = true;
}
if ($error) {
$missing = "<p>". $missing."</p>";
} else {
$complete;
}
}
?>
我认为不应该这样(因为当你提交表单时,它总是发布,所以你必须检查它是否为空)
应该是
if($_POST['subject']=='')
if($_POST['content']=='')'
( - it means if data is blank)
和在HTML页面中
你应该使用
<?php echo $missing; ?> or <?=$missing;?>
<?php echo $complete; ?> or <?=$complete;?>
或
或
我认为不应该这样(因为当你提交表单时,它总是发布,所以你必须检查表单是否为空)
应该是
if($_POST['subject']=='')
if($_POST['content']=='')'
( - it means if data is blank)
和在HTML页面中
你应该使用
<?php echo $missing; ?> or <?=$missing;?>
<?php echo $complete; ?> or <?=$complete;?>
或
或
参见代码中的注释
<?php
$error = false;
$missing = "";
if ($_POST) {
$complete = "<p><strong>Thank you!</strong> Your message was sent, we'll get back to you ASAP!</p>";
if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false ) { // There was a missing parenthesis here.
$missing .= "A vaild email address is required.<br>";
$error = true;
}
if ($_POST['subject']=="") { // Check if empty, not if exist... It may exist as empty.
$missing .= "A subject field is required.<br>";
$error = true;
}
if ($_POST['content']=="") {
$missing .= "A content field is required.<br>";
$error = true;
}
if ($error) {
$missing = "<p>". $missing."</p>";
$complete = ""; // Remove the success string here.
}
}
?>
参见代码中的注释
<?php
$error = false;
$missing = "";
if ($_POST) {
$complete = "<p><strong>Thank you!</strong> Your message was sent, we'll get back to you ASAP!</p>";
if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false ) { // There was a missing parenthesis here.
$missing .= "A vaild email address is required.<br>";
$error = true;
}
if ($_POST['subject']=="") { // Check if empty, not if exist... It may exist as empty.
$missing .= "A subject field is required.<br>";
$error = true;
}
if ($_POST['content']=="") {
$missing .= "A content field is required.<br>";
$error = true;
}
if ($error) {
$missing = "<p>". $missing."</p>";
$complete = ""; // Remove the success string here.
}
}
?>
在没有看到表单的情况下,很难在那里解决任何问题,但如果您有表单,最好进行一些返工。您不需要$error
标记,应该将错误存储在数组中。如果数组末尾的计数大于1,则存在明显错误。在确定post值是否为空之前,还应首先删除post值中的空格:
<?php
# Make a function to remove spaces in the array. If the user types two empty
# spaces, it's considered not empty, so this will remove that and only leave
# actual string content (or empty).
function trimArray($array)
{
# Trim the value of empty spaces
if(!is_array($array))
# Send back since not an array
return trim($array);
# Loop through array and apply this same function
foreach($array as $key => $value) {
$array[$key] = trimArray($value);
}
# Return this array
return $array;
}
# Store your errors in an array
$missing = array();
# Check if the post is empty or not
if(!empty($_POST)){
# Remove empty spaces from values in the post
$POST = trimArray($_POST);
# Check email
if(!filter_var($POST["email"], FILTER_VALIDATE_EMAIL))
$missing[] = "A vaild email address is required.";
# Check if subject set
if(empty($POST['subject']))
$missing[] = "A subject field is required.";
# Check if content
if(empty($POST['content']))
$missing[] = "A content field is required.";
if(empty($missing)) {
# Send mail here
# Also check that it was successful...
}
}
?>
<!-- You only need one div that has a ternary condition. Set the class and message value -->
<!-- based on whether the $message array is empty or not -->
<form method="post" action="#">
<h1>Get in touch!</h1>
<?php
# You have no neutral condition so if there is no post, then it will
# still come back as saying it's successful.
# But that doesn't make any sense if no post was sent....
if(isset($_POST['email'])) { ?>
<div id="error" class="alert alert-<?php echo (!empty($missing))? 'danger' : 'success' ?>" role="alert">
<p><?php echo (!empty($missing))? implode('<br />',$missing) : "<strong>Thank you!</strong> Your message was sent, we'll get back to you ASAP!" ?></p>
</div>
<?php } ?>
在没有看到表单的情况下,很难在那里解决任何问题,但如果您有表单,最好进行一些返工。您不需要$error
标记,应该将错误存储在数组中。如果数组末尾的计数大于1,则存在明显错误。在确定post值是否为空之前,还应首先删除post值中的空格:
<?php
# Make a function to remove spaces in the array. If the user types two empty
# spaces, it's considered not empty, so this will remove that and only leave
# actual string content (or empty).
function trimArray($array)
{
# Trim the value of empty spaces
if(!is_array($array))
# Send back since not an array
return trim($array);
# Loop through array and apply this same function
foreach($array as $key => $value) {
$array[$key] = trimArray($value);
}
# Return this array
return $array;
}
# Store your errors in an array
$missing = array();
# Check if the post is empty or not
if(!empty($_POST)){
# Remove empty spaces from values in the post
$POST = trimArray($_POST);
# Check email
if(!filter_var($POST["email"], FILTER_VALIDATE_EMAIL))
$missing[] = "A vaild email address is required.";
# Check if subject set
if(empty($POST['subject']))
$missing[] = "A subject field is required.";
# Check if content
if(empty($POST['content']))
$missing[] = "A content field is required.";
if(empty($missing)) {
# Send mail here
# Also check that it was successful...
}
}
?>
<!-- You only need one div that has a ternary condition. Set the class and message value -->
<!-- based on whether the $message array is empty or not -->
<form method="post" action="#">
<h1>Get in touch!</h1>
<?php
# You have no neutral condition so if there is no post, then it will
# still come back as saying it's successful.
# But that doesn't make any sense if no post was sent....
if(isset($_POST['email'])) { ?>
<div id="error" class="alert alert-<?php echo (!empty($missing))? 'danger' : 'success' ?>" role="alert">
<p><?php echo (!empty($missing))? implode('<br />',$missing) : "<strong>Thank you!</strong> Your message was sent, we'll get back to you ASAP!" ?></p>
</div>
<?php } ?>
是否启用了短标记?jQuery在这里的用法是什么?首先,这只是一个零散的变量或者{$complete;
-这里缺少代码,比如在(表单)元素输入和结束形式
标记中。所以我们不知道这是否有错。除了Chris的注释之外。直到我们确切知道是哪只动物我们正在处理这里,除了告诉您检查开发人员控制台并使用错误报告之外,任何人都无法为您做任何事情。如果(filter_var($_POST[“email”],filter_VALIDATE_email)===false)缺少此括号{$missing.=“需要有效的电子邮件地址。
”;$error=true;}提供完整的代码您是否启用了短标记?这里jQuery的用法是什么?首先,这只是一个杂散变量else{$Complete;
-这里缺少代码,如(表单)中所示元素输入和结束形式
标记。所以我们不知道这是否是错误。除了Chris的评论之外。直到我们确切知道我们要处理的是哪种动物之前,除了告诉您检查开发人员控制台并使用错误报告外,任何人都无法为您做任何事情。如果(filter_var($\u POST[“email”]、FILTER\u VALIDATE\u email)==false)缺少此括号{$missing.=“需要有效的电子邮件地址。
”;$error=true;}提供完整的Code@LouysPatriceBessette我不知道这是一个选项,谢谢你告诉我!@Louyspatricebesette编辑后将其包括在内。你每天都会学到新东西:D@LouysPatriceBessette我不知道这是一个选项,谢谢你告诉我!@Louyspatricebesette编辑了这个选项。你学到了一些东西每日新:D