Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have problem. I don't know why but my paramters from form don't pass too aad_process_ajax().

My code from functions and jquery:

Function with my form:

function aad_render_admin() {

    ?>
    <div class="wrap">
        <h2><?php _e('Admin Ajax Demo', 'aad'); ?></h2>
            <div class="wrap">
        <form id="aad-form" action="" method="POST">
            <div>
<input type="text" name="name" />
<input type="text" name="header" />
<input type="text" name="body" />
<input type="text" name="urls" />
                <input type="submit" name="aad-submit" id="aad_submit" class="button-primary" value="<?php _e('Get Results', 'aad'); ?>"/>
                <img src="<?php echo admin_url('/images/wpspin_light.gif'); ?>" class="waiting" id="aad_loading" style="display:none;"/>
            </div>
        </form>
        <div id="aad_results"></div>
    </div>
    <?php
}

Jquery code:

jQuery(document).ready(function($) {
    $('#aad-form').submit(function() {
        $('#aad_loading').show();
        $('#aad_submit').attr('disabled', true);

      data = {
          action: 'aad_get_results',
          aad_nonce: aad_vars.aad_nonce
      };

         $.post(ajaxurl, data, function (response) {
            $('#aad_results').html(response);
            $('#aad_loading').hide();
            $('#aad_submit').attr('disabled', false);
        });    

        return false;
    });
});

And this function don't get my parameters why?

function aad_process_ajax() {
    global $wpdb;

    echo $name = $_POST['name'];
    $header = $_POST['header'];
    $body = $_POST['body'];
    $urls = $_POST['urls'];
    echo 'xxxx';

        $wpdb->insert(  
        'wp_myprojects',
            array(    'name' => $name,
                    'header' => $header,
                    'body' => $body,
                    'urls' => $urls,
                    ),
            array(    '%s',
                    '%s',
                    '%s',
                    '%s'
                    )
        );

    $wpdb->show_errors();
    /*EVRY PROCESS AJAX MUST DIE!!!!*/
    die();
    /*EVRY PROCESS AJAX MUST DIE!!!!*/
}
add_action('wp_ajax_aad_get_results','aad_process_ajax');

Tell my why parameters don't show in function aad_process_ajax().

Problem is in Jquery or Wordpress?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

In your jQuery code

 data = {
      action: 'aad_get_results',
      aad_nonce: aad_vars.aad_nonce
  };

you are passing only action and aad_nonce, you are not passing form data.

If you want pass variables, you ave to pass it with ajax, use something like:

  data = {
      action: 'aad_get_results',
      aad_nonce: aad_vars.aad_nonce,
      form_data: $('#aad-form').serialize()
  };

After that, in your aad_process_ajax function use:

function aad_process_ajax() {

  parse_str($_POST['form_data'], $form_data);
  $name = isset($form_data['name']) ? : '';
  $header = isset($form_data['header']) ? : '';
  $body = isset($form_data['body']) ? : '';
  $urls = isset($form_data['urls']) ? : '';

  // the rest of your code here

}
share|improve this answer
    
i try it........ –  user33913 Aug 20 '13 at 20:46
    
when i call echo $name = isset($form_data['name']) ? : ''; I got 1? Edit: Ok Thx work thanks:) –  user33913 Aug 20 '13 at 21:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.