0

I am trying to enter insert data with form in wordpress and want to use ajax for that. Without ajax it works fine but when I use ajax pressing the submit button gives 0 in result and the data is not inserted to the database . Below is my code.

Html code is between form tags as below

<form id="insertform" action="" method="post">
<input id="user" style="height: 30px;" name="user" type="text" />
.
.
.
<button id="Submit" type="submit">SUBMIT </button>
</form>

php code

function wp_insert(){
if(isset($_POST['skillname'])!= '' && $_POST['Address']!= ''){
    $user_info = wp_get_current_user();
$username=$user_info->user_login;
//insert to database

If($_POST['Submit']) {

// run validation if you're not doing it in js
global $wpdb;

//assigning textbox values to variables

$yourname=$_POST['user'];
$lat=$_POST['latitude'];
$long=$_POST['longitude'];
$address=$_POST['Address'];
$city=$_POST['City'];
$state=$_POST['State'];
$country=$_POST['Country'];
$zip=$_POST['zipcode'];
$skillname=$_POST['skillname'];
$yourself=$_POST['yourself'];
$email=$_POST['email'];
$phone=$_POST['phone'];


if($wpdb->insert(
        'wp_store_locator',
        array(
                'secretcode' =>$username,
                'sl_description' => $yourname,
                'sl_latitude' => $lat,
                'sl_longitude' => $long,
                'sl_address' => $address,
                'sl_city' => $city,
                'sl_state' => $state,
                'sl_country' => $country,
                'sl_zip' => $zip,
                'sl_store' => $skillname,
                'yourself' => $yourself,
                'sl_email' => $email,
                'sl_phone' => $phone )) == false) wp_die('Database Insertion failed'); else echo 'Database insertion successful'; exit();

}

}

}//end of function

Enqueue code for Jquery

add_action( 'init', 'add_insert' ); 

function add_insert(){  

add_action( 'wp_ajax_wp_insert', 'wp_insert' );
add_action( 'wp_ajax_nopriv_wp_insert', 'wp_insert');
// register & enqueue a javascript file called globals.js
wp_register_script( 'globalss', get_stylesheet_directory_uri() . "/js/ajaxinsert.js", array( 'jquery' ) ); 
wp_enqueue_script( 'globalss' );

// use wp_localize_script to pass PHP variables into javascript
wp_localize_script( 'globalss', 'yess', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}  

Jquery File ajaxinsert.js

jQuery(function ($) {
    $("#insertform").submit(function (e) { //form is intercepted
        e.preventDefault();

        //serialize the form which contains secretcode
        var sentdataa = $(this).serializeArray();

        //Add the additional param to the data        
        sentdataa.push({
            name: 'action',
            value: 'wp_insert'
        })

        //set sentdata as the data to be sent
        $.post(yess.ajaxurl, sentdataa, function (rez) { //start of funciton
            alert(rez);

            return false;
        } //end of function
        ,
        'html'); //set the dataType as json, so you will get the parsed data in the callback
    }); // submit end here
});

Thanks !

  • 2
    I wouldn't call it wp_insert, it's a very generic name and could cause clashes, try zeeshan_insert_store_locator and avoid reusing the Core prefixes for non-core code – Tom J Nowell Feb 26 '15 at 14:37
  • Could you please format your code in a way that it is readable? Line breaks and intending properly, etc. would help a lot. – kaiser Feb 26 '15 at 16:40
1

Your AJAX callback method should be outputting something followed by a die() statement.

function wp_insert() {
    ..your code
    echo $whatever_your_results_are;
    //
    die();
}

I would also recommend against prefixing your custom methods with wp_. That should be reserved for WordPress and will cause confusion to other developers - and probably you 6 months down the road :)

Hope this helps

  • I am still getting zero I have added that – Zeeshan Feb 26 '15 at 14:43
  • what are you returning? – Welcher Feb 26 '15 at 14:45
  • Thanks everybody my problem is solved I had to change the below line ... below is the corrected version ... thanks everybody who helped me thanks add_action( 'wp_ajax_insertfunction', 'insertfunction' ); add_action( 'wp_ajax_nopriv_insertfunction', 'insertfunction'); – Zeeshan Feb 26 '15 at 16:18

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.