Why is PHP not allowing comparison? - php

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?

Have you tried changing your if statements to something like
<?php if(strtolower(trim($status)) == "yes") echo "checked";?>

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

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>

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.

Related

Radio buttons updating a database deletes the entry?

so I'm creating a small form where you can update the values of some fields pulled from a database.
Two of the fields I have however are acting wrong, when the page loads they display the information correctly but when you press 'submit' the fields in the MySQL table go blank. All the other fields update correctly including a checkbox, but not these two radio buttons.
Here's the code for it, I'm sorry to be asking this but does anyone see the error? I'm rather new with PHP so I'm not sure what to look for.
Snippet of the PHP Code (I can post the full code of it, but it's long):
<?php
if(isset($_POST['submit'])) {
$mbr=$_POST['mbr'];
$rec=$_POST['rec'];
$update = $dbconnect->query("UPDATE testing SET mbr='$mbr', rec='$rec'");
}
?>
HTML Code for the 2 radio buttons:
<input type="radio" name="mbr" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" <?php echo $noChecked; ?>>No
Nevermind I'm dumb and tired and missed the obvious solution, I forgot to put the values of the buttons in their code.
How they should be
<input type="radio" name="mbr" value="up" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" value="down" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" value="yes" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" value="no" <?php echo $noChecked; ?>>

Retrieve mysql value from php for radio buttons

As trivial as it can seems, I'm having problems retrieving values for radio buttons from MySql database through PHP. It's my first learning project so I'm trying my best
Question has already been asked but I found no useful answer.
The php code does a simple "Select *" so I retrieve all the fields.
This is the php code
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "A") { echo "true"; }?> value="A">A
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "B") { echo "true"; }?> value="B">B</br></br>
and I retrieve the values with mysqli_fetch_array().
This is the result:
As you can see the label retrieves the correct value, the radio buttons not.
I've already tried putting == instead of = and putting ' instead of " but I don't know why the checkbox "B" is checked, since Owner value is A.
Also, if there are any best practices which are better than this, you're welcome.
The HTML attribute checked should not get a value, its mere presence indicates that the radio button is checked. So do this:
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {echo "checked"}?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {echo "checked"}?> value="B">B
Or using the more compact short echo tag <?= .. ?> and ternary operator:
<label>Owner: <?=$row['Owner']?></label></br>
<input type="radio" name="Owner" <?=$row['Owner']=="A" ? "checked" : ""?> value="A">A
<input type="radio" name="Owner" <?=$row['Owner']=="B" ? "checked" : ""?> value="B">B
Note that you need double equals signs for comparisons.
try this code not = but use ==
<input type="radio" name="Owner" <?php if($row['Owner'] == "A") { echo "checked"; }?> value="A">
$gender=$row['gender'];
<input type="radio" name="gender" <?php if($gender=="Male"){?> checked="true" <?php } ?> />Male
<input type="radio" name="gender" <?php if($gender=="Female"){?> checked="true" <?php } ?>/>Female
$owner=$row['owner'];
$owners= ['A'=>'', 'B'=> ''];
$owners[$owner] = 'checked';
<input type="radio" name="Owner" <?php echo $owners['A']?> value="A">A
<input type="radio" name="Owner" <?php echo $owners['B']?> value="B">B
i think it's easy and simple and so useful if you have many values in DB.
i tried the answer given by #trincot but it give me errors so i have make little changes to improve the answer
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {?> <?php echo "checked";?> <?php }?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {?> <?php echo "checked";?> <?php }?> value="B">B

Radio buttons and $_POST

two problem i have:
first:
i want users see my form with no prechecked in radio buttons but it does when they see it at first time(as you see)(explain that i use checked to show user which radio he/she selected after pushing the button)
second:
why when i name submit button "select sex" and push it in the form, it doesn't echo "it's done" but when i name it "select" it works?! i want my submit name has two words.
and the codes:
<html>
<body>
<?php
if(isset($_POST['select sex']))
echo "it's done";
?>
<form name="input" action="" method="post">
<input type="radio" name="sex" value="male" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='male') echo 'checked'; else echo '';?>
"> Male<br />
<input type="radio" name="sex" value="female" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='female') echo 'checked'; else echo '';?>
"> Female<br />
<input type="submit" name="select sex" value="Submit" />
</form>
</body>
For checkbox, not mentioned checked attribute for any of the check box option.
For submit button, normally we are not using the space in field name and this is the best practice. Though you have used then you have written the wrong code. Please check below updated code line for if statement.
if(isset($_POST['select sex']))

Javascript, PHP, Saving field value

I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.

uncheck a checkbox when checking another

<?php
session_start();
if (count($_POST) > 0) {
$_SESSION['link'] = $_POST['link'];
}
?>
<form method="post">
Gmail: <input type="checkbox" name="link" value="gmail" id="gmail" <?php if ($_SESSION['link'] == 'gmail') echo "checked"; ?>>
Hotmail: <input type="checkbox" name="link" value="hotmail" id="hotmail" <?php if ($_SESSION['link'] == 'hotmail') echo "checked"; ?>>
<input type="submit" value="Spara">
</form>
Problem is if a box is checked you have to uncheck that then check another to change.
Is there a way so it unchecks the checked one when i check another? that sounds weird...
Thanks
You could just use Radio buttons instead, e.g:
<input type="radio" name="rdGroup1" value="John"> John
<input type="radio" name="rdGroup1" value="Jane"> Jane
There is a reason God created radio buttons. :)

Categories