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

this is my script

<?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>

and getdata.php

$no_hpe=$_POST['no_hpe'];
$sql = "SELECT * FROM hpe_material where no_hpe='$no_hpe'";

but, my problem is $no_hpe is empty.how would I do this? thanks.

Try this

  $.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.")
  })

This will send the data you want across to the php file using post

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);
}
},
});

This might be helps you