Sunday 18 August 2013

Display different parts of JQuery Ajax response in separate DIVs

Display different parts of JQuery Ajax response in separate DIVs

I have a script that allows the user to grade an image and then
dynamically adds another form to the page to grade the next. When the
image grade is entered it is entered into a database and displayed on the
page above the newly created form in a list with the ID responds.
All of this works great but I would also be able to show a running count
of how many images have been audited at the top of the page in a DIV with
the ID results.
The JQuery
$("#FormSubmit").click(function (e) {
e.preventDefault();
if($("#grade").val()!='1')
{
if($("#positioning_reason").val() == '' &&
$("#exposure_reason").val() == '' &&
$("#equipment_reason").val() == '' &&
$("#patient_reason").val() == '')
{
alert("Please select why the image was not perfect");
return false;
}
}
jQuery.ajax({
type: "POST",
url: "response.php",
dataType:"text",
data: $('#test_form').serialize(),
success:function(response){
$("#responds").append(response);
$('#test_form')[0].reset();
$("#test_form").get(0).scrollIntoView();
$("#results").display $numRows in this div
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
});
response.php
if(isset($_POST['grade']))
{
$grade = $_POST['grade'];
$positioning = $_POST['positioning_reason'];
$exposure = $_POST['exposure_reason'];
$patient = $_POST['patient_reason'];
$equipment = $_POST['equipment_reason'];
$user_id = $_POST['user_id'];
$audit_id = $_POST['audit_id'];
$sql= "INSERT INTO image(auditID,
imageGrade,positioning_reasonID,exposure_reasonID,patient_reasonID,equipment_reasonID,auditDate,userID)
VALUES('$audit_id',
$grade,'$positioning','$exposure','$patient','$equipment',NOW(),$user_id)";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
if($result)
{
$sql = "SELECT * FROM image WHERE auditID = '$audit_id'";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
$numRows = mysqli_num_rows($result);
$my_id = mysqli_insert_id($mysqli);
echo '<li id="item_'.$my_id.'" class="audit_item">';
echo '<div class="del_wrapper"><a href="#" class="del_button"
id="del-'.$my_id.'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a></div>';
echo 'Grade - '.$grade.' - '.$my_id.' - '.$numRows.'</li>';
}else{
header('HTTP/1.1 500 Looks like mysql error, could not insert
record!');
exit();
}
}
I would like to display the value $numRows in a div called 'results' (at
present it i being displayed with all of the other results for testing
purposes). I have tried getting the $numRow value from a different php
page using a separate page load function further down the page but it was
very slow and the result were not always correct because of timings

No comments:

Post a Comment