I get value of mrno and name by get method from the landing page. Its working fine till that point. But the problem start when I tried to get Previous Amount value from the existing table of mysql database using script . Its work fine when I manually enter mrno in first field as it successfully extract Previous Amount value from database. But as per my requirement, I only have to enter advance amount field, but in that case I unable to extract previous Amount value from the database as i didnt do keyup for mrno. How can i achieve that ?? Below is my code .
<?php
include "config.php";
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
$sql = "SELECT * FROM mis6 WHERE `id`='$user_id'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$mrno = $row['mrno'];
$name1 = $row['name1'];
;
} } }
?>
<form>
<label>MR No:</label> <input type="text" name= "mrno" id= "mrno" value="<?php echo $mrno; ?>" onkeyup ="GetDetail(this.value)" value="">
<label>Name:</label> <input type="text" name= "name1" id= "name1" value="<?php echo $name1; ?>" >
<label>Advance Amount</label> <input type="text" name= "advamt" id= "advamt" >
<label>Previous Amount:</label> <input type="text" name= "padv" id= "padv" >
<input type="submit" name="submit" id="submit" value="Submit" class="btn btn-success submit_btn invoice-save-btm">
<script>
// onkeyup event will occur when the user
// release the key and calls the function
// assigned to this event
function GetDetail(str) {
if (str.length == 0) {
document.getElementById("padv").value = "";
return;
}
else {
// Creates a new XMLHttpRequest object
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
// Defines a function to be called when
// the readyState property changes
if (this.readyState == 4 &&
this.status == 200) {
// Typical action to be performed
// when the document is ready
var myObj = JSON.parse(this.responseText);
// Returns the response data as a
// string and store this array in
// a variable assign the value
// received to first name input field
document.getElementById
("padv").value = myObj[0];
} };
// xhttp.open("GET", "filename", true);
xmlhttp.open("GET", "gfg1.php?mrno=" + str, true);
// Sends the request to the server
xmlhttp.send();
}
}
</script> </form>
// here is gfg1 file code which handle database
<?php
// Get the mrno
$mrno = $_REQUEST['mrno'];
// Database connection
$con = mysqli_connect("localhost", "thehospi_root", "u1m1a1r1", "thehospi_hmis");
if ($mrno !== "") {
// Get corresponding mrno
$query = mysqli_query($con, "SELECT padv FROM mis14 WHERE mrno ='$mrno'");
$row = mysqli_fetch_array($query);
// Get the first name
$ccc = $row["padv"];
}
// Store it in a array
$result = array("$ccc");
// Send in JSON encoded form
$myJSON = json_encode($result);
echo $myJSON;
?>
If you already have the value for mrno because it’s being passed in to your code, why not just run the second query to retrieve padv immediately after you retrieved mrno based on the user id?
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
$sql = "SELECT mrno, name1 FROM mis6 WHERE `id`='$user_id'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) { // why is there a while() loop?
$mrno = $row['mrno'];
$name1 = $row['name1'];
}
// check you have a value for `mrno`, then retrieve `padv` using that value
// run the query here
}
}
You should also look at using Prepared Statements rather than concatenating user-supplied data into queries like that. Also, if you only need two column values, don’t select *, just get those two column values.
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
$sql = "SELECT * FROM mis6 WHERE `id`='$user_id'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$mrno = $row['mrno'];
$name1 = $row['name1'];
$age1 = $row['age1'];
$mobno = $row['mobno'];
$gender1 = $row['gender1'];
$proces = $row['proces'];
$doctrname = $row['doctrname'];
} } }
if (isset($_GET['mrno'])) {
$user_id = $_GET['mrno'];
$sql = "SELECT mrno, padv FROM mis14 WHERE `mrno`='$user_id'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) { // why is there a while() loop?
$padv = $row['padv'];
}
// check you have a value for `mrno`, then retrieve `padv` using that value
// run the query here
}
}
Hi @itsumarnazir !! I think to get the previous amount value from the database table without the mrno manually you should use an onload event to call it when the page is loaded. What do you say? So you could pass the mrno value that you get from the landing page to the function and get the previous amount value from the database table automatically with these changes in your code.
Add an id attribute to your form element, so that you can access it in your script. Such as “MyList” etc.
Add an onload event listener to your form element, so that it will call the GetDetail function when the page is loaded.
document.getElementById(“mylist”).addEventListener(“load”, function() { GetDetail(“<?php echo $mrno; ?>”); });
Remove onkeyup attribute from your mrno input element:
Well you showed us the PHP changes you made, not the HTML. So yes, we have to ask the Helpdesk Level 1 questions to make sure We’ve all made the silly mistakes before.
While we’re on the subject…
is mrno a Varchar field in the database? You seem to be using it as one. Otherwise you wouldnt put quotes around an int.
Are you sure that mis14 contains a record with the mrno you’re referencing? “Doing nothing” would also be a symptom of an empty return from the database… (this relates to what @droopsnoot mentioned in their post - are you sure you should be sending user_id into that query?
As another of your threads suggests this is a patient record system, all the more reason to use Prepared Statements to help improve security, among other things.