views:

131

answers:

4

I'm using PHP to read if an entry in my table on the database is set to "yes" or "no" and auto check the radio button that corresponds:

<?php include 'file.php';
$query = "SELECT * FROM TABLE";
$runquery = odbc_exec($connect,$query);
$status= odbc_result($runquery,"status");
odbc_close($file);
?>
<form>
<div class="formContainer">
    <fieldset>
    <legend>Campus Alert<span class="tooltip">Turn campus alert on and off.</span></legend>
        <?php echo $status; ?>
        Yes <input type="radio" name="alertStatus" id="alertStatus" value="yes" <?php if($status== "yes") echo "checked";?>>
        No <input type="radio" name="alertStatus" id="alertStatus" value="no" <?php if($status== "no") echo "checked";?>>
    </fieldset>
</div>

the <?php echo $status; ?> is for debugging so I can make sure what the database says and the form's reaction is correct. It prints "yes" (no quotes). However, the if statement will not respond. Any idea why it's doing this?

A: 

Wow, that's really weird... the problem isn't with your PHP. First of all, You should remove the id attributes from those fields. But the real issue is that Firefox doesn't seem to want to check the second field when it has the name alertStatus. if you change the name to something else, it seems to be working. I'm not really sure why this is though.

Here's my test code:

<?php //include 'file.php';
//$query = "SELECT * FROM TABLE";
//$runquery = odbc_exec($connect,$query);
//$status= odbc_result($runquery,"status");
//odbc_close($file);
$status='no';
?>
<form>
<div class="formContainer">
    <fieldset>
    <legend>Campus Alert<span class="tooltip">Turn campus alert on and off.</span></legend>
        <?php echo $status; ?>
        Yes <input type="radio" name="alertStatu" value="yes" <?php if($status== "yes") echo "checked";?>>
        No <input type="radio" name="alertStatu" value="no" <?php if($status== "no") echo "checked";?>>
    </fieldset>
</div>
Chibu
if i set $status to a specific value like yes or no by doing $status = "yes" or $status = "no" it works. It only breaks when I set the value by pulling off the DB. I went into my code and change nothing other than that and it works.
dcp3450
This isn't to do with with the specific name of the field e.g. "alertStatus". When you refresh the page Firefox remembers the previous data from your form fields and restores the form to this state, overriding the "checked" that is in the HTML source. You need to do a full refresh (Ctrl+F5) instead. This is why renaming the field appeared to help because Firefox doesn't have a remembered value for the new name.
mikej
Oh... Well I guess I fail that one XD. Good to know though, since that didn't make any sense. Thanks for pointing that out >_<
Chibu
+3  A: 

Have you tried changing your if statements to something like

<?php if(strtolower(trim($status)) == "yes") echo "checked";?>
R. Bemrose
trim() did the trick.
dcp3450
The fact that `trim` did the trick implies there is leading or trailing white space in the database field. It is probably worth investigating where this is coming from.
mikej
That's why you must debug with var_dump() rather than echo
Álvaro G. Vicario
A: 

If you look at the page source you'll see that the check is actually there. However, you have a duplicate ID and the browser is getting confused. Replace:

Yes <input type="radio" name="alertStatus" id="alertStatus" ....>
No <input type="radio" name="alertStatus" id="alertStatus" .....>

with

Yes <input type="radio" name="alertStatus" id="alertStatus:yes" ....>
No <input type="radio" name="alertStatus" id="alertStatus:no" .....>

and it'll fix.

Álvaro G. Vicario
+1  A: 

It's not very good practise to use "yes/no" for your $status, you're better off using a int/boolean value.

Nick Knight