tags:

views:

360

answers:

2

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

I don't know much at all about JQuery but am learning day by day so any detailed help offered would be greatly appreciated.

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

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

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>
+1  A: 

This may be what you're 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);
}
Jose Basilio
Hi Jose, Thanks for the help. What I have are 3 seperate files. The first line of my code is in one file and the second block is in a seperate file and of course my html is in its own file. If I use your code, I guess I'm wondering which file I would put it into.Thanks for the help!
Jose, IT WORKED!! :) You be da man, man.. :) I have been trying to get this to work for the longest time. Thanks so much!I have a set of checkboxes too. Will this same block of code work for them as well?
All right. Glad to be of help!
Jose Basilio
I tried the file with the 1st line. There is a comment in that file for form validation so I put it into the second file and viola! :)
A: 

How are you getting the data out of the database? Server-side script?

Assuming you do your database query via php from a MySQL DB, you might want to consider doing 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 really a simple bit, because it assumes you just want one customer's status. If you had several customers, you might go with...

$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 would give you an associative array of all of your customers and their status, which, printed out, would look like:

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

etc...

Having that, you would simply make your output. Since it's a boolean (they can either be active or inactive), I'd reccomend using a checkbox instead of 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 have that work out for your database results, you could 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>";

That last bit avoids the need to move the results into an array and just echos them straight out. Once you've got that loaded, you can use jquery to run some ajax on the table if you want to run updates. So maybe you change the statuses to reflect new activity, you check the box, a text field appears for the reason, and then you hit "update" and it passes the values to a server-side script that stores the new info without having to submit a for for the entire customer sheet.

Anthony
Hi Anthony, thanks for the response and the help. Jose, gave me exactly what I needed however, I am having an issue with a set of checkboxes that is totally unrelated to this form. They also use JQuery and are echoing the bool vals from the database. But what's happening with them is that when I change a value, and echo via alert(), I am getting undefined. Can you please tell me if your code will help me?
Well, I think my solution was more about me showing off, in retrospect. I get the impression your database is not being queried server-side. If you are doing the queries server-side, I'd think my code would at least take the strain off the browser for outputting the html and reflecting the values pre-User Interaction. As far as the undefined, that happens to me all of the time. With jquery, it's usually because I'm using a property or method that works in normal JS but not on the jquery version of the object. If the echo via alert IS using a server-side script...
Anthony
then my first question for my debugging would be "is the script returning anything at all?" Try changing the return value from something in the DB to just "Testing!" and see what comes back. You might be getting a response but it's not getting tied to whatever you are alerting out. Or it could be that you are alerting out a property like "val()" for a server-responding with a string (which would not be tied to a form input YET and thus isn't a val)... Follow the money!
Anthony
I was actually wondering why you were using server side to scho the radio button values. I figured that if I couldn't find a JQ answer I would just do it with php. All of the queries are done server side.
I do everything server-side that doesn't involve user-interaction or dynamic layout (like adjusting style values based on screensize, etc.). My server goes a lot faster then the average browser, and I can hide things as necessary. But if you have something more appy, like a user that checks off some user-names and gets feedback sans reload, that's a good use, of course. I lost interest in serious ajax implimentation when I realized that if I did an innerHTML and the sesson timed out, the div got a funny "Please Log Back In" instead of the entire page.
Anthony