0

So I'm getting used to using AJAX, I have a simple form where the user enters a number (referring to a job card) and then I want to use AJAX to return the status and priority of that job card when the user clicks update.

The status field successfully updates with the code below.

Here is the html form:

<div class="JobCard">
No. <input id="num" type="text">
Status: <input id="status" type="text" readonly>
Priority: <input id="priority" type="text" readonly>
<input type="button" value="Update" onclick='showStatus(getElementById("num").value)'>
</div>

My java script:

<script>
function showStatus(num) {
if (num == "") 
{
    document.getElementById("status").value = "";
    return;
} 
else
 { 
     xmlhttp = new XMLHttpRequest();
 } 

 xmlhttp.onreadystatechange = function() 
 {
 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
            document.getElementById("status").value = xmlhttp.responseText;
        }
 };
    xmlhttp.open("GET","getStatus.php?q="+num,true);
    xmlhttp.send();

}
</script>

And also my getStatus.php file:

<?php
require_once("../functions.php");
$num = sanitizeString($_GET['q']);
$result =  queryMySql(" SELECT `Status` FROM JobCardHead WHERE JobCardNum = '$num' ");
$row = $result->fetch_assoc();
$status = $row['Status'];
echo $status ; 
?>

My question is how can I extend this so that the Priority field also updates. I know that I could create a new function showPriority() and and new file getPriority.php but I plan to add many more fields so I wonder if theres a more efficient way.

Connor Bishop
  • 921
  • 1
  • 12
  • 21
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jul 02 '16 at 10:07
  • Of course, I have a function to protect but keep forgetting to use it. Its not that important because the site won't be a public site, but still I have fixed the issue. – Connor Bishop Jul 02 '16 at 10:12
  • 1
    In your php, send back a json string and parse it in your `onreadystatechange = function()` – PaulH Jul 02 '16 at 10:14

0 Answers0