使用Strip标记并检查PHP中的空字段

使用Strip标记并检查PHP中的空字段,php,html,forms,Php,Html,Forms,所以我这里有一个表单,我想在上做一些php,这样如果字段是空的,它会说一旦你在加载到post页面后提交表单,很抱歉你没有输入日期。我还想添加条带标记,但是我不确定如何添加,如果它们出现在张贴墙上,或者是否出现在form.php中?我想使用带所有标签 我相信空验证是if(!empty(($date)){echo“对不起,您没有输入日期”},但我无法让它工作 如果您能帮助我,请提前感谢。我将发布form.php和下面的张贴墙 Form.php <!DOCTYPE HTML> <ht

所以我这里有一个表单,我想在上做一些php,这样如果字段是空的,它会说一旦你在加载到post页面后提交表单,很抱歉你没有输入日期。我还想添加条带标记,但是我不确定如何添加,如果它们出现在张贴墙上,或者是否出现在form.php中?我想使用带所有标签

我相信空验证是if(!empty(($date)){echo“对不起,您没有输入日期”},但我无法让它工作

如果您能帮助我,请提前感谢。我将发布form.php和下面的张贴墙

Form.php

<!DOCTYPE HTML>
<html> 
<head>

<link type="text/css" rel="stylesheet" href="../css/index.css" />
<link type="text/css" rel="stylesheet" href="../css/footer.css" />
<link type="text/css" rel="stylesheet" href="../css/header.css" />

<meta name="viewport" content="width=device-width" />
<title>Daily Dorm News</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
$('form').submit(function (e) {
    var value;

    // "message" pattern : from 3 to 150 alphanumerical chars

    value = $('[name="message"]').val();
    if (!/^[-\.a-zA-Z\s0-9]{3,150}$/.test(value)) {
        alert('Sorry, only alphanumerical characters are allowed and 3-150 character limit".');
        e.preventDefault();
        return;
    }

    // "name" pattern : at least 1 digit

    value = $('[name="name"]').val();
    if (!/\d+/.test(value)) {
        alert('Wrong value for "name".');
        e.preventDefault();
        return;
    }
});

});
</script>

</head>
<body>

<?php include 'header.php' ?>

<form action="index.php" method="get">
<div id="container">
Username:*<input type="text" name="name" pattern="{3,15}" title="Letters and numbers only, length 3 to 15" required autofocus><br>
E-mail:* <input type="email" name="email" maxlength="20" required><br>
Post:*<br>

<textarea rows="15" cols="50" name='message'></textarea>
</div>

Date this event took place: <input type="text" name='date' id="datepicker" > <br>
Your favorite website is: <input type="text" name="website">
Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <br>


<input type="reset" value="Reset">
<input type="submit">
</form>
<p> Fields with * next to them are required </p>
<p><a href="index.php">Posting Wall</a></p>
<div id="time">
<?php 

$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'The time at which you are posting is:'. date('h:i Y-m-d') ."\n";
?>
</div>
<?php include 'footer.php' ?>


</body>
</html>

