如何将post数据从Javascript发送到PHP

如何将post数据从Javascript发送到PHP,javascript,php,Javascript,Php,这是我的剧本 <?php $test= '121606901-hpe2'; ?> <script type="text/javascript"> $(document).ready(function(){ var url="getdata.php"; var data={ no_hpe : <?php echo "'$test'"; ?> } var type="POST"; }); </script> 但是,我的问题是,$n

这是我的剧本

<?php
    $test= '121606901-hpe2';
?>

<script type="text/javascript">

$(document).ready(function(){
 var url="getdata.php";
 var data={ no_hpe : <?php echo "'$test'"; ?> }
 var type="POST";
});

</script>
但是,我的问题是,
$no\u hpe
是空的。我该怎么做?谢谢。

试试这个

  $.ajax({
    url: "getdata.php",
    type: "POST",
    data: { no_hpe : <?php echo "'$test'"; ?> }
  }).done(function(resp) {
    // success
  }).fail(function(jqXHR, textStatus) {
    alert("Request failed: " + textStatus + " - Please try again.")
  })
$.ajax({
url:“getdata.php”,
类型:“POST”,
数据:{否hpe:

这可能有助于您

鉴于您已经在使用jquery,请查看jquery ajax文档-并尝试一些东西如果您在两年前似乎已经掌握了jquery ajax的概念-您只是在js中将数据分配给var,您需要将该数据提交给服务器。您可以使用jquery get、post或ajax函数来完成
  $.ajax({
    url: "getdata.php",
    type: "POST",
    data: { no_hpe : <?php echo "'$test'"; ?> }
  }).done(function(resp) {
    // success
  }).fail(function(jqXHR, textStatus) {
    alert("Request failed: " + textStatus + " - Please try again.")
  })
var formdata = new FormData($('form')[0]);
$.ajax({
url: 'mail.php',
type: 'POST',
data: formdata ,
success: function (response) {
        if(data.response === 'success') {
 alert(response);
}else{
alert(response);
}
},
});