0

I have a form coded below

 <form id="language-registration">
   <?php
    global $wpdb;
  $results=$wpdb->get_results('SELECT * FROM keeled;', ARRAY_A);
  $keeled=$wpdb->get_results('SELECT `id`, `keel`, COUNT(keel) as `count` FROM `voorkeel` GROUP BY keel;', ARRAY_A);
  ?>
  <?php foreach($keeled as $k) { ?>
  <p>
  <?php echo $k['keel'] . " " . $k['count'] . "/20";?>
  </p>
  <?php }?>
  <p>
  <label for="name">Nimi</label>
  <input type="text" name="nimi" id="nimi" value=" " size="30" tabindex="1" />
  </p>
  <p>
  <label for="klass">Klass</label>
  <input type="text" name="klass" id="klass" size="30" value=" " tabindex="2" />
  </p>
  <p>
  <label for="keel">Keel</label>
  <select name="keel" id="keel">
  <option selected="selected">Vali keel</option>
  <?php
    foreach($results as $r) { ?>
      <option value="<?php echo $r['keel'] ?>"><?php echo $r['keel'] ?></option>
  <?php
    } ?>
</select> 
  </p>
  <p>
  <input type='button' id='submit' name='submit' value='Submit'/>
  </p>
  </form>

This is my ajax callback in function.php

function post_language(){
  $nimi = $_POST['nimi'];
  $klass = $_POST['klass'];
  $keel= $_POST['keel'];
  global $wpdb;
  $wpdb->insert(
    'voorkeel',
    array(
      'nimi' => $nimi,
      'klass' => $klass,
      'keel' => $keel
     ),
    array(
      '%s'
    )
  );

  die();
  return true;
  }
  //
  add_action('wp_ajax_post_post_language', 'post_post_language'); // Call when user logged in
  add_action('wp_ajax_nopriv_post_language', 'post_language'); // Call when user in not logged in
  function add_our_script() {

   wp_register_script( 'ajax-js', get_template_directory_uri() . '/scripts/keeled.js', array( 'jquery' ), '', true );
   wp_enqueue_script( 'ajax-js' );

   }
   add_action( 'wp_enqueue_scripts', 'add_our_script' );

this is my keeled.js

jQuery(document).ready(function() {
    jQuery("#submit").click(function() {
        var nimi = jQuery("#nimi").val();
        var klass = jQuery("#klass").val();
        var keel = jQuery("#keel").val();
        jQuery.ajax({
            cache: false,
            type: 'POST', // Adding Post method
            url: "/wp-admin/admin-ajax.php", // Including ajax file
            data: { "action": "post_language", "nimi": nimi, "klass": klass, "keel": keel }, // Sending data dname to post_word_count function.
            success: function(data) { // Show returned data using the function.
                alert(data);
            }
        });
    });
});

But nothing happens? i try first time to enter data into database via ajax in wordpress.

  • I don't think you meant to put post_post_langauge on the private add_action. change it to add_action('wp_ajax_post_language', 'post_language'); – Wilco Nov 24 '17 at 7:45
0

This way to insert data in db using AJAX works for me. Hope this help you.

Function scripts

function add_our_script() {
   wp_register_script( 'ajax-js', get_template_directory_uri() . '/scripts/keeled.js', array( 'jquery' ), '', true );
   wp_localize_script( 'ajax-js', 'ajax-js_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

}
add_action( 'wp_enqueue_scripts', 'add_our_script' );

Template Form:

<form>
    <label for="nimi">Nimi</label>
    <input name="nimi" id="nimi" type="text"/>

    <input id="nimi_submit" type="button" value="Send">
</form>

In keeled.js

jQuery('#nimi_submit').click( function () {

    var nimi = jQuery('#nimi').val();

    jQuery.ajax({
        url: ajax-js_ajax.ajax_url,
        type: "POST",
        data: {
            'action': 'post_language',
            'nimi': nimi,

        },
        success: function (data) {
            alert(data);
        }
    });

});

Callback action

function post_language(){

    global $wpdb;

    $nimi = isset( $_POST['nimi'] ) ? $_POST['nimi'] : '';

    $table = 'voorkeel';

    $wpdb->insert( $table, array('nimi' => $nimi));
}
add_action('wp_ajax_post_language', 'post_language');
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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