简体   繁体   English

如何在php的同一页面上从ajax获取价值

[英]How to get value from ajax on same page of php

I want to get value from ajax on same page i used html php and javascript我想在我使用 html php 和 javascript 的同一页面上从 ajax 获取价值

<script>
$(document).ready(function() {
    $('#godown').change(function() {
        var gownid = $(this).val();
        alert(gownid);
        $.ajax({
            url: '../inc/safedispatch_detail.class.php',
            method: 'GET',
            data: {gownid:gownid},
            success: function(result){
            }
        });
    })
});


</script>

and my php code is:我的 php 代码是:

echo $_POST['gownid'];

showing me blank output.给我看空白输出。 Please guide whats wrong in this请指导这有什么问题

Your method is GET so if your code is :你的方法是GET所以如果你的代码是:

echo $_POST['gownid'];

It will not work ;)不起作用 ;)

  • change your method to POST or your php code to将您的方法更改为 POST 或将您的 php 代码更改为

    echo $_GET['gownid']; echo $_GET['gownid'];

  • then you can test the value returned with developper tools or :然后您可以测试使用开发人员工具返回的值或:

    success: function(result) { alert(result);成功:功能(结果){警报(结果); } }

or或者

success: function(result) {
console.log(result);
}
  • if the result is what you expect, you can ie write the php returned html in your html page ie如果结果是你所期望的,你可以在你的 html 页面中编写 php 返回的 html ie

    success: function(result) { $('#myid').text(result);成功:函数(结果){ $('#myid')。文本(结果); // for ap paragraph field } // 对于 ap 段落字段 }

or或者

success: function(result) {
$('#myid').val(result); // for an input field
}

What is good with js ajax request is to have your method type as 'POST'. js ajax 请求的好处是将您的方法类型设为“POST”。

so if in your jquery change event your alert is not empty then in your php it will just work fine.因此,如果在您的 jquery 更改事件中,您的警报不为空,那么在您的 php 中它会正常工作。

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

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