$ .post结果回显HTML而不是值

时间:2019-04-12 00:33:00

标签: php html ajax post

一个简单的问题。我正在获取HTML元素的值,并使用以下代码通过POST将其发送到服务器。但是,当我警告结果而不只是结果时,我也得到了包含的PHP文件的HTML。为什么会这样?

function postData(){
  let select_value = document.getElementById("selector").value;
  $.post('customer_order_form.php', { valor: select_value }, function(result) { 
      alert(result);
  });
}

PHP代码非常简单,它只是回显数量以进行测试:

<?php include('includes/header.html'); 


if (isset($_POST["valor"]))
{
  $quantity = $_POST["valor"];
  echo $quantity;

} 
else 
{
  $quantity = null;
  echo "no quantity here";
}

?>

但是在提醒结果的回调函数上,我得到了所有包含的文件HTML代码而不是值,为什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

include('includes/header.html');正在将HTML添加到页面。当您执行$.post()时,您正在检索头文件。此include不应在文件中。

删除include,一切正常。

相关问题