jQuery的AJAX函数来调用PHP脚本返回一个值?脚本、函数、jQuery、AJAX

2023-09-10 19:03:30 作者:一杯柠檬薄荷水

感谢您看我的问题,希望你能帮助我为我是一个不精通编写JavaScript功能,是比较新的使用jQuery ...

Thank you for looking at my issue, hopefully you can help me out as I'm an not well versed in writing JavaScript functions and am relatively new to using jQuery...

不管怎么说,我需要一种方法来使用jQuery的Ajax调用异步调用PHP脚本将在头元素返回一个值,并更新值。

Anyways, I need a way to use jQuery's ajax call to asynchronously call a php script which will return a value and update the value in a header element.

任何人都知道正确的方法去这样做?

Anyone know of the proper way to go about doing this?

推荐答案

jQuery的:

$.post('url_to_script',{"anyData": "that is needed"}, function(data){
    $('#headertarget').text('data');
});

PHP:

$anyData = $_POST['anyData'];
function getAnswer ($inp){
    //logic goes here
    return "a string of some sort";
}
echo getAnswer($anyData);
exit;

这将让你发送回客户端进入标题元素的文字。

It will put any text you send back to the client into the heading element.