Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

77
Views
Trying to fetch MySQL Data and display it in text fields using PHP - Nothing Happening when making my selection?

I am trying to pull through the corresponding use by date for the selected Stock Item. I have searched through my code and tried to re write it more than once and I just can't find where my error lies atall. When I am trying to run the code, I am able to select my Stock, but nothing happens when I make that selection. After choosing 'Inspect' on my webbrowser, it used to come up with a HTTP:500 Error but that seems to have gone and now just nothing is happening? Looking for a fresh pair of eyes to point me in the right direction if possible!

To note: I have used the following at the top of my html page in the section.

<script src="./fetchtraceability.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

SNIPPET FROM HTML PAGE:

<label for="labels">Stock:</label>
    <select id="stockid" name="Stock" onchange="fetchuseby()">
        <option>Select Stock</option>
            <?php
                $sqli = "SELECT Stock FROM stock WHERE Sector = 'Beef'";
                $result = mysqli_query($con, $sqli);
                while ($row = mysqli_fetch_array($result)) {
                    $k = $row['Stock'];
                    echo '<option value="'.$k.'">'.$k.'</option>';
                }
            ?>
    </select>
    <br>
    <br>
<label for="labels">Use By:</label>
    <input type="text" name="UseBy" id="usebyid">
    <br>
    <br>
    <input type="submit" value="Submit">

JS FUNCTION:

function fetchuseby(){
var id = document.getElementById("stockid").value;

$.ajax({
    url:"showuseby.php",
    method: "POST",
    data:{
        x : id
    },
    dataType: "JSON",
    success: function(data){
        document.getElementById("usebyid").value = data.UseBy
    }
})

}

PHP CODE:

<?php
ini_set('display_errors', 1); 
$server="localhost";
$userid="root";
$password="root";
$myDB="butchery_system_trial";

$con = mysqli_connect($server, $userid, $password, $myDB);

$k = $_POST["x"];
$sqli = "SELECT Use_By FROM stock WHERE Stock={$k}";
$result = mysqli_query($con, $sqli);
while ($rows = mysqli_fetch_array($result)) {
    $data['UseBy'] = $rows["Use_By"];
}

echo json_encode($data);

?>

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

your all code is currect.please check connection information.i think connection password is blank not root

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs