-1

I have two scripts that are parsing data. The message.php works fine but when I am trying to send data to data.php I am facing the error:

"Notice: Undefined index: userid in D:\xampp\htdocs\metro\data.php on line 11"

The code:

<script type="text/javascript">
  function post(){
      var name = document.getElementById("name").value;
      if(name==''){
        swal('Type Your Message','You Cannot Send An Empty Message','error');
        return false;
      }
      else{
        $.ajax
        ({
          type: 'post',
          url: 'message.php',
                data:
          {
             message:name,
             userid: <?php echo $get; ?>
          },
                 success: function (response)
          {
            alert(user);
}
        });
        return false;
      }
  }
</script>


<script type="text/javascript">
  $(document).ready(function(){

    setInterval(function(){
      $('#show').load('data.php');
    },1000);

  });


</script>
<script type="text/javascript">
$(document).ready(function(){
  var k =<?php echo $get; ?>;
  if(k!=''){

  $.ajax
  ({
    type: 'post',
    url: 'data.php',
    data:
    {
       "userid": <?php echo $get; ?>
    },
     success: function (response)
    {
      alert(k);
}
  });
  return false;
}

});
</script>

The error is thrown:

Notice: Undefined index: userid in D:\xampp\htdocs\metro\data.php on line 11

KevinO
  • 4,303
  • 4
  • 27
  • 36
  • Your `$('#show').load('data.php');` -> `setInterval(function(){$('#show').load('data.php'); },1000); });` is not sending `userid` value in `post` like your other 2 `$.ajax()` calls. – Sean Feb 17 '19 at 06:40
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Sean Feb 17 '19 at 06:42
  • @Sean i am sending data to data.php checkout the last script where code is $.ajax ({ type: 'post', url: 'data.php', data: { "userid": }, success: function (response) { alert(k); } }); – Mujahid Farooq Feb 17 '19 at 06:51

1 Answers1

0

In your data.php calling ajax code, you should make below changes:

data:
   {
      userid: '<?php echo $get; ?>'
   },

You missed quotes from variable value and also added quotes when you are defining key. Hope it helps you.

Rohit Mittal
  • 2,064
  • 2
  • 8
  • 18
  • i also have tried this but it's not working above in message.php ajax call this works fine but down when i parse data to data.php it isn't working anyhow userid: – Mujahid Farooq Feb 17 '19 at 07:58
  • can you check on console -> network -> click on ajax -> select Params -> check if is passing userid value in your ajax request – Rohit Mittal Feb 17 '19 at 08:08