Varuni N R
Varuni N R

Reputation: 802

jquery onchange with php

I am new to coding in php. Please help me with this. This is my html code.

<label>Select room type:</label>
<select name="user" id="user ">
  <option>one</option>
  <option>two</option>
  <option>three</option>
</select>
<div id="display"></div>

This is my jquery code:

$(document).ready(function() {
    $("#user").change(function (e) {
      var user = $(this).val();
      $.post("fetch.php",{"user": user},function (data) {
          $('#display').html(data);
      });
    }); 
});

This is fetch.php:

<?php
   echo $_POST['user'];
?>

Error i am getting when i run fetch.php is

Notice: Undefined index: user on line 2

Please help me where i am going wrong

Upvotes: 1

Views: 144

Answers (1)

Nana Partykar
Nana Partykar

Reputation: 10548

Alternative Way

jquery code:

$('#user').change(function(){
    var user = $('#user').val();
    $.ajax({url:"fetch.php?user="+user,cache:false,success:function(result){
        $('#display').html(result);
    }});
});

fetch.php

<?php echo $_GET['user'];?>

Upvotes: 1

Related Questions