获取PHP变量以在HTML Div中显示

时间:2017-08-02 03:40:34

标签: php jquery html

尝试将$ missing的错误消息显示在带有id错误的表单div中,如果表单填写正确,则将显示$ complete消息显示到id成功的div中。

<?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;
    }
} 
?>

这是试图显示的HTML表单。

<form method="post">
     <h1>Get in touch!</h1>      
     <div id="error" class="alert alert-danger" role="alert">
          <? echo $missing; ?></div>
     <div id="success" class="alert alert-success" role="alert"><? echo $complete; ?></div>

无法真正看到我在哪里出错,任何帮助都会令人惊叹。感谢。

4 个答案:

答案 0 :(得分:2)

<someNode value="<%= getUuid() %>"> 在PHP中没有任何意义(除非您在<?文件中启用了short open tags)。不鼓励使用它,因为它可以被禁用 - 也许这就是这里发生的事情。

还有另外两个PHP的起始标记,php.ini<?php(后者仅用于回显,我怀疑你要使用它)。

尝试将<?=替换为<? echo $missing; ?>,将<?=$missing?>替换为<? echo $complete; ?>

此外,您永远不会将<?=$complete?>设置为包含任何值,并且有些情况下根本不会定义它,这会导致错误。

您应该在PHP代码的顶部将其定义为空(就像使用$complete一样),然后在$missing语句中将某些值分配给它,即else

答案 1 :(得分:1)

我认为不应该(因为当你提交表格时,它总是张贴,所以你必须检查它是否为空白)

if (!$_POST['subject'])    
&&  if(!$_POST['content'])
( -it means if data is not posted)

应该是

if($_POST['subject']=='')   
if($_POST['content']=='')'
( - it means if data is blank)

在HTML页面中 你应该使用

<?php echo $missing; ?> or <?=$missing;?>
<?php echo $complete; ?> or <?=$complete;?>

答案 2 :(得分:1)

请参阅代码中的注释。

<?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.
  }
} 

?>

答案 3 :(得分:0)

如果没有看到你的表格,很难在那里解决任何问题,但是如你所知,做一些返工会更好。您不需要$error标记,您应该将错误存储在数组中。如果数组末尾的计数大于1,那么就会出现明显的错误。在确定值实际上是否为空之前,您还应首先删除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 } ?>