2

I have tried to get this to work but haven't been successful. I am a real noob with js so please keep that in mind when trying to help me. You'll have to spell it out for me because I just don't understand what is happening yet with all this code.

I have two things that I am trying to get working. One is a set of radio buttons. These buttons are a group meaning that only one radio button can be checked. The values that I have for these radio buttons are either 1 or 0 and are coming out of a database.

Here is the problem I am having. I can switch the buttons and change the vals in the db but when json calls the data out of the database, there is no change in the buttons to the user. For example: if I changed button A to true, or checked, the val in the db changes to 1. When that page is viewed from that moment on, button A should reflect that. Also, when the user checks B, the db val is then changed to 0 and the button should reflect that as well.

What I don't understand is how to switch the actual buttons to reflect the values in the database. Here is my code so far:

else if (item.field == "status" && item.value == "1"){
  $("radio#status").attr("checked", "checked");
}

<input type="radio" id="active" name="status" value="1" checked="checked" class="chkbx">
<input type="radio" id="inactive" name="status" value="0" class="chkbx">


Here is my second issue. The checkboxes are exactly the same as the radio buttons with the exception that they are NOT a group. Each checkbox has its own value. Again I am using 0 and 1 but multiple checkboxes can be checked at one time. Here is the code for that as well.

else if (item.field == "rptDly" && item.value == "1"){
  $("checkbox#rptDly").attr("checked", "checked");
}

<input type="checkbox" id="rptDly" name="rptDly" value="1" checked="checked" class="chkbx">
<input type="checkbox" id="rptSum" name="rptSum" value="1" checked="checked" class="chkbx">
<input type="checkbox" id="rptDtl" name="rptDtl" value="1" checked="checked" class="chkbx">

Can someone please guide me? When I put an alert in the js code above, I do get the alert so I know I'm good there but because I don't know js, I'm not able to troubleshoot this any further.

EDIT:

I found the original post that I am duplicating. It is here.

Community
  • 1
  • 1
  • Could you clarify what exactly you're expecting to happen, and what happens instead. Also, unless you are going to include all of the code between the database and the JS, don't mention the database. Narrow down your problem, and explain the code directly surrounding your question. – Andrey Fedorov Mar 27 '09 at 08:04
  • Also, please make this a wiki. – Andrey Fedorov Mar 27 '09 at 08:05
  • "when json calls the data out of the database" - JSON is a text format for encoding data. It cannot do anything to a database... – Andrey Fedorov Mar 27 '09 at 08:06
  • I know that JSON cannot do anything to the database. Part of my problem is that I don't understand what is happening on the client side. Server side I understand well. I'm sorry, I don't know how much better I can explain this problem. I need to echo the vals imported by JSON in the page. –  Mar 27 '09 at 08:48
  • Do I need to echo them with PHP? I have already tried that but I am not getting anything. I have tried to echo the POST vals with PHP AFTER JSON has fetched the data but I don't get anything. –  Mar 27 '09 at 08:50
  • Let me clarify.. When I say that I need to echo the vals imported by JSON, I mean that I need to "toggle" the radio buttons and checkboxes to "match" the imported JSON values. –  Mar 27 '09 at 08:52

3 Answers3

2

It seems you have mixed up what happens on the server, and what happens on the client. What is the item object you are refering to?

Also it's tag#id, where you use tag#name in your first code example.

Usually the code you're writing looks a bit like this (depending on serverside language andframework):

<input type="checkbox" id="forreferencinginjavascriptandcss" name="tobesenttotheserver" value="1"<% if someboolean then print " checked=\"checked\"" %>>

Note that the code between <% and %> gets run on the server, before sending anything to the client.

If you want to check/uncheck a radiobutton/checkbox after it's been sent to the browser, you can use jQuery to do it:

$("input#forreferencinginjavascriptandcss").attr("checked", true);

or

$("input#forreferencinginjavascriptandcss").attr("checked", false);

If a checkbox is checked, name=value is sent to the server, and if it's not checked nothing gets sent to the server. If you want to see what gets sent where, I recommend that you google for and download Fiddler2. It's a works as a http proxy and lets you see exacly what gets sent to and from the server.

Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
  • Ahh... So you mean that I need a condition to switch the checked value? That makes perfect sense. I was under the impression though that JQuery automatically did this for me. BTW: I'm using PHP for server side. –  Mar 27 '09 at 08:08
0

You need to set the "checked" property in javascript to true/false rather than the string "checked".

checked="checked" is an HTML convention for specifying boolean properties. In the javascript DOM this is converted to a true boolean

Gareth
  • 133,157
  • 36
  • 148
  • 157
-1

The syntax that you're using for the jquery selectors is a bit wrong.

Instead of...

$("checkbox#rptDly")

...the syntax for accessing checkboxes is...

$("input:checkbox[name='rptDly']")

But, as the checkboxes each have their own unique id attribute, you could simply use...

$("#rptDly")

Radio buttons are a bit trickier, as they all have the same name or id. These have to be distinguished by the value attribute - will investigate a correct solution for this and get back to you.

Brush up on your JQuery selector syntax on the JQuery documentation website Or buy a good book on the subject

belugabob
  • 4,302
  • 22
  • 22
  • Thanks for the help and reply. The last poster said that I needed a server side if condition. Is this correct or will JQuery handle the manipulation for me? I mean, what he said makes perfect sense. I'd just like to know if JQ will automatically do this for me. –  Mar 27 '09 at 08:30
  • Thanks for checking on the radio buttons for me. –  Mar 27 '09 at 08:30
  • Ok, there are 2 issues in your original post. The first is the syntax required to access and modify a particular radio button or checkbox. The second is how to make the page reflect the state of you database - a more complete example of your code would be a good place to start.. – belugabob Mar 27 '09 at 08:53
  • JQ doesn't 'automatically' do anything really - it's a very good library but, at the end of the day, it will only do what you tell it to. – belugabob Mar 27 '09 at 08:54
  • OK, I have posted the other piece of code that I have. This is the original post where I got all this code. I have textboxes that work great. Data is imported from JSON from my db into my textboxes but the radio buttons and checkboxes have me stumped. I'll post the link for you so you can see. –  Mar 27 '09 at 09:03
  • OK, here is the post from SO that I went off of to have my data imported. http://stackoverflow.com/questions/558445/dynamically-fill-in-form-values-with-jquery –  Mar 27 '09 at 09:04