简体   繁体   中英

Running a PHP Script without redirecting or refreshing the page

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.

My script code is -

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

Thanks in advance.

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

HTML

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.

JavaScript

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

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.

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

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

Instead of <a href> , use ajax to pass the values to your php and get the result back-

$.post('updateRCstatus/test.html', { 'rxdtime': <?php ecdho $time ?>, OTHER_PARAMS },
  function(data) {
    alert(data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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