0

I am trying to show data from the database in my textbox. But when I start the script I am getting no results. I tested the script in different ways and i figured out that the variable: $product1 is empty. Does anybody know how I can fix this?

index.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM forms";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<select class='form-control select2' id='product1' name='product1' onChange='getPrice(this.value)' style='width: 100%;'>";
     echo "<option selected disabled hidden value=''></option>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
                      echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
     }                   
echo "</select>";
} else {
     echo "0 results";
}

$conn->close();

?>
<html>
<body>
<!-- Your text input -->
<input id="product_name" type="text">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    function getPrice() {

        // getting the selected id in combo
        var selectedItem = jQuery('.product1 option:selected').val();

        // Do an Ajax request to retrieve the product price
    jQuery.ajax({
        url: 'get.php',
        method: 'POST',
        data: 'id=' + selectedItem,
        success: function(response){
            // and put the price in text field
            jQuery('#product_name').val(response);  
        },
        error: function (request, status, error) {
            alert(request.responseText);
        },
    }); 
    }
    </script>
    </body>
    </html>

get.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) 
    {
    die('Connection failed: ' . $conn->connect_error) ;
    } 
else 
    {
    $product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ;

    $query = 'SELECT price FROM forms WHERE id=" . $product1 . " ' ;

    $res = mysqli_query($conn, $query) ;
if (mysqli_num_rows($res) > 0) 
{
    $result = mysqli_fetch_assoc($res) ;
    echo $result['price'];
}else{
    echo 'no results';
}

    }
?>

2 Answers 2

1

Change

var selectedItem = jQuery('.product1 option:selected').val();

To

var selectedItem = jQuery('#product1 option:selected').val();

You are selecting a class with name product1, but you set only an ID with this name. Id's are specified with # and classes with .

Update on your script, because you used getPrice(this.value);

<script>
function getPrice(selectedItem) {

    // Do an Ajax request to retrieve the product price
jQuery.ajax({
    url: 'get.php',
    method: 'POST',
    data: 'id=' + selectedItem,
    success: function(response){
        // and put the price in text field
        jQuery('#product_name').val(response);  
    },
    error: function (request, status, error) {
        alert(request.responseText);
    },
}); 
}
</script>

TIP: Did you know that you can use jQuery.ajax and jQuery('selector') also like this: $.ajax and $('selector') :-)

Sign up to request clarification or add additional context in comments.

Comments

0

You have not a form tag in your HTML. The default form Method is GET.

In Your get.php you try to get a POST Variable with filter_input The function filter_input returns null if the Variable is not set.

Two possible solutions: 1. Add a form to your html with method="post" 2. Change your php code to search for a GET variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.