Vote count:
0
Ok so I have this form below. The drop down in my form "dropdown-select" is a drop down that is populated with data from under the column "invoiceid" from my database. Im trying to figure out how to have a user select a invoice id from the drop down menu and then hit a button "submit-id" to prefill the form with associated record/row data based on the "dropdown-select" value selected and submitted. Columns in database: "invoiceid", "txt1" & "txt2". I've included the code ive been working on for a few weeks now and cant figure out how to get it to work. Youtube videos and a few tutorials online of going through hours of trial and error and different code have only lead me here. Any help is greatly appreciated. Thank you.
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 invoiceid = $('#dropdown-select').val();
e.preventDefault();
$.ajax({
url: "/tst/orders2.php",
data: {
invoiceid: invoiceid
}
}).done(function(data) {
$('#txt1').append(data);
$('#txt2').append(data);
});
});
});
</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['dropdown-select']))
{
$dropdown-select= $_GET['dropdown-select'];
$query = "SELECT txt1, txt2
FROM seguin_orders
WHERE invoiceid = '".($dropdown-select)."'";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result))
{
echo $row['txt1']
echo $row['txt2'];
}
?>
Using Ajax and php, how can I prefill a form with record/row data from my database on button click?
Aucun commentaire:
Enregistrer un commentaire