在不重定向或刷新页面的情况下运行PHP脚本 [英] Running a PHP Script without redirecting or refreshing the page

查看:201
本文介绍了在不重定向或刷新页面的情况下运行PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP和Javascript非常陌生. 现在,我通过使用来运行PHP脚本,但是它将重定向到另一个页面. 代码是

I am very new to PHP and Javascript. Now I am running a PHP Script by using but it redirect to another page. the code is

<a name='update_status' target='_top'
href='updateRCstatus.php?rxdtime=".$time."&txid=".$txid."&balance=".$balance."&ref=".$ref."'>Update</a>

如何在不重定向到另一个页面的情况下执行此代码,并弹出成功和失败警报消息.

How do I execute this code without redirecting to another page and get a popup of success and fail alert message.

我的脚本代码是-

<?PHP
$rxdtime=$_GET["rxdtime"];
$txid=$_GET["txid"];
$balance=$_GET["balance"];
$ref=$_GET["ref"];
-------- SQL Query --------
?>

谢谢.

推荐答案

您将需要使用 AJAX 为此.这是一个简单的示例:

You will need to use AJAX to do this. Here is a simple example:

就像您在问题中一样,只是一个简单的链接.但是,我将对结构进行一些修改以使其更整洁:

Just a simple link, like you have in the question. However I'm going to modify the structure a bit to keep it a bit cleaner:

<a id='update_status' href='updateRCstatus.php' data-rxdtime='$time' data-txid='$txid'  data-balance='$balance' data-ref='$ref'>Update</a>

我在这里假设这段代码是带有内插变量的双引号字符串.

I'm assuming here that this code is a double-quoted string with interpolated variables.

自从您标记了jQuery ...我将使用jQuery:)

Since you tagged jQuery... I'll use jQuery :)

浏览器将在链接上侦听click事件,并对适当的URL执行AJAX请求.当服务器发回数据时,将触发成功功能. 在jQuery文档中了解有关.ajax()的更多信息.

The browser will listen for a click event on the link and perform an AJAX request to the appropriate URL. When the server sends back data, the success function will be triggered. Read more about .ajax() in the jQuery documentation.

如您所见,我正在使用.data()来获取GET参数.

As you can see, I'm using .data() to get the GET parameters.

$(document).ready(function() {
    $('#update_status').click(function(e) {
        e.preventDefault(); // prevents the default behaviour of following the link

        $.ajax({
            type: 'GET',
            url: $(this).attr('href'),
            data: {
                rxdtime: $(this).data('rxdtime'),
                txid: $(this).data('txid'),
                balance: $(this).data('balance'),
                ref: $(this).data('ref')
            },
            dataType: 'text',
            success: function(data) {
                // do whatever here
                if(data === 'success') {
                    alert('Updated succeeded');
                } else {
                    alert(data); // perhaps an error message?
                }
            }
        });
    });
});

PHP

好像您知道自己在这里做什么.重要的是输出适当的数据类型.

PHP

Looks like you know what you're doing here. The important thing is to output the appropriate data type.

<?php
$rxdtime=$_GET["rxdtime"];
$txid=$_GET["txid"];
$balance=$_GET["balance"];
$ref=$_GET["ref"];

header('Content-Type: text/plain; charset=utf-8');

// -------- SQL Query -------
// your logic here will vary

try {
    // ...
    echo 'success';
} catch(PDOException $e) {
    echo $e->getMessage();
}

这篇关于在不重定向或刷新页面的情况下运行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