New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Retrieve Checked Checkboxes Value from Database using AJAX
I have a php form where I have multiple list in checkbox format to be retrieved from MySQL database. Say for example:
<form>
<label><input type="checkbox" name="diskusage[]" value="gleen" />Glenn</label>
<label><input type="checkbox" name="diskusage[]" value="peter" />Peter</label>
<label><input type="checkbox" name="diskusage[]" value="louis" />Louis</label>
<label><input type="checkbox" name="diskusage[]" value="freddy" />Freddy</label>
<label><input type="checkbox" name="diskusage[]" value="mark" />Mark</label>
</form>
<br>
<div id="result"><b>Total Disk Usage from selected user is:</b></div>
And table in database:
+----+--------------+---------------+
| id | username | diskusage |
+----+--------------+---------------+
| 1 | glenn | 200 |
| 2 | freddy | 120 |
| 3 | peter | 400 |
| 4 | louis | 50 |
| 5 | mark | 380 |
+----+--------------+---------------+
Can anyone help, how to sum data usage from the selected checkbox to displayed automatically with AJAX?
I tried to googling to find tutorial about this, but mostly is using selectbox or text input as example.
Comments
You can use onclick="yourAjaxFunction('freddy')" on the checkbox but that won't register the keyboard selections. You can try onchange, but I haven't tried that one myself.
do
$('#checkbox').is(':checked');
on onclick event. If it is, add the value of input tox
variable@jetchirag using jquery for such a small task is definitely overkill and will increase loading time significantly on slower connections for no reason.
if(document.getElementById("checkbox").checked) { do shit } should be fine.
Link <= Here is the link.
What does this have to do with JS/Ajax?
Actually data in table is about +500 row, so I think using ajax may be better, although I don't understand much about programming. Usually I just follow the tutorial and similar examples which I found.
Currently I am using something similar to this
jQuery and AJAX are different.
Why do you want ajax? I think you are confused between differences in both
I don't know what the difference, as I don't know much about code. Because when I search for it, the word "ajax" is mentioned in the title.
When I search php & mysql tutorial/example, one of them I found like this:
https://www.w3schools.com/php/php_ajax_database.asp
Somewhat similiar as I needed, but the tutorial example is using dropdown instead using checkbox as input.