否则无法正常工作

时间:2016-03-07 16:05:06

标签: php forms

大家好我有问题,如果我重新加载我的页面其他显示文本之前从表单发送值为什么?其他文字可见。我希望从复选框php检查发送一些选项后,检查后做一些事情.....

 <?php
if (isset($_POST['pravilen'])) {
  echo ('pravileneee');
}else{
  echo ('greshen');
}
 ?>



<form action="" method="post" role="form">
   <div class="col-md-12">
     <h1 class="gameh1">Моля отговорете на нашите въпроси за да продължите</h1>
 <div class="alerts alert-info">
   dsadsadass
 </div>
 </div>
   <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check1"  name="pravilen" value="pravilen" onclick="selectOnlyThis(this.id)"> Pravilen</br></div>
 </div>
 <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check2"  name="answer" value="greshen" onclick="selectOnlyThis(this.id)"> greshen</br></div>
 </div>
 <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check3"  name="answer" value="greshen" onclick="selectOnlyThis(this.id)"> greshen</br></div>
 </div>
 <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check4"  name="answer" value="greshen" onclick="selectOnlyThis(this.id)"> greshen</br></div>
 </div>
  <div class="form-group col-md-12">
   <input type="submit" class="questionbtn" value="Submit">
 </div>
 </form>

5 个答案:

答案 0 :(得分:1)

你可以尝试

<?php
if ($_POST['pravilen'] == 'pravilen') {
  echo ('pravileneee');
}else{
  echo ('greshen');
}
 ?>

答案 1 :(得分:1)

checkbox数据仅在检查时通过HTTP请求发送。

因此,isset($_POST['pravilen'])不检查数据是否已过帐,但是否已检查

如果您只是想检查表单是否已张贴,请尝试:

<?php
if (!empty($_POST)) {
    echo ('pravileneee');
} else {
    echo ('greshen');
}
?>

答案 2 :(得分:0)

检查按钮是否提交然后发送值:

<?php
if(isset($_POST['send'])){
if (isset($_POST['pravilen'])) {
  echo ('pravileneee');
}else{
  echo ('greshen');
}
}
 ?>



<form action="" method="post" role="form">
   <div class="col-md-12">
     <h1 class="gameh1"></h1>
 <div class="alerts alert-info">
   dsadsadass
 </div>
 </div>
   <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check1"  name="pravilen" value="pravilen" onclick="selectOnlyThis(this.id)"> Pravilen</br></div>
 </div>
 <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check2"  name="answer" value="greshen" onclick="selectOnlyThis(this.id)"> greshen</br></div>
 </div>
 <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check3"  name="answer" value="greshen" onclick="selectOnlyThis(this.id)"> greshen</br></div>
 </div>
 <div class="form-group  col-md-6 col-sm-12">
   <div class="question"><input type="checkbox" id="check4"  name="answer" value="greshen" onclick="selectOnlyThis(this.id)"> greshen</br></div>
 </div>
  <div class="form-group col-md-12">
   <input type="submit" class="questionbtn" value="Submit" name = "send">
 </div>
 </form>

答案 3 :(得分:0)

在使用代码之前,您必须先测试请求方法是否为POST(请使用下面的代码)。你的其他声明将始终显示&#34; greshen&#34;之前即使你没有提交,因为如果你从未发帖(提交),$ _POST [&#39; pravilen&#39;]永远不会被设置。

if($_SERVER['REQUEST_METHOD']=='POST'){
    //Put your if statement here that you wrote
    if (isset($_POST['pravilen'])) {
        echo ('pravileneee');
    }else{
        echo ('greshen');
    }
}

此外,对于您的复选框,还必须有一个名称作为答案:

name="pravilen"

答案 4 :(得分:0)

尝试,

$result = $_POST['pravilen'] == 'pravilen' ? echo ('pravileneee') : echo ('greshen');

如果您重新加载,浏览器将再次发送以前的表单数据!