0

I'm trying to check a check box, if the value for that field is 1 in the database.

I have:

<?php 

$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];

if ($selectedSPK) {
    $Priorityquery = "SELECT  Priority FROM Data WHERE SPKCustNo  = '$selectedSPK' ";
    $Priorityresult = mysql_query($Priorityquery);
    $row = mysql_fetch_array($Priorityresult);
    $checked = $Priorityresult['Priority'];
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1" 
<?php if ($checked == 1) echo ' checked'; ?> />

but not getting any joy, any ideas?

1
  • you aren't using the result data... Commented Oct 3, 2012 at 11:52

6 Answers 6

1

Try this:

You were not using the row returned by the query...

<?php 
    $selectedSPK=$_POST['SPKSelect'];
    $assigned = $_POST['Sales_Exec'];
    $date = $_POST['DateSelect'];
    if ($selectedSPK)
    {
        $Priorityquery = "SELECT  Priority FROM Data WHERE SPKCustNo  = '$selectedSPK' ";
        $Priorityresult = mysql_query($Priorityquery);
        $row = mysql_fetch_array($Priorityresult);
        //$checked = $Priorityresult['Priority']; // <------ this is where you went wrong...
        $checked = $row['Priority']; // <------ this will fix where u went wrong!
    }
    ?>
    <input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1){echo ' checked'; }?>
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, thanks a lot Chris, new to this so it's tricky to get this stuff right, worked like a charm though, much appreciated, and thanks for showing me where I went wrong.
No worries, we all started somewhere :)
1

You should use

<?php if ($checked == 1){echo "checked='checked'"; }

and also

$checked = $Priorityresult['Priority']; 

to

 $checked = $row['Priority'];

Comments

1

I think you have one mistake... Try this

<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($row['Priority'] == 1) echo ' checked'; ?> />

Comments

0

Try it this way

<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1) echo "checked='checked'"; ?> />

Comments

0

change

<?php if ($checked == 1) echo ' checked'; ?>

to

<?php if ($checked == 1) echo ' checked="checked"'; ?>

and $checked = $Priorityresult['Priority']; to $checked = $row['Priority'];

Comments

0

It should be checked="checked"

<?php if ($checked == 1) echo "checked='checked'"; ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.