0

I am new to javascript and am trying to use the following code to input a value into the database using php and javascript.

When the checkbox is checked, all the code works, except for the "load('../check.php?checked=Y');". I want that to input the "Y/N" into the database. I thought "load" would do the trick but it didn't. What other function would I use?

I appreciate the help!

if (checkbox.checked)

{

$('#btn').show();$('#hide').show();$('#count').show();$('#message').show();$('#notify').show();$('#responds').show();

load('../check.php?checked=Y');



}else{
  $('#btn').hide();$('#hide').hide();$('#count').hide();$('#message').hide();$('#notify').hide();$('#responds').hide();

load('../check.php?checked=N');}

}
Sam J.
  • 353
  • 1
  • 3
  • 12
  • 3
    you want [.ajax](http://api.jquery.com/jQuery.ajax/) or [.post](http://api.jquery.com/jQuery.post/), [.load](http://api.jquery.com/load) just loads in data from that url it doesnt send (except in the GET variables). You can do it with load with the GET url variables but its better to use the proper functions. – Patrick Evans Jan 27 '14 at 22:21
  • 1
    Just the mere question title suggests it's going to be a bad question. Why do you start learning by mixing 3 technologies you don't know? Just learn one by one! – Tomas Jan 27 '14 at 22:22
  • @Tomas I know PHP very well and am now learning Javascript. – Sam J. Jan 27 '14 at 22:36

3 Answers3

2

You need to look into $.ajax() or the shorthand counterpart $.post()

http://api.jquery.com/jquery.post/

$.ajax({
    type: "POST",
    url: "load.php",
    data: {"checked" : "Y"},
    success: function( d ){
        console.log( d ); // returned by script on server
    }
});

This assuming you have some script in load.php on your server to actually handle the data and insert into the database.

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
1

You can use $.ajax to communicate between JS and the server (PHP) without refreshing the page:

$.ajax({
            url: 'check.php?checked=N',
            type: 'get',
            success: function(data) {
             alert('great success!')
            }
});

Hope this helps!

dev7
  • 6,259
  • 6
  • 32
  • 65
1

Here are some tutorials that will help:

phpAcademy - Register and Login - OOP version
phpAcademy - Register and Login - Procedural version

theNewBoston - Project Lisa (adv)

From your code, I think you will be most comfortable with the procedural tutorial from phpAcademy, but you should really learn the OOP one.


Some tutorials for assistance with AJAX:

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111