简体   繁体   中英

AJAX GET Url Parameter and pass to PHP

My user will be access ../verify.php?code=1228119j8dwq8dj218

and i want the 1228119j8dwq8dj218 passed to /verify_action.php

I've tried something like below but it says code is undefined,

when i tried to place my verify-action.php script in the verify.php and change url:"verify-action.php" to url:"verify.php" it working perfectly but causing my success and error message not showing properly

AJAX

<script>
$(document).ready(function(){
        $.ajax({
                url:"verify-action.php",
                method:"GET",
                data:{code:code},
                success:function(data)
                {
                    if(data.error !== '')
                    {
                        $('#message').html(data.error);
                    }
                    if(data.success != '')
                    {
                        $('#message').html(data.success);
                    }
                }
            })
});
</script>

PHP

$error = '';
$success = '';

if(isset($_GET["code"]))
{
    $object->query = "
    UPDATE user_table 
    SET email_verify = 'Yes' 
    WHERE user_verification_code = '".$_GET["code"]."'
    ";

    $object->execute();

    $success = '<div class="alert alert-success">Your Email has been Verified</div>';
}else{
    $error = '<div class="alert alert-danger">Verify Error. Please Contact Administrator</div>';
}

$output = array(
            'error'     =>  $error,
            'success'   =>  $success
        );
        echo json_encode($output);
?>
const urlParams = new URLSearchParams(window.location.search);
var code = urlParams.get('code');

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