在ajax调用之后使用php变量

Use a php variable after an ajax call

本文关键字:php 变量 之后 ajax 调用      更新时间:2023-09-26

很抱歉,如果这是一个愚蠢的问题。我是Ajax的新手。我试图改变验证凭证代码的Wordpress插件代码。代码在javascript中加载,从输入的html字段调用它的id。

html是这样的:

<input type="text" id="woo_vou_voucher_code" name="woo_vou_voucher_code" value="" /> 

和js

    jQuery( document ).ready( function( $ ) {
// Check Voucher code is valid or not
    $( document ).on( 'click', '#woo_vou_check_voucher_code', function() {
        //Voucher Code
        var voucode = $( '#woo_vou_voucher_code' ).val();
        if( voucode == '' || voucode == 'undefine' ) {
            //hide submit row
            $( '.woo-vou-voucher-code-submit-wrap' ).fadeOut();
            $( '.woo-vou-voucher-code-msg' ).removeClass( 'woo-vou-voucher-code-success' ).addClass( 'woo-vou-voucher-code-error' ).html( WooVouCheck.check_code_error ).show();
        } else {
            //show loader
            $( '.woo-vou-check-voucher-code-loader' ).css( 'display', 'inline' );
            //hide error message
            $( '.woo-vou-voucher-code-msg' ).hide();
            var data = {
                            action  : 'woo_vou_check_voucher_code',
                            voucode : voucode
                        };
            //call ajax to chcek voucher code
            jQuery.post( WooVouCheck.ajaxurl, data, function( response ) {
                //alert( response );
                if( response == 'success' ) {
                    //show submit row
                    $( '.woo-vou-voucher-code-submit-wrap' ).fadeIn();
                    $( '.woo-vou-voucher-code-msg' ).removeClass( 'woo-vou-voucher-code-error' ).addClass( 'woo-vou-voucher-code-success' ).html( WooVouCheck.code_valid ).show();

                }

它工作得很好,但我现在想要的是,在div '。我可以回显js文件中使用的voucode变量。例如:

<div class="woo-vou-voucher-code-submit-wrap"><?php echo $voucode; ?><div>

我不知道如何做到这一点。

编辑:对不起,我只使用echo为例。我需要的是使用$voucode内的其他php函数。

<?php
                global $wpdb;
                $querystr = "SELECT post_id
                FROM $wpdb->postmeta
                WHERE
                (meta_key = '_woo_vou_purchased_codes' AND meta_value = '$voucode')";
                $postid = $wpdb->get_var($wpdb->prepare($querystr));?><div class="woo-vou-voucher-code-submit-wrap"><?php echo $postid; ?><div>
$('.woo-vou-voucher-code-submit-wrap').text(voucode);

既然您将您的代码发布到php脚本中,您的代码将在$_POST['voucode']中,那么您可以随意使用它。然后回显$postid以及成功消息

PHP

<?php
$voucode = $_POST['voucode'];
//do whatever processing
//..
if($validCode){
    echo "success,".$postid;
}

JS

//check that success is the first part of the response
if( response.indexOf('success') === 0 ) {
    var postid = response.split(",")[1];
    $( '.woo-vou-voucher-code-submit-wrap' ).text(postid).fadeIn();
}

这只是一个简单的例子。您可以使用其他响应类型,如JSON,这样您就不必进行字符串分割或其他操作。

PHP在收到请求后呈现页面。PHP在服务器上运行,因此一旦在用户的浏览器上加载了页面,就无法执行PHP代码。您需要使用JavaScript,在本例中使用jQuery。

修改代码,当您想要添加变量的值时,只需使用jQuery函数添加它。

$(".woo-vou-voucher-code-submit-wrap").text(voucode);

如果是表单的输入字段,则使用val函数。

$(".woo-vou-voucher-code-submit-wrap").val(voucode);