调用php脚本onclick使用Jquery和刷新数据不重新加载页面


Call on php Script onclick using Jquery and Refresh the Data without Reloading the Page?

是这样的:我有php解析器,它将数据保存到一些本地文件中,这些文件是通过include加载页面后获取的。我想给用户一个刷新数据的机会,因此我需要解析器再次启动,并将新的数据字符串传递给页面的元素,而无需重新加载页面。

谁能帮帮我,我的大脑已经在融化了。可能我只需要一个简单的AJAX调用,但请不要省略适当的语法。

这是我的看法(可能是非常错误的):

<element id="button"></element>
<div id="new_data"></div>
<jquery>
$('#button').onclick(function() {
call for test.php, let it do it's job and get the $result_string
$('#new_data').innerHTML($result_string);
});
</jquery>

<?test.php
function parser() {
all in place;
return $result_string;
}
?>

注:还有一些与表单有关的东西,但也不能弄清楚。

解决! !

如果有人想问这个问题,下面是对我有用的方法:

    $("#button").bind('click', function() {
$.post("test.php", function(data) {
$("#data").html(data);
});
});

$("#button").bind('click', function() {
$('#data').load('test.php');
});

和在test.php只是一个简单的echo $result;在最后,工作完美,相当快。

希望有帮助!

我假设你需要一个ajax调用按钮点击将得到result_string,试试这个:

$('#button').onclick(function() {
    //using simple ajax post..
    $.post('test.php',function(data){
        $('#new_data').html(data); //here i am returning the data as HTML from test.php..
   });
});

如果你需要return_string作为javascript对象,那么你可以发送JSON作为响应:

function parser() {
    all in place;
    return echo json_encode(array('result'=>$result_string));
}
并在post 中将其作为对象获取
$.post('test.php',function(data){
    $('#new_data').html(data.result); 
});
虽然我对你的问题有点困惑,但我想这会让你开始…

这是ajax请求的语法。

   $('#button').onclick(function() {
      jQuery.ajax({ 
        type:'post',
        url:'test.php',
        data:[],
        dataType:'json',
        success: function(rs)
        {
          $('#new_data').innerHTML(rs.result_string);
         }
        failure : function(rs)
        {
            alert(rs.errorMesage);
        }
     });
  });