Jump to content

MySQL with Ajax


dylan.bathurst

Recommended Posts

Please HALP!!!!The setupI have a form that the user inputs news events for their department. There is only about 8 fields on the form. (Nothing to crazy and complicated). In the form at the top I made a separate container that displays all the current news events.fort_hays_state_university_-_alumni___news_and_events_administration-20071109-113352.jpgEach event in the database has a field called archive and each value is defaulted to 0. When the administrator scrolls through the events box, I would like them to be able to click that link, and it will run an SQL statement to update its archive value to 1. (When the news events are displayed on the page, I have php look at that row, and if it is zero it will display in the news section. If it is 1 it won't display, and visa versa in the archive page.)I am using simple ajax to accomplish this without refreshing the page. The problemI am using a php loop to display the id, and title.

<?php	for($i = 0; $i<count($event); $i++) { ?>		<script type="text/javascript">		function ajaxFunction()		  {		  var xmlHttp;		  try			{			// Firefox, Opera 8.0+, Safari			xmlHttp=new XMLHttpRequest();			}		  catch (e)			{			// Internet Explorer			try			  {			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");			  }			catch (e)			  {			  try				{				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");				}			  catch (e)				{				alert("Your browser does not support AJAX!");				return false;				}			  }			}			xmlHttp.onreadystatechange=function()				  {				  if(xmlHttp.readyState==4)					{					document.admin_insert.title_<?php echo $event[$i][0]; ?>.value=xmlHttp.responseText;					}				  }				xmlHttp.open("GET","event_update.php",true);				xmlHttp.send(null);		  }		</script>		<div class="ind_event">		<h1><?php echo $event[$i][1]; ?></h1>		<?php echo $event[$i][2]; ?><br>		<a href="" onclick="ajaxFunction();">Click</a>		<em> to move to archive</em><br>		</div>	<?php } ?>

The event_update.php is as such:

<?phpinclude('/***/****/SQLManager.php');$database = new SQLManager;$database->select_db('web_dev');$sql = "UPDATE alumni_news SET archive = '1'WHERE id=???";$database->execute($sql);?>

What I'm not sure about is how to pass the dynamic name of each event to the event_update.php SQL script. I also wasn't sure how to explain this problem so it might be tricky to understand, but please ask question to help me help you help me. Thanks.

Link to comment
Share on other sites

Since you are using a get request you can add the ID onto the URL.xmlHttp.open("GET","event_update.php?id=<?php echo $event[$i][0]; ?>",true);You can access that on the PHP page using $_GET['id']. If you change the request to a post request then you send the ID and anything else using the xmlHttp.send command, and read it from $_POST instead of $_GET.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...