1

I'm relatively new to coding in PHP with MySQL. Here's the problem: I want a radio button selected based off on the result I'm getting from my query. For simplicity purposes, I'm going to only select the field that I need to do the task. Overall, the entire site is a repair system for a local computer shop that I work at, and it is my senior project for a class I'm taking at my college. In the database among other fields, there is a adapterType that has the value of "cust" or "csc" for when customer's bring in their computer (mainly a laptop) and if we are using their power adapter or ours.

The query is

    $query =   "SELECT adapterType FROM repair WHERE repairID = '$repairID'";
    $result = mysqli_query($mysqli, $query);
    list($adapter) = mysqli_fetch_row($result);

If I echo out the value of adapter, I get the value that I want.

On another page, I have a list of repairs and next to each is an update button which loads the repair based off of the repairID. On the repairUpate page, I have various text fields that hold certain values that pertain to that field in the repair table. I also have a the radio buttons for the adapter type. I have

    <input type=\"radio\" name=\"adapter\" value=\"csc\"> CSC A/C
    <input type=\"radio\" name=\"adapter\" value=\"cust\"> Cust A/C

How can I have the correct radio button be selected or "checked" based on the value of $adapter that I get from the query?

I know this was a very long post, but I had to explain everything. Thank you for your patience.

3
  • <input type=\"radio\" name=\"adapter\" value=\"csc\" <?php echo ($adaptor=="csc") ? "checked":""?>> CSC A/C <input type=\"radio\" name=\"adapter\" value=\"cust\"<?php echo ($adaptor=="cust") ? "checked":""?>> Cust A/C Commented Dec 3, 2017 at 16:37
  • Your code is vulnerable to SQL injections. Please learn to use prepared statements instead. Commented Dec 3, 2017 at 16:54
  • @tereško while updating to prepared statements is best, saying the OP is open to sql injection is currently speculation as they may be using mysqli_real_escape_string(), or other escaping, on $repairID. Commented Dec 3, 2017 at 17:02

1 Answer 1

2

A simple ternary will allow you to set checked="checked"

$checked = ($adapter == 'csc') ? 'checked="checked"' : '';

Where you have multiple buttons, you can created them in a loop, checking the selected -

$array = array("csc"=> "CSC A/C", "cust" => "Cust A/C");
foreach($array as $value => $name) {
   $checked = ($adapter == $value) ? 'checked="checked"' : '';
   echo "<input type=\"radio\" name=\"adapter\" value=\"{$value}\" {$checked}> {$name}";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Before I wrote my post, I saw other answers that were similar to the one you posted, but they didn't work. Yours did though, thank you!

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.