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

时间:2016-10-20 08:21:54

标签: javascript php

这是我的剧本

    $scope.CMComboOptions = {
        dataTextField: "EmployeeFullName",
        dataValueField: "UserId",

        filter: "contains",
     filtering:
           function(e){
               var selectedValue = $('#combocontactmanager').closest("span").children("span").children("input").val(); // comboBox.value();

                           if (selectedValue.length > 0) {
                               var filter = new Array();
                               if (!isNaN(selectedValue)) {
                                   var temp = selectedValue.toString().toLowerCase();
                                   filter.push({ field: "UserId", operator: "contains", value: temp });
                               } else {
                                   filter.push({ field: "EmployeeFullName", operator: "contains", value: selectedValue });
                               }
                               dorDataSource.filter(filter);
                           } else {
                               dorDataSource.filter([]);
                          }
               //},
           },


        dataSource: dorDataSource,
        clearButton: false,
        change: function (e) {
              debugger;

            var widget = e.sender;

            if (widget.value() && widget.select() === -1) {
                // console.log(widget.value())
                //custom has been selected
               // widget.value(""); //reset widget
                jQuery("#combocontactmanager").data("kendoComboBox").value("")
                $scope.cMVal = "";


            } 
        },
        headerTemplate: '<div class="dropdown-header">' +
'<span class="k-widget k-header">Id</span>' +
'<span class="k-widget k-header">Employee Name</span>' +
'</div>',
        template: '<span class="k-state-default">#: UserId#</span>' +
'<span class="k-state-default">#: EmployeeFullName#</span>',
    }

和getdata.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>

但是,我的问题是$no_hpe=$_POST['no_hpe']; $sql = "SELECT * FROM hpe_material where no_hpe='$no_hpe'"; 是空的。我会这样做吗?感谢。

2 个答案:

答案 0 :(得分:1)

试试这个

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

这将使用post

将您想要的数据发送到php文件

答案 1 :(得分:0)

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

这可能对您有所帮助