-4

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;
?> 
ADyson
  • 57,178
  • 14
  • 51
  • 63
Moon
  • 63
  • 5
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 29 '23 at 21:12
  • You appear to be asking how to retrieve a value which depends on the mrno, without entering the mrno. It's unclear how that could be possible. Please clarify the situation and the requirements – ADyson Aug 29 '23 at 22:52
  • @ADyson I get value in mrno and name from the landing page, as landig page have 3 column, mrno, name and add amount, mrno and name displaying number and names where as when i click on add amount column, it open new form where mrno and name autofill from that landing page automatically, now the problem is that as mrno already in it field, how can i extract value in previous account field from the data base as i am unable to call on key up event – Moon Aug 30 '23 at 04:20
  • 1
    And what is stopping you from calling `GetDetail` outside of any event handler now? – CBroe Aug 30 '23 at 06:31
  • As CBroe says, just call that function in response to some other event instead. E.g. After a button click, or typing into a different field. Do whatever is appropriate for the requirements and the user experience. It is unclear what would prevent you from doing that currently. – ADyson Aug 30 '23 at 06:36
  • mrno is already field. if i do this by using adv amount which i have to entered manually, then event not working with this field – Moon Aug 30 '23 at 07:27
  • @CBroe i think as i am not pressing onkeyup, as mrno already filled – Moon Aug 30 '23 at 07:29
  • 1
    Yes, we know that the keyup event is not going to fire; I asked you what is stopping you from calling the function yourself, _without_ it being tied to any event? – CBroe Aug 30 '23 at 07:48
  • ...or perhaps in response to some other suitable event instead. – ADyson Aug 30 '23 at 08:14
  • @ADyson other suitable event ???? whch one? i tried on focus event and even on input, not worked, i dont think so there is any event which I can link with mrno as mrno passed by get method into text field – Moon Aug 30 '23 at 08:27
  • @ CBore, like ???????? – Moon Aug 30 '23 at 08:40
  • It's up to you. It's unclear how exactly you want it to work, so it's hard to give specific advice. You mentioned something about the "advance amount field"...so maybe the function should be triggered when the user writes in that field? Or maybe it should just happen automatically as soon as the form is loaded?? Or something else? We don't know what your intended design is – ADyson Aug 30 '23 at 08:45
  • @ ADyson both will be fine, yes i have to entered Advance amount manually, and with that field, function can be triggered. but how ?? can you gave idea for that with little example – Moon Aug 30 '23 at 08:55
  • Well, you already know how to handle an input being given using `keyup` on the mrno field. So why can't you do that on the advance amount field? Or if you don't want to handle every single keypress, you could try `input` or `change`. Read about what they do, and decide which one is best for you. We can't make that decision for you. – ADyson Aug 30 '23 at 09:16
  • yes i knew but it doesn't work when i used all this events one by one with adv amount – Moon Aug 30 '23 at 10:00
  • doesn't work in what way exactly? An error? Or just no response? Or what? Choose the event you actually _want_ to use (having read about each one of them so you know what to expect), and then show us what code you used in order to implement it, and explain the problem. Then maybe we can help you understand if you did something specific which was incorrect. (You can [edit] your question to include the new code). – ADyson Aug 30 '23 at 10:02
  • its done for me by using other method without any event – Moon Aug 30 '23 at 10:44

0 Answers0