I am trying to send a php script some content to be stored in a database via ajax. I am using the jQuery framework. I would like to use a link on a page to send the information. I am having trouble writing the function that will send and receive the information, everything that I have tried is asymptotic.
EDIT The idea is that the user will click the link, and a coulumn called "show_online" (a tiny int) in a table called "listings" will update to either 1 or 0 (**a basic binary toggle!) On success, specific link that was clicked will be updated (if it sent a 1 before, it will be set as 0).
EDITThere will be 20-30 of these links on a page. I have set each containing div with a unique id ('onlineStatus'). I would rather not have a separate js function for every instance.
Any assistance is much appreciated. The essential code is below.
<script type="text/javascript">
function doAjaxPostOnline( shouldPost, bizID ){
load("ajaxPostOnline.php?b='+bizID+'&p='+shouldPost", jsonData, callbackFunction);
function callbackFunction(responseText, textStatus, XMLHttpRequest)
{
// if you need more functionality than just replacing the contents, do it here
}
}
}
</script>
<!-- the link that submits the info -->:
<div id='onlineStatus<?php echo $b_id ?>'>
<a href='#' onclick="doAjaxPostOnline( 0, <?php echo $b_id ?> ); return false;" >Post Online</a>
</div>
ajaxPostOnline.php
<!-- ajaxPostOnline.php ... the page that the form posts to -->
<?php
$id = mysql_real_escape_string($_GET['b']);
$show = mysql_real_escape_string($_GET['p']);
if( $id && ctype_digit($id) && ($show == 1 || $show == 0) ) {
mysql_query( "UPDATE listing SET show_online = $show
WHERE id = $id LIMIT 1" );
}
if($result) {
if($show == '0'){
$return = "<a class='onlineStatus' href='#' onchange='doAjaxPostOnline( 1, <?php echo $b_id ?> ); return false;' >Post Online</a>";
}
if($show == '1'){
$return = "<a class='onlineStatus' href='#' onchange='doAjaxPostOnline( 0, $b_id ); return false;' >Post Online</a>";
}
print json_encode(array("id" => $id, "return" => $return));
}
?>