简体   繁体   English

echo 在 ajax 调用后不显示任何值

[英]echo not displaying any value after ajax call

Please Help me with this i want to display the value in php echo using ajax call, the code is running without error.请帮助我,我想使用 ajax 调用显示 php echo 中的值,代码运行无误。 the value get displayed on console console.log(id) when i select options from select field, and the value which i want to display using echo displays on console when i used console.log(data) .当我使用 select 字段中的 select 选项时,该值显示在控制台console.log(id)上,而当我使用console.log(data)时,我想使用 echo 显示在控制台上显示的值。

<script>
$('#select-user-id').click(function() {
  var id = $(this).val();

  $.ajax({
    url: 'ajax.php',
    type: 'POST',
    data: {
      id: id
    },
    success: function(data) {
      console.log(id);
      console.log(data);
    }
  });
})
</script>

But not displays on browser screen when i echo the value using.但当我回显使用的值时,不会显示在浏览器屏幕上。

$select_id = $_POST['id'];
echo "PHP: $select_id";

What you need to do is assign the value in a span or other html tags on success.您需要做的是在成功时在 span 或其他 html 标签中分配值。

success: function(data) {
     yourSpan.value = data;
}

Just like what Ainz had mentioned, what you would want to do is send the value to a tag and display it there.就像 Ainz 提到的那样,您想要做的是将值发送到标签并在那里显示。

<!-- Be sure that your jQuery file is added -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>  

<select name="select-user-id" id="select-user-id">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

//Add a div as a holder for the returned values from your AJAX file
<div class="displayData"></div>

<script>
$('#select-user-id').on('change', function() {
  var id = $(this).val();
  /* Also make sure that you have the correct path for your file  */
  $.ajax({
    url: 'ajax.php',
    type: 'POST',
    data: {
      "id": id
    },

    success: function(data) {
      $('.displayData').html(data);
    }
  });
});
</script>

You can also append your variable to the string like below.您也可以将您的变量 append 设置为如下所示的字符串。

<?php
   $select_id = $_POST['id'];
   echo 'PHP: '.$select_id;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM