Ajax Jquery and Get Variables

Ajax, Jquery and GET Variables

This example uses jquery to make an Ajax call via the 'GET' method. This method is coded almost identically to the previous 'POST' sample. For example, when you select the checkbox, the Jquery change() function is triggered.

After that, if the check box is checked, a variable = 1 is created. If it is deselected, the variable becomes = 2. Then, asyncronous ajax takes places with the proper data and url. Essentially, new get variables are created when you click the checkbox. Finally, the get variables are used to update the database table with basic php / mySQL.

<?php
header('Cache-Control: no cache'); //no chache but no error
session_start();
include ("connect.inc");
$db=public_db_connect();
?>

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
});
//alert("hi");
	$(document).ready(function(){
	
		$("input[name*='mycheckbox']").change(function(){
		
			var variable = $(this).attr("id");
			
			if ($(this).is(':checked')) {
			
			var variable2 = 1;
			
			} else {
			var variable2 = 2;
			}
						
			alert(variable);
			
			alert(variable2);
			/*var dataString = 'variable=' + variable + '&variable2=' + variable2;*/
			$.ajax({
				type: "GET",
				cache: false,
				url: "ajax-get-jquery.php",
				data: "variable="+variable +"&variable2="+variable2,
				/*data: { variable2: "variable2" },*/
				/*data: dataString,*/
				success: function(msg){
					alert("Success!");
					/*alert(variable2);*/
				}
			});
			
		});
	});
</script>
  	</head>
	<body>
	<?php
	$command = "SELECT * FROM ajax";
	$result = mysqli_query($db, $command);
	while($row = mysqli_fetch_assoc($result)){
	}

	$variable2 = $_GET['variable2'];
    $variable = $_GET['variable'];

$command = "SELECT * FROM ajax";
	$result = mysqli_query($db, $command);
	while($row = mysqli_fetch_assoc($result)){
	//echo $row['checking'];
	}

if($_GET['variable2']){
echo $variable2;
	$commandb = "UPDATE ajax SET checking='{$_GET['variable2']}' WHERE id=1";
	$resultb = mysqli_query($db, $commandb) ; 
	}	else{
	$commandc = "UPDATE ajax SET checking=3 WHERE id=1";
	$resultc = mysqli_query($db, $commandc) ; 
	}
	?>	
	
	<form method ="post" action = "">
<input type= "checkbox" name="mycheckbox" id="checker" value="1" />
</form>