每日宿舍新闻
$(函数(){
$(“#日期选择器”).datepicker();
$('form')。提交(函数(e){
var值;
//“消息”模式:从3到150个字母数字字符
value=$('[name=“message”]')。val();
如果(!/^[-\.a-zA-Z\s0-9]{3150}$/.test(value)){
警报('对不起,只允许使用字母数字字符,限制为3-150个字符');
e、 预防默认值();
返回;
}
//“名称”模式:至少1位
value=$('[name=“name”]')。val();
如果(!/\d+/.test(值)){
警报(“名称”的值错误”);
e、 预防默认值();
返回;
}
});
});
用户名:*
电子邮件:*
帖子:*
此事件发生的日期:
您最喜欢的网站是: 性别: 女性 男性
旁边带*的字段是必填字段

张贴墙

<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="../css/post.css" />
 <link type="text/css" rel="stylesheet" href="../css/heaader.css" />
<link type="text/css" rel="stylesheet" href="../css/footer.css" />

<meta name="viewport" content="width=device-width" />
<title>Daily Dorm News</title>
</head>

<body>
<h1>Daily Dorm News </h1>
<h2> You're best place for the latest dorm news from campus </h2>
<div id="container"> <?php if ( isset($_GET['name']) and preg_match("/^[A-Za-z0-9]+$/", $_GET['name']) ) {

    echo $_GET['name'];

} else {

    echo "You entered an invalid name!\n";

}

?><br>


Your email address is: <?php if ( isset($_GET['email']) and preg_match("/.+@.+\..+/i", $_GET['email']) ) {

    echo $_GET['email'];

} else {

    echo "You didn't enter a proper email address!\n";

}
?><br>
You Posted : <?php if ( isset($_GET['message']) and preg_match("/^[-\.a-zA-Z\s0-9]+$/", $_GET['message']) ) {

    echo $_GET['message'];

} else {

    echo "The message is not valid! The message box was blank or you entered invalid symbols!\n";

}
?>

This event happened :<?php echo $_GET["date"]; ?><br>
Your gender is:<?php echo $_GET["gender"]; ?><br>
Your favorite website is: <?php echo $_GET["website"]; ?><br>


</div>

<?php
/* [INFO/CS 1300 Project 3] index.php
 * Main page for our app.
 * Shows all previous posts and highlights the current user's post, if any.
* Includes a link to form.php if user wishes to create and submit a post.
*/

require('wall_database.php');

// Fetching data from the request sent by form.php  
$name = strip_tags($_REQUEST['name']);
$email = strip_tags($_REQUEST['email']);
$message = strip_tags($_REQUEST['message']);
$date = strip_tags($_REQUEST['date']);
$gender = strip_tags($_REQUEST['gender']);
$website = strip_tags($_REQUEST['website']);

$is_valid_post = true;
// Checking if a form was submitted
if (isset($_REQUEST['name'])){
 // Fetching data from the request sent by form.php  
$name = strip_tags($_REQUEST['name']);
$email = strip_tags($_REQUEST['email']);
$message = strip_tags($_REQUEST['message']);
$date = strip_tags($_REQUEST['date']);  
$gender = strip_tags($_REQUEST['gender']);  
$website = strip_tags($_REQUEST['website']);  


 // Saving the current post, if a form was submitted
 $post_fields = array();
 $post_fields['name'] = $name;
 $post_fields['email'] = $email;
 $post_fields['message'] = $message;
 $post_fields['date'] = $date;
  $post_fields['gender'] = $gender;
 $post_fields['website'] = $website;

 $success_flag = saveCurrentPost($post_fields);

}

//Fetching all posts from the database
$posts_array = getAllPosts();

?>


   <?php
   if(isset($name)) {
     echo "<h3>Thanks ".$name." for submitting your post.</h3>";
   }
   ?>
  <div id="logo"> Don't forget to tell all you're friends about Daily Dorm News! <br> The only dorm news website on campus!</div>

   <p id="received">Here are all the posts we have received.</p>
   <ul id="posts_list">
   <?php

   // Looping through all the posts in posts_array
   $counter = 1;

   foreach(array_reverse($posts_array) as $post){
    $alreadyPosted = false;
     $name = $post['name'];
     $email = $post['email'];
     $message = $post['message'];
     $date = $post['date'];
      $gender = $post['gender'];
     $website = $post['website'];

     if ($counter % 2==1)
       $li_class = "float-left";
     else
       $li_class = "float-right";

     if ($name == $_GET['name']) {
        $alreadyPosted = true;
     }
        echo '<div class=post';
          if ($alreadyPosted) {
            echo ' id="highlight"';
          }
        echo '>';
        echo '<li class="'.$li_class.'"><h3><span>'.$name.'</span> wrote a post.</h3></li>';
        echo '<li class="'.$li_class.'"><h3><span>'.$name.' email is: '.$email.'</span></h3></li>';
        echo '<li class="'.$li_class.'"><h3><span>'.$name.' wrote '.$message.'</span> wrote a post.</h3></li>';
        echo '<li class="'.$li_class.'"><h3><span>This event occured on '.$date.'</span></h3></li>';
        echo '<li class="'.$li_class.'"><h3><span>Your a  '.$gender.'</span></h3></li>';
        echo '<li class="'.$li_class.'"><h3><span>You said your favorite website is  '.$website.'</span></h3></li>';
        echo '</div>';
   }
   ?>

</ul>
</div>
<p id="submit"><a href="../index.php">Would you like to submit another post?</a></p>
<?php require 'footer.php' ?>

</body>
</html>

每日宿舍新闻
每日宿舍新闻
你是最新校园宿舍新闻的最佳地点

您的电子邮件地址是:应该是

if(empty(($date)) { 
    echo "sorry you did not enter a date"; 
} 
不是

if(!empty(($date)) { 
     echo "sorry you did not enter a date"; 
 } 
如果日期字段为空,第一个表达式将打印“对不起,您没有输入日期”。如果日期字段为而非空,第二个表达式将打印该消息

您可以尝试下面的代码。它将打印“这是一个空字符串”

<?php
$date = '';
if (empty($date)) {
    echo 'This is an empty string';
} 

if (!empty($date)) {
    echo 'The date is ' . $date;
}

您可以通过提交函数中的jquery进行检查

var datepicker=$("#datepicker").val();
if(datepicker=='') { alert('Please enter date'); return false; }

您可以发布您尝试过的javascript代码吗?我没有使用任何java来执行if-else语句或strip标记,只是尝试了php,我希望它也使用php。我知道它类似于if(!empty(($date)){echo“…”}还有条带标签我真的不确定有什么想法吗?我真的不确定,已经尝试了好几个小时,尝试获取日期选择器的值:
$(“输入:text[name=date]”).val();
。或者只是在该输入中添加
required
属性。此语句有什么问题发生了此事件:
表示意外(是的,我想要它,因此如果该字段为空,它会打印“对不起,您没有输入日期”。因此,我想要的第二个字段不正确。!empty()表示日期不是空的(即已填写)。是否要用empty()显示错误是true。请查看对我原始答案的编辑。您可以使用示例代码查看empty()和!empty()之间的差异。