dougan1205 2016-01-01 22:26
浏览 57
已采纳

无法使用AJAX将JS变量传递给PHP

I have a requirement in a wordpress website (PHP) where on select of a dropdown, two radio buttons show up and depending on the radio button selected a select dropdown will be made available.

I reached the stage where I have the radio buttons are made available. I use jQuery ON event to identify which radio button is checked and have the respective value in a javascript variable. I am trying to pass this JS variable to my php using ajax. Success attribute of ajax works fine but $_POST['name'] does not show the value. I tried to use .html() inside the success attribute of ajax but that just replaces my div element with the value of javascript variable. I need this JS variable value in my PHP code so that I can run a condition based on which I decide which dropdown I need to display on the website.

I have been trying a solution since few days but not able to find a solution. Request some guidance

Edit: Based on the suggestion received I tried the following changes. I see the value in my input type=hidden elements but only if I use Inspect in Chrome. However using View Source does not show me these values.. What am I missing? Can someone please give some guidance..

Ajx-script.js

$.ajax({
  type : "POST",
  url : myAjaxData.ajaxurl,
  data : {
  action : "sProdOrRegion_ajax_action",
  selectedProdReg : selectedProdReg                     
  },
  success : function(data) {
   $("#radioValueHidd1").val(selectedProdReg);                  
   // $('#stakeholderParentData').load( urlValue + " " +"#stakeholderData");
   //$("input[id=radioValueHidd3][value="+ selectedProdReg +"]").html();
   $("input[id=radioValueHidd2]").val(selectedProdReg);
 }
});

functions.php

add_action('wp_enqueue_scripts', function() {
wp_enqueue_script('my-ajax', get_template_directory_uri() . '/library/js/ajx-script.js', array('jquery') );
wp_localize_script(
'my-ajax',
'myAjaxData',
array( 'ajaxurl' => admin_url('admin-ajax.php') )
);
});

add_action( 'wp_ajax_singleIdeaProdOrRegion_ajax_action', 'callback_singleIdeaProdOrRegion' );
add_action('wp_ajax_singleIdeaProdOrRegion_ajax_action', 'callback_singleIdeaProdOrRegion');

function callback_singleIdeaProdOrRegion() {
    if(isset($_POST['selectedProdReg'])) {
        $selectedProdReg =  $_POST['selectedProdReg'];
        $selectedProdReg1 =  $_POST['selectedProdReg'];
        die();
    }

}

single-car.php

<div id="stakeholderParentData" class="stakeholderParentData">
                                <div id="stakeholderData" class="stakeholderData">
                                <?php $selectedProdReg = $wpdb->escape($_POST['selectedProdReg']); ?>



                            <?php


                                if (isset ( $selProdReg )) {
                                    if ($selProdReg === "custom_post_prod_company") {
  • 写回答

2条回答 默认 最新

  • dqwh1209 2016-01-02 07:05
    关注

    Using Ajax in WordPress is usually done in the following manners:

    1- You have to submit your ajax data to the admin-ajax.php. Sometimes you can use the global js variable ajaxurl, predefined by WordPress for that URL. But more reliably I'd use the output of admin_url( 'admin-ajax.php' ).

    2- Your data object must contain an action value that have a unique string to represent this ajax submission. I'll use AJAX_ACTION here: data = { action: 'AJAX_ACTION', your_var: 'your_val' };

    3- Receive the data by using hooks that contain the action name that you've specified:

    add_action( 'wp_ajax_AJAX_ACTION', 'callback_function' );
    add_action( 'wp_ajax_nopriv_AJAX_ACTION', 'callback_function' ); 
    

    4- Use your callback_function to receive the data:

    function callback_function() {
        // $_POST['your_var'] is accessible here
    }
    

    By the way, using echo inside the callback_function would result in the response being sent to the success function.

    wp_ajax_nopriv_AJAX_ACTION is for submission for visitors that do not have the WordPress account or are not logged in, while wp_ajax_AJAX_ACTION is for logged in users.

    Please check: WordPress Codex

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置