Post to mySQL and refresh page using AJAX
Hi all. I'm trying to create a simple HTML task manager. There are 3 forms on the page, for adding a new task, displaying the task list (while loop, recordset 'tasks') and displaying the archived tasks list (while loop, recordset 'archive'). My issue is with the 'delete' button within the tasks list. It was posting the task (by ID) to another page, which updated the 'archived' field to 'yes' then returned to index2.php, where the task had moved to the archive list. I've realised that I need to use AJAX now, to keep everything happening on the same page, but I have no experience with this. Can anyone advise me on how to go about updating the 'archived' field without leaving the page? Thanks guys, here's my code...
PHP Code:
<?php require_once('Connections/testdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formNewTask")) {
$insertSQL = sprintf("INSERT INTO tasks (`description`) VALUES (%s)",
GetSQLValueString($_POST['fieldNewTask'], "text"));
mysql_select_db($database_testdb, $testdb);
$Result1 = mysql_query($insertSQL, $testdb) or die(mysql_error());
$insertGoTo = "index2.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_testdb, $testdb);
$query_tasks = "SELECT * FROM tasks WHERE archived = 'no'";
$tasks = mysql_query($query_tasks, $testdb) or die(mysql_error());
$row_tasks = mysql_fetch_assoc($tasks);
$totalRows_tasks = mysql_num_rows($tasks);
mysql_select_db($database_testdb, $testdb);
$query_archived = "SELECT * FROM tasks WHERE archived = 'yes'";
$archived = mysql_query($query_archived, $testdb) or die(mysql_error());
$row_archived = mysql_fetch_assoc($archived);
$totalRows_archived = mysql_num_rows($archived);
?>
HTML Code:
New Task<br />
<div id="newTask">
<form id="formNewTask" name="formNewTask" method="POST" action="<?php echo $editFormAction; ?>">
<table width="500" border="1">
<tr>
<td width="173"><input name="fieldNewTask" type="text" /></td>
<td width="311"><input name="buttonNewTask" type="submit" value="add"/></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="formNewTask" />
</form>
</div>
<br />
Task List<br />
<div id="tasks">
<form id="formTasks" name="formTasks" method="post" action="">
<?php do { ?>
<table width="500" border="1">
<tr>
<td width="170"><input name="fieldTasks" type="text" value="<?php echo $row_tasks['description']; ?>" /></td>
<td width="314"><input name="buttonDeleteTasks" type="button" value="delete"/></td>
</tr>
</table>
<?php } while ($row_tasks = mysql_fetch_assoc($tasks)); ?>
</form>
</div>
<br />
Completed Tasks<br />
<div id="taskArchive">
<form id="formTasksArchive" name="formTasksArchive" method="post" action="">
<?php do { ?>
<table width="500" border="1">
<tr>
<td width="170"><input name="fieldTasksArchive" type="text" value="<?php echo $row_archived['description']; ?>" /></td>
<td width="314"><input name="fieldDateArchive" type="text" /></td>
</tr>
</table>
<?php } while ($row_archived = mysql_fetch_assoc($archived)); ?>
</form>
</div>