-2

when i run this code it already says checked even if I haven't submitted any thing.. any help?? why the if statement isn't working

<?php 

if ( isset ( $_POST['roll'] ) && !empty($_POST['roll'])){
    echo 'checked' ;
}   
?>

<form action = 'test.php' method = 'POST'>
    <input type = 'submit' name  = 'roll' value = 'roll dice.'> 
</form>
sepp2k
  • 363,768
  • 54
  • 674
  • 675
Hussain Wali
  • 308
  • 3
  • 12
  • Where is the if statement? – Ragnar Dec 17 '14 at 20:50
  • put `print_r($_POST)` at the very first line of this page and see the values. – Riad Dec 17 '14 at 20:55
  • 1
    There is no problem with your code it works, just ensure that you are not refreshing the page after click submit button, because in that case the browser submits the content again, try opening the page in a new browser tab – Ragnar Dec 17 '14 at 21:00

4 Answers4

1

The if condition works as intended.

Although, you only need to call isset.

if (isset($_POST['roll'])) {
    echo "checked";
}

What you more or less did was write

if (isset($_POST['roll']) && isset($_POST['roll'])) {
    echo "checked";
}

which is redundant.

The reason why you see "checked" every time you refresh the page is because the "roll" query will continue to be in your browser's cache from the first time you submitted your form, until you decide to dump it. What I mean, is that this is an HTML issue, not a PHP issue.

Riad
  • 3,822
  • 5
  • 28
  • 39
verthandi
  • 74
  • 6
  • *"this is an HTML issue"* seems like a confused analysis to me. If the issue is to do with caching, as you claim, then certainly its a browser-related issue that isn't unique to PHP but it has nothing to do with HTML. – Mark Amery Dec 17 '14 at 22:32
  • Check my answer with [this](http://stackoverflow.com/a/8336492/4308587), [this](http://stackoverflow.com/a/8176940/4308587), or [this](http://en.wikipedia.org/wiki/Post/Redirect/Get) – verthandi Dec 18 '14 at 03:19
0

You never bothered checking if a POST was actually performed, so your code runs when the page is fetched (GET). YOu need something like this:

<?php

if ($_SERVER[['REQUEST_METHOD'] === 'POST') {
   ... validate here...
}
?>
... show form here ...
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • thanks... this works but what is wrong with isset( $_POST[])...? – Hussain Wali Dec 17 '14 at 21:02
  • well, your html is horrible. multiple spaces where they shouldn't be. `name=value`, not `name = value`. do a `var_dump($_POST)` with your original html and see what got received by php. but in general, checking for the presence/absence of a particular form field to decide if a post happened is not a good idea. request_method is 100% reliable. checking for the field is subject to human vagaries: typos, change the field name and forgetting to update your code, blah blah blah... – Marc B Dec 17 '14 at 21:05
  • There is no problem with the code, the html is not very good but I've tested the code and it works perfectly – Ragnar Dec 17 '14 at 21:07
  • i've added these spaces because of this stupid websites policies of adding spaces with code -_- – Hussain Wali Dec 17 '14 at 21:08
  • if you highlight your code in the editor and hit the `{}` key, it'll be formatted as code. e.g. indented by 4 spaces. then you can display all the html you want. – Marc B Dec 17 '14 at 21:09
  • Your answer does not account for the behaviour the OP claims to be seeing. He complains that `"checked"` is *always* echoed. That can't possibly be happening on a GET request, because on a GET request `$_POST['roll']` will not be set. – Mark Amery Dec 17 '14 at 22:35
-1

Try removing un-necessary white spaces. Make sure you are chekcking it from test.php. If you want to use it in same page that has forms in int, use either ? or # in action.

if(isset($_POST['roll']) && !empty($_POST['roll']))
{
    echo 'checked' ;
}   


<form action = 'test.php' method = 'POST'>
    <input type = 'submit' name  = 'roll' value = 'roll dice.'> 
</form>

But I use isset only

if(isset($_POST['roll']))
{
    echo "Checked";
}
-1

You can also try separating isset and empty checks. This will make sure that second condition of your code is executed only when 'roll' is set.

Something like this:

if(isset($_POST['roll']))
{
  if(!empty($_POST['roll'])
    echo 'checked' ;
}

user3252599
  • 811
  • 1
  • 5
  • 6