Schmoopy
03-07-2009, 11:14 PM
Hey there, I have the following javascript code:
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, error){
clearForm();
animatedcollapse.toggle('addapp')
if (error == 0)
$('appdiv').innerHTML= req.responseText;
else{
$('response').innerHTML=req.responseText;
}
}
It's prototype code just in case you were wondering, but basically I want to pass an error value from PHP to this code here and if there are no errors it will respond in one div, and if there are errors, it will respond in a different div.
The thing is I don't know how I can pass this error value to the function, my knowledge on JavaScript is limited so I don't know how you'd do it.
Here is the whole PHP file, you don't need it all just focus on the bits that say $error = 1;
<?php
require_once("includes/connection.php");
require_once("includes/functions.php");
date_default_timezone_set("Europe/London");
$datenow = date("Y-m-d");
$error = 0;
/* Adds new appointments - Checks for valid fields before submitting to database */
// 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)
{
$error = 1;
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
{
$error = 1;
die("You can't enter a time that has already past!");
}
// END VALIDATION
$query = "INSERT INTO appointments (`name`, `date`, `time`, `where`, `phone_no`, `job_description`, `quoted_price`)
VALUES('$name', '$fulldate', '$time', '$where', '$phone_no', '$job_desc', '$quoted_price')";
if(!mysql_query($query))
{
die("Failed to create" . mysql_error());
}
else
{
$refresh = mysql_query("SELECT * FROM appointments WHERE date = '$datenow' ORDER BY date, time");
if (mysql_num_rows($refresh))
{
echo "<table id=\"apptable\">\n<tr class=\"headings\"><th class=\"first\">Name</th><th>Time</th><th>Where</th><th>Phone No.</th><th>Job Description</th><th>Quoted Price</th></tr>\n";
while($row = mysql_fetch_array($refresh)) // Go through all records in the database, echo the following code for each record found
{
echo "<tr class=\"data\""; if (AppSoon($row['time'])) { echo "style=\"background-color:#FF7348;\" title=\"This appointment starts within an hour!\""; $appsooncount = true; } echo "><td>" . $row['name'] . "</td><td>";
echo FormatTime($row['time']);
echo "<td>" . $row['where'] . "</td><td>" . $row['phone_no'] . "</td><td>" . $row['job_description'] . "</td><td>£" . $row['quoted_price'] . " </td><td class=\"last\"><a class=\"remove\" onclick=\"RemoveApp("; echo $row['id']; echo ")\" href=\"#\"></a></td></tr>\n";
}
echo "</table>";
if(isset($appsooncount) && ($appsooncount))
echo "<div id=\"appsoon\">Appointments with this background colour start within the hour.</div>";
}
else
{
die("No appointments scheduled.");
}
}
mysql_close($con);
?>
So, basically how could I pass this variable from the PHP to the javascript file? At the moment all it gets is the text, so I need to add this in somehow.
Thanks for any help you guys can come up with :)
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, error){
clearForm();
animatedcollapse.toggle('addapp')
if (error == 0)
$('appdiv').innerHTML= req.responseText;
else{
$('response').innerHTML=req.responseText;
}
}
It's prototype code just in case you were wondering, but basically I want to pass an error value from PHP to this code here and if there are no errors it will respond in one div, and if there are errors, it will respond in a different div.
The thing is I don't know how I can pass this error value to the function, my knowledge on JavaScript is limited so I don't know how you'd do it.
Here is the whole PHP file, you don't need it all just focus on the bits that say $error = 1;
<?php
require_once("includes/connection.php");
require_once("includes/functions.php");
date_default_timezone_set("Europe/London");
$datenow = date("Y-m-d");
$error = 0;
/* Adds new appointments - Checks for valid fields before submitting to database */
// 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)
{
$error = 1;
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
{
$error = 1;
die("You can't enter a time that has already past!");
}
// END VALIDATION
$query = "INSERT INTO appointments (`name`, `date`, `time`, `where`, `phone_no`, `job_description`, `quoted_price`)
VALUES('$name', '$fulldate', '$time', '$where', '$phone_no', '$job_desc', '$quoted_price')";
if(!mysql_query($query))
{
die("Failed to create" . mysql_error());
}
else
{
$refresh = mysql_query("SELECT * FROM appointments WHERE date = '$datenow' ORDER BY date, time");
if (mysql_num_rows($refresh))
{
echo "<table id=\"apptable\">\n<tr class=\"headings\"><th class=\"first\">Name</th><th>Time</th><th>Where</th><th>Phone No.</th><th>Job Description</th><th>Quoted Price</th></tr>\n";
while($row = mysql_fetch_array($refresh)) // Go through all records in the database, echo the following code for each record found
{
echo "<tr class=\"data\""; if (AppSoon($row['time'])) { echo "style=\"background-color:#FF7348;\" title=\"This appointment starts within an hour!\""; $appsooncount = true; } echo "><td>" . $row['name'] . "</td><td>";
echo FormatTime($row['time']);
echo "<td>" . $row['where'] . "</td><td>" . $row['phone_no'] . "</td><td>" . $row['job_description'] . "</td><td>£" . $row['quoted_price'] . " </td><td class=\"last\"><a class=\"remove\" onclick=\"RemoveApp("; echo $row['id']; echo ")\" href=\"#\"></a></td></tr>\n";
}
echo "</table>";
if(isset($appsooncount) && ($appsooncount))
echo "<div id=\"appsoon\">Appointments with this background colour start within the hour.</div>";
}
else
{
die("No appointments scheduled.");
}
}
mysql_close($con);
?>
So, basically how could I pass this variable from the PHP to the javascript file? At the moment all it gets is the text, so I need to add this in somehow.
Thanks for any help you guys can come up with :)