无法使用AJAX将JS变量获取到PHP [英] Not able to get JS variable to PHP using AJAX

查看:62
本文介绍了无法使用AJAX将JS变量获取到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wordpress网站(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.

我到达了使单选按钮可用的阶段.我使用jQuery ON事件来标识选中了哪个单选按钮,并在javascript变量中具有相应的值.我正在尝试使用ajax将这个JS变量传递给我的php. ajax的成功属性可以正常工作,但是$ _POST ['name']不显示该值.我试图在ajax的成功属性内使用.html(),但这只是将div元素替换为javascript变量的值.我在PHP代码中需要此JS变量值,以便可以运行一个条件,根据该条件可以确定需要在网站上显示的下拉列表.

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

根据收到的建议 我尝试了以下更改.仅当我在Chrome中使用Inspect时,我才能在输入type = hidden元素中看到该值.但是,使用查看源"并不会向我显示这些值.我想念的是什么?有人可以给些指导吗.

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

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

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

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") {

推荐答案

在WordPress中使用Ajax通常是通过以下方式完成的:

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

1-您必须将ajax数据提交到admin-ajax.php.有时,您可以使用WordPress为该URL预定义的全局js变量ajaxurl.但更可靠的是,我将使用admin_url( 'admin-ajax.php' )的输出.

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-您的数据对象必须包含action值,该值具有表示此ajax提交的唯一字符串.我在这里使用AJAX_ACTION:data = { action: 'AJAX_ACTION', your_var: 'your_val' };

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-通过使用包含您指定的操作名称的钩子接收数据:

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-用您的callback_function接收数据:

4- Use your callback_function to receive the data:

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

顺便说一句,在callback_function中使用echo将导致响应发送到成功函数.

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适用于没有WordPress帐户或未登录的访问者的提交,而wp_ajax_AJAX_ACTION适用于已登录的用户.

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.

请检查: WordPress Codex

这篇关于无法使用AJAX将JS变量获取到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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