Display Specific data using Ajax and jquery and html select button

Bless

New Member
#1
hello community i need help on this what i really want to do is to select from database using onchange() function through Ajax.then return the result to the client.i used Ajax to send the information to the server side and but i am having problem return it with the where condition

//this is the html page
Mã:
<body   style="font-family:arial bold; ">

<div style="text-align:left; padding:1%;  font-family:Arial bold; color:#cccccc;font-size:40px;"> Select</div>
      <select class="form-control"  id="color" name="color" >
<option >Please select an option</option>
  <option> Red</option>
  <option  >Yellow</option>
  <option  >white</option>
  <option  >Black</option>
  <option  >Violet</option>
</select>
<br/>


//this is where the output will be displayed with parameter (res);
<div id="dis"></div>

</body>
//this is the script.js
Mã:
$(function(){
    $('#color').on('change', function()
    {
    var selt= this.value ;
    d = $('#color').val();
    alert(d);
    $.ajax({
            url: "ajax-cart.php",
      method: "POST",
      data: {
                request:"select",
            selt:selt
      }

        }).done(function(res) {

      $('#dis').val(res) ;

console.log(res);
        });




  });
//this is ajax cart.php
Mã:
<?php// START THE SESSION
session_start();// CONFIGURATION
require("db.php");
// PROCESS REQUESTS
switch ($_POST['request']) {

  // THIS PART is for the select button
case "select":
require("db.php");

 $conn = new mysqli($dbServername, $dbUsername, $dbPassword, $dbName );

$val = $_POST['selt'];


$data =  $conn->query("SELECT * FROM `goods` where color = '.$val.' LiMIT 4");


echo Json_encode($data);
break;
this is the console result {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
 

Admin

Administrator
Thành viên BQT
#2
you just missed fetching mysql result, you are just returning query not result.

change your query to

$data = $conn->query("SELECT * FROM goods where color = '".$val."' LiMIT 4");

Add

$result = $data->fetch_array(MYSQLI_ASSOC);

after

$data = $conn->query("SELECT * FROM goods where color = '.$val.' LiMIT 4");

and then

echo json_encode($result);
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top