简体   繁体   中英

How Can I Prefill My form with data from my database based on drop down menu selection?

Hi I have been trying to figure out how to prefill my from with data from my database based on a selection from a drop down menu. Been on it for a couple weeks cant figure it out...much thanks to anyone's help.

I have a form that has a dropdown selection menu that loads invoice ids from my database. I'm trying to prefill my form based on the value of the invoiceid selected.

In my database, column "invoiceidcopy" data shows as a selection for each different record/row value under my drop down menu.

If you notice in my image of my database table below you will notice the first row/record's invoiceidcopy field is blank..consequently...in my drop down menu ...the first selection is blank.

Ultimately my code below works only when i select the blank selection from the drop down menu but not for the other 2 selections?

Anything wrong with my code and or anyone know of a different way to achieve this...Thank u.

数据库

FORM

<form action="#">

<select id="dropdown-select" name="dropdown-select">
<option value="">-- Select One --</option>
</select>

<button id="submit-id">Prefill Form</button>

<input id="txt1" name="txt1" type="text">
<input id="txt2" name="txt2" type="text">



<button id="submit-form" name="Submit-form" type="submit">Submit</button>


</form> 

SCRIPT

<script>
     $(function(){


        $('#submit-id').on('click', function(e){  


            var invoiceidcopy = $('#dropdown-select').val();
            e.preventDefault(); 

             $.ajax({
              url: "/tst/orders2.php",
              data: {
                'invoiceidcopy': invoiceidcopy
              }
            }).done(function(data) {

    data = JSON.parse(data);

$('#txt1').val(data.txt1);
$('#txt2').val(data.txt2);


});
        });
     });
</script>

/tst/orders2.php

<?php

// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");


// Check if the connection failed
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  die();
}

   if (isset($_GET['invoiceidcopy']))
{
    $invoiceidcopy= $_GET['invoiceidcopy'];

   $query = "SELECT txt1, txt2, q1
        FROM seguin_orders
  WHERE invoiceidcopy = '".$invoiceidcopy."'";

    $result = mysqli_query($con,$query);

    while ($row = mysqli_fetch_assoc($result)) 
        {
 echo json_encode($row);
 die(); 
    }
}
?>

Like minion stated, what exactly is sent as invoiceidcopy? From what I see the selecting the last one should send "THIS IS A TEST 2759f633-d6cb-4b3e-b4dc-3a4d0b54367..." exactly. It would be better to come up with another shorter Key for this field like "ThisIsATest-2759f633" or "ThisIsATest-201502070057". Also keep in mind that you want to sanitize the input to keep out SQL injection. Even though it is a drop down list, someone can use the HTML to change the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM