0

guys i have problem with else , if i reload my page else show text before send value from form why ? else text aways visible. I want after send some option from checkbox php check and after checked do something .....

 <?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>
2
  • 1
    if you hit reload button, browser will send previous form data again.
    – Milan
    Commented Mar 7, 2016 at 16:14
  • When you you submit the form there's no guarantee that $_POST['pravilen'] will be set - checkbox data is only sent through with the HTTP Request if the checkbox is checked (ditto on radio buttons and textarea)
    – CD001
    Commented Mar 7, 2016 at 16:22

5 Answers 5

1

You can try

<?php
if ($_POST['pravilen'] == 'pravilen') {
  echo ('pravileneee');
}else{
  echo ('greshen');
}
 ?>
1
  • No, again else echo staying visible before php check link
    – r00t3r
    Commented Mar 7, 2016 at 16:20
1

checkbox data is only sent through with the HTTP Request if it is checked.

Therefore isset($_POST['pravilen']) is not checking whether the data has been POSTed but whether it has been checked and posted.

If you're simply wanting to check that the form has been POSTed, try:

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

You have to test to see if the request method is POST before using your code (Use this code below). Your else statement would always be showing "greshen" before even if you didn't submit since $_POST['pravilen'] would never be set if you never POST'ed (submitted).

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

Also, this will have to have a name as answer too for your checkbox:

name="pravilen"
1
  • Does the code solve your question? This code should only let your if statement run when you submit the form.
    – sgtcoder
    Commented Mar 7, 2016 at 16:26
0

Check if the button submit and then send values:

<?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>
0

Try,

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

if you reload, browser will send previous form data again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.