JQuery Help and Radio Buttons

I have data that is being imported from a database and I would like to represent this data in a set of radio buttons. If the value in the database is 1, the "client status" should show the "active" radio button, also show the "inactive" button.

I am not very good with JQuery, but I am learning day by day, so any detailed help would be appreciated.

I took the main code snippets from my files and included them here.

The interesting thing about the code I am posting is that I can use buttons to change the data in the database. This works, but showing what is in the database is not.

var admCustStatus = $("input[name='admCustStatus']:checked").val();


if (item.field == "admCustStatus")
{
   // ?? radio buttons
}

<tr>
    <td class="admMarker">Active<input type="radio" id="admCustStatusActive" name="admCustStatus" value="1" checked="checked" class="admChkbx"></td>
    <td class="admMarker">Inactive<input type="radio" id="admCustStatusInactive" name="admCustStatus" value="0" class="admChkbx"></td>
</tr>

      

0


a source to share


2 answers


This might be what you are looking for:



if (item.field == "admCustStatus")
{
  if(item.value == 1) //assuming this is how you get the value
   $("#admCustStatusActive").attr('checked',true);
  else
   $("#admCustStatusInactive").attr('checked',true);
}

      

+1


a source


How do you get data from the database? Server side script?

Assuming you are doing your database query via php from a MySQL database, you might need to do something like ...

$cust_query = "SELECT status FROM custDB WHERE cust_name = '$cust'";
$cust_result = mysql_query($cust_query);

while ($row = mysql_fetch_assoc($cust_result)) {
      $cust_status = $row['status'];
      }

      

Now, this is a really simple bit, because it assumes you only need one client status. If you have had multiple clients, you can go with them ...

$cust_query = "SELECT cust_name, status FROM custDB ORDER cust_name";
$cust_result = mysql_query($cust_query);

while ($row = mysql_fetch_assoc($cust_result)) {
      $cust_status[$row['cust_name'] = $row['status'];
      }

      

This will give you an associative array of all your customers and their status, which will be printed out like this:

     [Jones] => 'Active',
     [Smith] => 'Inactive'

      



etc...

After that, you will simply draw your conclusion. Since it is logical (they can be active or inactive), I would recommend using a checkbox instead of a radio. This is the basic HTML:

   <table>
      <tr><td>Jones</td><tr><input type="checkbox" checked="checked" value="active" /></td></tr>
     <tr><td>Smith</td><tr><input type="checkbox" value="active" /></td></tr>
  </table>

      

And to get this to work for your database results, you can go with ...

 echo <<<EOT
 <table id="cust_status">
 <tr><th>Customer Name</th><th>Status</th></tr>
 EOT;

 while($row = mysql_fetch_assoc($cust_results)) {
       $status = ($row['status'] == 'active') ? 'checked="checked"' : "";
       $cust_name = $row['cust_name'];
       echo <<<EOT
       <tr><td>$cust_name</td><tr><input type="checkbox" $status value="active" /></td></tr>
      EOT;
  }
  echo "</table>";

      

This last bit avoids having to move the results into an array and just push them straight out. Once you load this, you can use jquery to trigger some ajax on the table if you want to trigger updates. So maybe you change the statuses to reflect the new activity, you check the box, a text box appears for that reason, and then you hit "refresh" and pass the values ​​to the server-side script that saves the new information without having to apply for the entire client sheet ...

0


a source







All Articles