Jump to content

Checkbox checked and updating MySQL database help needed


lunged

Recommended Posts

I found a topic that helped, but isn't quite doing it for me yet…

http://www.phpfreaks.com/forums/index.php/topic,257587.msg1211752.html#msg1211752

 

I am attempting to make a simple restaurant list were you can check off places you have been.  I am using Jquery mobile, with PHP, AJAX and a MySQL database.

 

This is my first use of Ajax outside of basic tutorials and I'm not sure where I am going wrong.  Any help would be appreciated!

 

The main code:

<div data-role="fieldcontain">
	    <fieldset data-role="controlgroup" >
	    <?php
	    	$query  = "SELECT * FROM restaurants ";
	    	$result = mysql_query($query);
	    	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	    	{
	    	
	    	$id = stripslashes($row['id']);
	    	$restaurantname = stripslashes($row['restaurantname']);
	    	$visited = stripslashes($row['visited']);
	    ?>
	    	<input type="checkbox" name="res_<?php echo $restaurantname ?>" id="res_<?php echo $restaurantname; ?>" value="<?php echo $restaurantname; ?>" class="custom" onchange="ajaxFunction(<?php echo $id; ?>, this.checked);"
				<?php if ($visited == '1') {echo "checked";} ?> />
			<label for="res_<?php echo $restaurantname ?>"><?php echo $restaurantname ?></label>
	    <?php } ?>
	    </fieldset>
	</div>

 

The JS:

<script type="text/javascript">
	function ajaxFunction(uid, uvisited) {
		var ajaxRequest;  // The variable that makes Ajax possible!
			try{
				// Opera 8.0+, Firefox, Safari
				ajaxRequest = new XMLHttpRequest();
			} catch (e){
				// Internet Explorer Browsers
				try{
					ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e){
						// Something went wrong
						alert("Your browser broke!");
						return false;
					}
				}
			}
		uvisited = (uvisited==true ? "1" : "0");
		var url = "updatevisited.php?id="+uid+"&visited="+uvisited;

		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	}
</script>

 

The PHP:

<?php
include("dbinfo.inc.php");
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$uvisited=$_GET['uvisited'];
$uid=$_GET['uid'];

$queryUpdateVisited="UPDATE restaurants SET visited='$uvisited', timestamp='NOW()' WHERE id='$uid' ";
mysql_query($queryUpdateVisited) or die(mysql_error());
?>

You haven't said what errors you get, what unexpected output, or lack of output, etc. We can't guess where you're going wrong without more info.

 

I'm curious why you have all sorts of browser checks if you're using jquery. It's supposed to do that stuff for you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.