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());
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.