Reputation: 77
Sorry for not stated my problem. Actually I want to update my data in database. But the problem now is even i tried to choose approve or reject the ajax still won't update. I am new in ajax and try search around net but my code still got problem
here is my php page
<?php
$querysel = "SELECT * FROM tblinternapplication WHERE course_code = '{$course_codeapp}' ORDER BY student_id, 1 DESC " ;
$resultsel = mysql_query($querysel, $connection);
echo "<h2><div class=\"h_title\">Status still in pending</div></h2>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th scope=\"col\">Matric ID</th>";
echo "<th scope=\"col\">Company name</th>";
echo "<th scope=\"col\" width = \"200\">Job Scope</th>";
echo "<th scope=\"col\">Status</th>";
echo "<th scope=\"col\">Action</th>";
echo "</tr>";
echo "</thead>";
while($rowsel = mysql_fetch_array($resultsel)){
if($rowsel['status_approval'] == NULL){
$id = $rowsel['id'];
echo "<tr>";
echo "<tr>"."<td class=\"align-center\">".$rowsel['student_id']."</td>";
echo "<td class=\"align-center\">".$rowsel['company_name']."</td>";
echo "<td class=\"align-center\" width = \"200\">".$rowsel['job_scope']."</td>";
echo "<td class=\"align-center\">";
if($rowsel['status_approval'] != NULL){
if( $rowsel['status_approval'] == 0)
{
echo "Reject";
}
else
{
echo "Approve";
}
}
else
{ echo "Pending";
}
echo "</td>";
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus(this.value)\">";
echo "<option value=\"\">Select status:</option>";
echo "<option value=\"1\">Approve</option>";
echo "<option value=\"0\">Reject</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
here is my jscript page
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function getstatus(id, approve)
{
if (approve=="")
{
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","updatestatus.php?id=" + id + "&status=" + approve,true);
xmlhttp.send();
}
</script>
then here is my updatestatus.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codeapp = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<?php
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
$result = mysql_query($sql);
?>
I work for few days but problem still cannot solve. Hope someone can help me. I will appreciate your help!
Upvotes: 2
Views: 1282
Reputation: 4550
I would suggest to use the developer tools like F12 in IE or chrome or the fiddler to check the request you are sending to the PHP code . In case you are getting any error you can easily see in the response
Upvotes: 0
Reputation: 12815
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus(this.value)\">";
In that line above, you are passing only one value. At the same time your JS function waits for 2 parameters. Currently, you have id
always equal to 0 or 1 and status is always undefined.
Suppose you just need to change that line like:
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus("+ $id +", this.value)\">";
One note:
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
Except that you should not use mysql_* functions anymore, code above is opened to sql injections. To avoid sql injections with mysql_ extension you should do something like this:
$sql="UPDATE tblinternapplication set status_approval = ".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
See docs for mysql_real_escape_string
here. Also, read a warning message on that page - it tells you what you should use in replacement for mysql extension
Upvotes: 0
Reputation: 1959
For preapre result for JS in php use json_encode() function. Make your update script somthing like this:
<?php require_once("../includes/session.php");
require_once("sessioncourse.php");
$course_codeapp = $_SESSION['course_code'] ;
confirm_logged_in();
require_once("../includes/connection.php");
require_once("../includes/functions.php");
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
$result = mysql_query($sql);
$json = array();
while ($row = mysql_fetch_assoc($result)) {
$json[] = $row;
}
echo json_encode($json);
Dont close php tag or you may add extra space chars
In your case using jQuery ajax was good practic. Mkae you code like this:
<script type="text/javascript">
function getstatus(id, approve)
{
$.ajax({
'url': 'updatestatus.php',
'data': {"id": id, "status": approve},
'success': function (response) {
console.log(response);
//TODO: use server response
}
});
}
</script>
Upvotes: 1