Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Shells Virtual Desktop
BMail.ag - Secure Email Service
Server.net
CPLicense.net
VPS Server
Buy VPN
Vultr
VMs for AI
HostDare
HostDare
ReliableSite White-Label Dedicated Hosting for Resellers
InterServer VPS
BMail.ag - Secure Email Service
Best VPN
High-Performance Bare Metal Server Solutions
Karvl.com
Server Mania Cloud Hosting
DataWagon Hosting
AlphaVPS Hosting
Evoxt.com
Clouvider
VPS Hosting with NVMe
Residential IPs in the US & 4G Mobile Proxies in EU & US with Unlimited Bandwidth
ReliableSite White-Label Dedicated Hosting for Resellers
Rabisu - Hosting Solutions
Shells Virtual Desktop
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.

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

  • bugrakocbugrakoc Member
    edited September 2017

    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 to x variable

    Thanked by 1WSS
  • bugrakocbugrakoc Member
    edited September 2017

    @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.

  • bapbap Member
    edited September 2017

    Link <= Here is the link.

    Thanked by 1WSS
  • doghouchdoghouch Member
    edited September 2017

    @bap said:
    Link <= Here is the link.

    What does this have to do with JS/Ajax?

  • bugrakoc said: @jetchirag using jquery for such a small task is definitely overkill and will increase loading time significantly on slower connections for no reason.

    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.

    if(document.getElementById("checkbox").checked) { do shit } should be fine.

    Currently I am using something similar to this

  • jQuery and AJAX are different.

  • @Bayu said:

    bugrakoc said: @jetchirag using jquery for such a small task is definitely overkill and will increase loading time significantly on slower connections for no reason.

    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.

    if(document.getElementById("checkbox").checked) { do shit } should be fine.

    Currently I am using something similar to this

    Why do you want ajax? I think you are confused between differences in both

  • BayuBayu Member
    edited September 2017

    bugrakoc said: jQuery and AJAX are different.

    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.

    jetchirag said: Why do you want ajax? I think you are confused between differences in both

    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.

Sign In or Register to comment.