我有以下表格:
<form action="#" method="POST" enctype="multipart/form-data">
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info"
name="submit_form" value="Click to start chat! " />
它的工作方式是用户可以从gender
和age
中选择两个选项之一并搜索用户。两个选项都不需要完成搜索。
但是,如果没有选择任何选项,我希望它保留在当前页面(`random_chat.php),并回显一条消息。
这是我的方法:
$refined_gender = isset($_POST['gender']) ? escape($_POST['gender']) : '';
$age_from = isset($_POST['age_from']) ? escape($_POST['age_from']) : '';
$age_to = isset($_POST['age_to']) ? escape($_POST['age_to']) : '';
if (isset($_POST['submit_form'])){
if (empty ($refined_gender) || empty($age_from) || empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
目前,当我有两个搜索选项为空时,消息回声,这是好的。但是当我正在寻找男性用户,或者年龄不是空的时候,这个消息也会回响,这不应该发生。
答案 0 :(得分:1)
您需要将class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :slugging, use: [:slugged, :history, :finders]
def slugging
Digest::SHA1.hexdigest("#{title} #{id}")[0..8]
end
end
更改为||
,(其他一些更改也会说明为评论): -
&&
答案 1 :(得分:0)
if条件没有反映出你想要修复它的内容
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else { // search user }