0

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") {
6
  • post what you have so far Commented Jan 1, 2016 at 22:31
  • Looks like your urlVal does not call the single-car.php script, given value is http:localhost/car/my-future-car/ Commented Jan 1, 2016 at 22:40
  • @maxhb, they could be using urlrewriting. @rbpjava, What is the value of the javscript variable selRegProd? Commented Jan 1, 2016 at 22:42
  • @Untitled123: I do not have the complete code now but this is the code I had written last. The above ajax would replace the my html form div id RegionProductID with the expected JS value. But not to the PHP code. Commented Jan 1, 2016 at 22:44
  • @Patrick Evans : it is the input type="radio" value that I want to pass from JS to PHP. Commented Jan 1, 2016 at 22:48

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Tried your method but the form data is still shown in admin-ajax.php and not my custom for php..
Define "my custom for php." Do you mean your plugin or theme? That's what the hooks are used for: add_action( "wp_ajax_**AJAX_ACTION**'" 'callback_function' ); add_action( wp_ajax_nopriv_**AJAX_ACTION**", 'callback_function' ); PLease note the AJAX_ACTION serve as placeholder to the value you've to your action in data. Also I'd try to use $.post or $.get. You know that WordPress uses jQuery instead of $ right? Such as jQuery.post instead of $.post.
0

I am not sure if these will help you but I have 2 ideas. First is you might be getting the value of radio button false. Second is maybe you should write your data like this.

data: { "selRegProd" : selRegProd },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.