$myField = (isset($_POST['myField'])) ? 1 : 0; - always takes 1 never 0

Hello, I think I am trying to understand how to change a RadioButton value during an UPDATE transaction, not Insert, not Retrieve.

With an INSERT INTO I can insert a “0” or a “1” into a table field by means of a RADIO BUTTON like so:

PageInsert.php:


<form  name="form1" id="form1" method="post" action="PageInsert_Process.php?myID=<?php echo $myID ?>">

<input type="radio" name="OPT1" id="OPT1 value="1" /> (Yes)
<input type="radio" name="OPT1" id="OPT1" value="0" /> (No)
<!-- etc... -->

Then in
PageInsert_Process.php?myID=99 :

[php]
//stuff here

$OPT1 = $_POST[‘OPT1’];

//stuff here

// INSERT INTO query here, etc.
[/php]

The above works as expected and the RADIOBUTTON field (OPT1) is INSERTED with a 0 or 1 respectively.

For the same table from above, I also have an UPDATE form, as follow:

PageUpdate.php?myID=99 :

<form  name="form1" id="form1" method="post" action="PageUpdate_Process.php?myID=<?php echo $myID ?>">

<input type="radio" name="OPT1" id="OPT1 value="1" <?php if ($OPT1 == 1) { echo " checked"; }?>/> (Yes)
<input type="radio" name="OPT1" id="OPT1 value="0" <?php if ($OPT1 == 0) { echo " checked"; }?>/> (No)
<!-- etc... -->

So the above form show me the CURRENT status(value) of the RADIOBUTTON as it is in the table.

With this same update form, I can ofcourse change the RADIOBUTTON from Yes(1) to No(0) or vice-versa.

The problem I have is that when I do an UPDATE, it always takes the “1” value, never the “0”.

PageUpdate_Proccess.php?myID=99 :

[php]
//stuff here

$OPT1 = (isset($_POST[‘OPT1’])) ? 1 : 0;

//stuff here

// UPDATE query here, etc.
[/php]

Yes I am new to php/mysql.

Please point me in the right direction.

Thank you kindly,
Jamie.

First I don’t know why you using php inside the radio buttons, but I guess I really don’t want to know. :stuck_out_tongue:

Anyways, why don’t you do the following: (BTW YOU had a missing "):

[php] <input type=“radio” name=“OPT1” id=“OPT1” value=“1” <?php if ($OPT1 == 1) { echo " checked"; }?>/> (Yes)
<input type=“radio” name=“OPT1” id=“OPT2” value=“0” <?php if ($OPT1 == 0) { echo " checked"; }?>/> (No)[/php]

then you could simply do this:

[php]$OPT1 = (isset($_POST[‘OPT1’]) && $_POST[‘OPT1’] == 1) ? 1 : 0;[/php]

That should work, but you might have to put quotes around the 1, for I been coding with PHP and JQuery - I sometimes get my languages scrambled. ;D

@Strider64

[php]$OPT1 = (isset($_POST[‘OPT1’]) && $_POST[‘OPT1’] == 1) ? 1 : 0;[/php]

Thank you. That worked.

The missing " was a just a typo in this forum post, not in my actual php page, but thanks for making me double-ckeck :slight_smile:

You said: “…I don’t know why you using php inside the radio buttons…”

Is there another way ?

I mean, the value in the table field is either a 1 or a 0, represented by a radio button on the form, and on the UPDATE form I want the user to see the form in the exact same way as he did in the INSERT form - with the radio buttons.

So in the Update form I need to show the radio button, where either the “Yes” radio button or the “No” radio button is “checked/set/on”.

If not with this…
[php]
<input type=“radio” name=“OPT1” id=“OPT1” value=“1” <?php if ($OPT1 == 1) { echo " checked"; }?>/> (Yes)
<input type=“radio” name=“OPT1” id=“OPT2” value=“0” <?php if ($OPT1 == 0) { echo " checked"; }?>/> (No)
[/php]

… then how do I “display” the radio button’s status from the database value on the update form ?

Unless of course I am completely missing your point due to old age and a lack of sleep on my part.

Anyway, your solution to my original question worked, and I thank you for that.

Have a good day.
Jamie.

It got me thinking when you said was there another way, I’m probably thinking client-side (jQuery/JavaScript)…me bad. ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service