1

i am trying to display data inside using ajax in php, i have done the following code:

<table>
    <select id="staff" name="staff">
        <option value="@N">N</option>
        <option value="@R">R</option>
        <option value="@S">S</option>
        <option value="@J">J</option>
        <option value="@So">So</option>
        <option value="@Sr">Sr</option>
        <option value="@Jo">Jo</option>
        <option value="@Sc">Sc</option>
        <option value="@P">P</option>
    </select>
    <textarea id="show" rows="8" name="notice" class="form-control"></textarea>
</table>
$('#customer').change(function () {
    var id = $(this).val();
    $.ajax({
        type: "GET",
        url: "page2.php",
        data: "pass_id=" + id,
        success: function (data) {
            alert(data);
            document.getElementById("show").innerHTML = data;
        }
    });
});

below is my page2.php which fetches the data from database:

<?php
    echo $get_id = $_GET['pass_id'];
    include("db.php");
    $sql = "select notice from admin where username='$get_id'";
    while ($row = mysqli_fetch_array($sql)) {
        echo $row['notice'];
    }
?>

but this is not giving me any data in textbox area , can anyone please tell me what is wrong in my code?

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • 3
    Your code is wide open to [SQL Injection](https://en.wikipedia.org/wiki/SQL_injection). Please use prepared statements as described in [this post](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Ivar Feb 11 '20 at 11:02
  • @Dlk i didnt understand –  Feb 11 '20 at 11:08
  • Check your SQL query and data – vadivel a Feb 11 '20 at 12:11

3 Answers3

0

Use $('#staff').change(function () {

because your Id is staff not customer.

  • its worrking but, its showing the value from the dropdown now, instead of database –  Feb 11 '20 at 11:24
0

You are using 'customer' instead of 'staff' please change and try

$('#staff').change(function () {
    var id = $(this).val();
    $.ajax({
        type: "GET",
        url: "page2.php",
        data: "pass_id=" + id,
        success: function (data) {
            console.log(data);
            document.getElementById("show").innerHTML = data;
        }
    });
});



<?php
  $data = [];
  $get_id = $_GET['pass_id'];
  include("db.php");
  $sql = "select notice from admin where username='$get_id'";
  while ($row = mysqli_fetch_array($sql)) {
    $data[] = $row['notice'];
  }
  echo json_encode($data);
?>
Marmik Patel
  • 192
  • 2
  • 12
0

Change your id customer to staff

$('#staff').change(function () {
    var id = $(this).val();
    $.ajax({
        type: "GET",
        url: "page2.php",
        data: "pass_id=" + id,
        success: function (data) {
            alert(data);
            document.getElementById("show").innerHTML = data;
        }
    });
});

And also there is a mistake in your query, please try the below code.

<?php
    $get_id = $_GET['pass_id'];
    include("db.php");
    $sql = "select notice from admin where username='$get_id'";
    $result = mysqli_query($con,$sql);
    while ($row = mysqli_fetch_array($result)) {
        echo $row['notice'];
    }
?>

Hope it will help.

ashokan
  • 70
  • 5