Ok I've added a bit to my code - still not working but I may be going down the right track.
Here's my js file:
Code:
function validate() {
new Ajax.Request("validateapp.php",
{
method: 'post',
parameters: {
name: $F('name'),
date: $F('date'),
time: $F('time'),
where: $F('where'),
phone_no: $F('phone_no'),
job_desc: $F('job_desc'),
quoted_price: $F('quoted_price'),
},
onComplete: ShowErrors
});
}
function ShowErrors(req)
{
$('response').innerHTML=req.responseText;
}
function sendRequest() {
new Ajax.Request("appointments.php",
{
method: 'post',
parameters: {
name: $F('name'),
date: $F('date'),
time: $F('time'),
where: $F('where'),
phone_no: $F('phone_no'),
job_desc: $F('job_desc'),
quoted_price: $F('quoted_price'),
},
onComplete: showResponse
});
}
function showResponse(req){
clearForm();
animatedcollapse.toggle('addapp')
$('appdiv').innerHTML= req.responseText;
}
function clearForm()
{
document.getElementById('name').value = "";
document.getElementById('date').value = "";
document.getElementById('time').value = "";
document.getElementById('where').value = "";
document.getElementById('phone_no').value = "";
document.getElementById('job_desc').value = "";
document.getElementById('quoted_price').value = "";
}
function refreshTable(req){
$('appdiv').innerHTML= req.responseText;
}
function RemoveApp(id)
{
var response = confirm('Are you sure you want to delete the appointment?');
if (response == true)
{
new Ajax.Request("removeapp.php",
{
method: 'post',
postBody: 'id=' + id,
onComplete: refreshTable
});
}
else{
return false;
}
}
And relevant HTML:
Code:
<div id="addapp">
<form name="appform" id="addappform" onsubmit="return false;"> <!-- Stop the form from being submitted so AJAX code is called instead -->
<label for="name">Name:</label> <input type="text" name="name" id="name"/><br />
<label for="date"><a href="javascript:showCal('appcal')">Select Date:</a> </label> <input type="text" name="date" id="date"/><br />
<label for="time">Time:</label> <input type="text" name="time" id="time"/><br />
<label for="where">Where:</label> <input type="text" name="where" id="where"/><br />
<label for="phone_no">Phone No:</label> <input type="text" name="phone_no" id="phone_no"/><br />
<label for="job_desc">Job Description:</label> <input type="text" name="job_desc" id="job_desc"/><br />
<label for="quoted_price">Quoted Price:</label> <input type="text" name="quoted_price" id="quoted_price"/><br />
<input class="submit" type="submit" name="submit" value="Add Appointment" onclick="validate() && sendRequest();"/>
<div id="response">
</div>
</form>
</div>
So what it does now is send off the form, pass it to "validateapp.php", which then does some checks and then is *supposed* to send it off to the sendRequest function if no errors occurred, otherwise it will echo back an error in another div.
validateapp.php:
PHP Code:
<?php
session_start();
require_once("includes/connection.php");
require_once("includes/functions.php");
if(!LoggedIn())
{
$login = "login.php?login=no";
redirect_to($login);
}
date_default_timezone_set("Europe/London");
$datenow = date("Y-m-d");
// START VALIDATION
$fields = array('name','date','time','where','phone_no','job_desc','quoted_price');
foreach($fields as $field)
{
if(empty($_POST[$field]))
{
die("You must complete all fields");
}
else
{
$$field = mysql_real_escape_string($_POST[$field]); // Variable within a variable, sanitize variable
}
}
$formatdate = explode("/",$date); // Strip slashes from date and put year, month and day into array
$fulldate = $formatdate[2] . "-" . $formatdate[1] . "-" . $formatdate[0]; // Format date from DD/MM/YYYY -> YYYY-MM-DD
if($fulldate < $datenow)
{
die("You can't enter a date that has already past!");
}
$timenow = strftime("%H:%M");
if($time < $timenow)//If time before time now and date is today -> Delete
{
die("You can't enter a time that has already past!");
}
// END VALIDATION
return true;
?>
So as you can see at the end I tried returning true, I know it wouldn't really do anything as it has to pass it back through AJAX and echo an error message or it does nothing.
At the moment if no errors occurr then nothing happens, I imagine this is because there is nothing for the ShowErrors function to echo back. Which leads me back to passing arguments to the ShowErrors function, to see if any errors occurred.
This is really integral to my project, please someone help!
Thanks.
Bookmarks