w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
I am using ajax to show the output after entering some data on textfield ,it is not showing proper output?

The approach should be

  1. Write onchange event (onchange of textbox content call the process() function) for the text box.

               or 
    
  2. To add a button to triggering the function. => user enters the dish and clicks on button.

Approach one (Using jQuery)

<!DOCTYPE>
<html>
<head>

<script type="text/javascript"
src="jquery-1.9.1.min.js"></script>
<!--<script type="text/javascript"
src="foodstore.js"></script>-->
<script>
    $(document).on("keyup","#inputuser", function(){

        var dish = $(this).val();

        $.ajax({
          type: "GET",
          url: 'foodstore.php',
          data : {food:dish},
          success: function(data){alert(data);
            $('#usererror').html(data);
          }
        });
    });
</script>
</head>
<body>
    <h1>choose your favorite food</h1>
    <input type="text"  id="inputuser" value="" />
    <div id="usererror"></div>
</body>
</html>

foodstore.php

<?php
$food=$_GET['food']; 
$foodArray=array('shahi paneer','matar paneer','matar alu','raita');
if(in_array($food,$foodArray)) {
   echo 'we  do have '.$food.'!'; 
}
elseif($food=='') { 
   echo 'please enter any dish';
} else  {
   echo 'we dont have '.$food.'!';
}

?>




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.