Ajax submit returns undefined value no data submitted
-
hi, my problem is about submitting data using ajax. It wont store data into the database nor return a data. Can anybody tells me what is wrong with my code
<script type="text/javascript"> jQuery(document).ready(function() { jQuery('.rate-value').change(function(){ value = jQuery(this).val(); jQuery('#loader h4').fadeOut('fast'); jQuery('#loader img').fadeIn('fast'); send_rate_surveys(value); }); jQuery('#filter-by').change(function(){ change_filtering(); }); jQuery('#filter-btn').click(function(){ jQuery('#loader h4').fadeOut('fast'); jQuery('#loader img').fadeIn('fast'); send_filter(); }); jQuery('#show-all').click(function(){ jQuery('#loader h4').fadeOut('fast'); jQuery('#loader img').fadeIn('fast'); show_all(); }); jQuery('#rate-this-product').click(function(){ jQuery('.ratings-container').fadeOut('fast'); jQuery('.ratings-btn').fadeIn('fast'); r_height = jQuery('.ratings-btn').height(); jQuery('.ratings-container-wrap').css('height',r_height+'px'); }); jQuery('#no-rate-this-product').click(function(){ alert("Please complete the survey information in your account to show you are qualified to rate this product."); }); jQuery('#login-rate-this-product').click(function(){ alert("Please login to rate this product."); }); jQuery('#filter-product-rates').click(function(){ jQuery('.ratings-container').fadeOut('fast'); jQuery('.ratings-filtering').fadeIn('fast'); f_height = jQuery('.ratings-filtering').height(); jQuery('.ratings-container-wrap').css('height',f_height+'px'); }); jQuery('#no-filter-product-rates').click(function(){ alert("Please login to sort research results."); }); }); function show_all(){ var data = {}; data.user_id = "20"; data.product_id = "1281"; data.action = "get_show_all"; // The action that needs to be in the request to let WordPress hooks with a callback jQuery.post('https://newsite.cancerclan.com/wp-admin/admin-ajax.php',data, function(results){ var all_obj = jQuery.parseJSON(results); jQuery('#loader img').fadeOut('fast'); jQuery('#loader h4').fadeIn('fast'); jQuery('#good-bar').css('width', all_obj.good+'%'); jQuery('#bad-bar').css('width', all_obj.bad+'%'); jQuery('#indifferent-bar').css('width', all_obj.indifference+'%'); jQuery('#good-bar').text(all_obj.good+"%"); jQuery('#bad-bar').text(all_obj.bad+"%"); jQuery('#indifferent-bar').text(all_obj.indifference+"%"); if(all_obj.good === '0.00'){ jQuery('#good-bar').text(''); } if(all_obj.bad === '0.00'){ jQuery('#bad-bar').text(''); } if(all_obj.indifference === '0.00'){ jQuery('#indifferent-bar').text(''); } jQuery('#product-ratings-wrap span:nth-child(1)').text(all_obj.total_count + ' Members rated this product'); jQuery('#product-ratings-wrap span:nth-child(2)').text(all_obj.good+"% Says it's Good"); jQuery('#product-ratings-wrap span:nth-child(3)').text(all_obj.bad+"% Says it's Bad"); jQuery('#product-ratings-wrap span:nth-child(4)').text(all_obj.indifference+"% Says it's Indifferent"); }); } function send_filter(){ var data = {}; data.user_id = "20"; data.product_id = "1281"; data.person_status = jQuery('#self-description').val(); data.filter_by = jQuery('#filter-by').val(); data.cancer_types = jQuery('#cancer-types').val(); data.years_diagnosed = jQuery('#years-diagnosed').val(); data.years_begin = jQuery('#years-of-age option:selected').attr('id'); data.years_of_age = jQuery('#years-of-age').val(); data.action = "get_filter"; jQuery.post('https://newsite.cancerclan.com/wp-admin/admin-ajax.php',data, function(results){ var filter_obj = jQuery.parseJSON(results); jQuery('#loader img').fadeOut('fast'); jQuery('#loader h4').fadeIn('fast'); jQuery('#good-bar').css('width', filter_obj.good+'%'); jQuery('#bad-bar').css('width', filter_obj.bad+'%'); jQuery('#indifferent-bar').css('width', filter_obj.indifference+'%'); jQuery('#good-bar').text(filter_obj.good+"%"); jQuery('#bad-bar').text(filter_obj.bad+"%"); jQuery('#indifferent-bar').text(filter_obj.indifference+"%"); if(filter_obj.good === '0.00'){ jQuery('#good-bar').text(''); } if(filter_obj.bad === '0.00'){ jQuery('#bad-bar').text(''); } if(filter_obj.indifference === '0.00'){ jQuery('#indifferent-bar').text(''); } jQuery('#product-ratings-wrap span:nth-child(1)').text(filter_obj.total_count + ' Members rated this product'); jQuery('#product-ratings-wrap span:nth-child(2)').text(filter_obj.good+"% Says it's Good"); jQuery('#product-ratings-wrap span:nth-child(3)').text(filter_obj.bad+"% Says it's Bad"); jQuery('#product-ratings-wrap span:nth-child(4)').text(filter_obj.indifference+"% Says it's Indifferent"); }); } function change_filtering(){ val = jQuery('#filter-by').val(); if(val == 'cancer-type'){ jQuery('.filter-container').fadeOut('fast'); jQuery('#cancer-types-list').fadeIn('fast'); }else if(val == 'years-diagnosed'){ jQuery('.filter-container').fadeOut('fast'); jQuery('#diagnosed-years-list').fadeIn('fast'); }else if(val == 'years-of-age'){ jQuery('.filter-container').fadeOut('fast'); jQuery('#years-of-list').fadeIn('fast'); } } function send_rate_surveys(value){ var data = {}; data.user_id = "20"; data.product_id = "1281"; data.rated_value = value; data.action = "survey"; jQuery.post('https://newsite.cancerclan.com/wp-admin/admin-ajax.php',data, function(results){ var obj = jQuery.parseJSON(results); jQuery('#loader img').fadeOut('fast'); jQuery('#loader h4').fadeIn('fast'); jQuery('#good-bar').css('width', obj.good+'%'); jQuery('#bad-bar').css('width', obj.bad+'%'); jQuery('#indifferent-bar').css('width', obj.indifference+'%'); jQuery('#good-bar').text(obj.good+"%"); jQuery('#bad-bar').text(obj.bad+"%"); jQuery('#indifferent-bar').text(obj.indifference+"%"); if(obj.good === '0.00'){ jQuery('#good-bar').css('display','none'); } if(obj.bad === '0.00'){ jQuery('#bad-bar').css('display','none'); } if(obj.indifference === '0.00'){ jQuery('#indifferent-bar').css('display','none'); } jQuery('#product-ratings-wrap span:nth-child(1)').text(obj.total_count + ' Members rated this product'); jQuery('#product-ratings-wrap span:nth-child(2)').text(obj.good+"% Says it's Good"); jQuery('#product-ratings-wrap span:nth-child(3)').text(obj.bad+"% Says it's Bad"); jQuery('#product-ratings-wrap span:nth-child(4)').text(obj.indifference+"% Says it's Indifferent"); }); } </script>
The script handler in my functions.php is this
/*************************** SURVEY FILTERING ***************************/ add_action('wp_ajax_survey', 'survey'); // When user login add_action('wp_ajax_nopriv_survey', 'survey'); // When user not loggin function survey(){ global $wpdb; $user_id = $_POST['user_id']; $product_id = $_POST['product_id']; $rated_value = $_POST['rated_value']; if($rated_value == 'good'){ $good_points = 1; }else{ $good_points = 0; } if($rated_value == 'bad'){ $bad_points = 1; }else{ $bad_points = 0; } if($rated_value == 'indifferent'){ $indifferent_points = 1; }else{ $indifferent_points = 0; } $check_product = "SELECT * FROM wp_survey_ratings WHERE product_id = '{$product_id}'"; $result = $wpdb->get_row($check_product); $check_user = "SELECT * FROM wp_survey_ratings_meta WHERE product_id = '{$product_id}' AND user_id = '{$user_id}'"; $row_user = $wpdb->get_row($check_user); $prev_rated = $row_user->rated_value; if(empty($row_user)) { $insert_user = "INSERT INTO wp_survey_ratings_meta (product_id, user_id, rated_value, date) values ('{$product_id}', '{$user_id}', '{$rated_value}', NOW())"; $wpdb->query($insert_user); }else{ $update_user = "UPDATE wp_survey_ratings_meta SET rated_value = '{$rated_value}', date = NOW() WHERE user_id = '{$user_id}' AND product_id = '{$product_id}'"; $wpdb->query($update_user); if($rated_value == ''){ $update_rates = "UPDATE wp_survey_ratings SET good = good + '{$good_points}', bad = bad + '{$bad_points}' , indifference= indifference + '{$indifferent_points}' WHERE product_id = '{$product_id}'"; $wpdb->query($update_rates); }else{ if($prev_rated == 'good'){ $update_rates = "UPDATE wp_survey_ratings SET good = good -1, bad = bad + '{$bad_points}' , indifference= indifference + '{$indifferent_points}' WHERE product_id = '{$product_id}'"; }else if($prev_rated == 'bad'){ $update_rates = "UPDATE wp_survey_ratings SET good = good + '{$good_points}', bad = bad -1 , indifference= indifference + '{$indifferent_points}' WHERE product_id = '{$product_id}'"; }else if($prev_rated == 'indifferent'){ $update_rates = "UPDATE wp_survey_ratings SET good = good + '{$good_points}', bad = bad + '{$bad_points}' , indifference= indifference -1 WHERE product_id = '{$product_id}'"; } $wpdb->query($update_rates); } } if(empty($row_user)) { if(empty($result)){ $sql = "INSERT INTO wp_survey_ratings (product_id, good, bad, indifference) values ('{$product_id}','{$good_points}','{$bad_points}','{$indifferent_points}')"; $wpdb->query($sql); }else{ $update_rates = "UPDATE wp_survey_ratings SET good = good + '{$good_points}', bad = bad + '{$bad_points}' , indifference= indifference + '{$indifferent_points}' WHERE product_id = '{$product_id}'"; $wpdb->query($update_rates); } } $rates_results = "SELECT * FROM wp_survey_ratings WHERE product_id = '{$product_id}'"; $row = $wpdb->get_row($rates_results); $total_ratings = $row->good + $row->bad + $row->indifference; if($row->good != 0){ $good = ($row->good / $total_ratings) * 100 . "%"; }else{ $good = 0; } if($row->bad != 0){ $bad = ($row->bad / $total_ratings) * 100 ."%"; }else{ $bad = 0; } if($row->indifference != 0){ $indifference = ($row->indifference / $total_ratings) * 100 ."%"; }else{ $indifference = 0; } if(empty($total_ratings)){ $total_ratings = 0; } $good = number_format($good, 2, '.', ''); $bad = number_format($bad, 2, '.', ''); $indifference = number_format($indifference, 2, '.', ''); echo json_encode(array('total_count' => $total_ratings, 'good' => $good, 'bad' => $bad, 'indifference' => $indifference)); die(); } /******************************** SHOW FILTERING ***********************************/ add_action('wp_ajax_get_show_all', 'get_show_all'); // When user login add_action('wp_ajax_nopriv_get_show_all', 'get_show_all'); // When user not loggin function get_show_all(){ global $wpdb; $product_id = $_POST['product_id']; $rates_results = "SELECT * FROM wp_survey_ratings WHERE product_id = '{$product_id}'"; $row = $wpdb->get_row($rates_results); $total_ratings = $row->good + $row->bad + $row->indifference; if($row->good != 0){ $good = ($row->good / $total_ratings) * 100; }else{ $good = 0; } if($row->bad != 0){ $bad = ($row->bad / $total_ratings) * 100; }else{ $bad = 0; } if($row->indifference != 0){ $indifference = ($row->indifference / $total_ratings) * 100; }else{ $indifference = 0; } if(empty($total_ratings)){ $total_ratings = 0; } $good = number_format($good); $bad = number_format($bad); $indifference = number_format($indifference); echo json_encode(array('total_count' => $total_ratings, 'good' => $good, 'bad' => $bad, 'indifference' => $indifference)); die(); } add_action('wp_ajax_get_filter', 'get_filter'); // When user login add_action('wp_ajax_nopriv_get_filter', 'get_filter'); // When user not loggin function get_filter(){ global $wpdb; $user_id = $_POST['user_id']; $product_id = $_POST['product_id']; $person_status = $_POST['person_status']; $filter_by = $_POST['filter_by']; $cancer_types = $_POST['cancer_types']; $years_diagnosed = $_POST['years_diagnosed']; $years_of_age = $_POST['years_of_age']; $years_begin = $_POST['years_begin']; if($filter_by == 'cancer-type'){ if($person_status == 'everybody'){ $total_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE product_id = '{$product_id}' AND cancer_types ='{$cancer_types}'") ); $good_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE product_id = '{$product_id}' AND cancer_types ='{$cancer_types}' AND rated_value = 'good'") ); $bad_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE product_id = '{$product_id}' AND cancer_types ='{$cancer_types}' AND rated_value = 'bad'") ); $indifferent_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE product_id = '{$product_id}' AND cancer_types ='{$cancer_types}' AND rated_value = 'indifferent'") ); }else{ $total_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}' AND product_id = '{$product_id}' AND cancer_types ='{$cancer_types}'") ); $good_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}' AND product_id = '{$product_id}' AND cancer_types ='{$cancer_types}' AND rated_value = 'good'") ); $bad_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}' AND product_id = '{$product_id}' AND cancer_types ='{$cancer_types}' AND rated_value = 'bad'") ); $indifferent_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) AS total FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}' AND product_id = '{$product_id}' AND cancer_types ='{$cancer_types}' AND rated_value = 'indifferent'") ); } if($good_count != 0){ $good = ($good_count / $total_count) * 100; }else{ $good = 0; } if($bad_count != 0){ $bad = ($bad_count / $total_count) * 100; }else{ $bad = 0; } if($indifferent_count != 0){ $indifference = ($indifferent_count / $total_count) * 100; }else{ $indifference = 0; } }else if($filter_by == 'years-diagnosed'){ //$get_years = "SELECT * FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}' AND product_id = '{$product_id}'"; //$total_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_survey_ratings_meta WHERE user_id IN ($list)")); if($person_status == 'everybody'){ $get_years_diagnosed = "SELECT * FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id"; }else{ $get_years_diagnosed = "SELECT * FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}'"; } $row_get_years_diagnosed = $wpdb->get_results($get_years_diagnosed); $get_id = array(); foreach($row_get_years_diagnosed as $row_get_years_diagnosed){ $num_years = date('Y')- $row_get_years_diagnosed->diagnosed_year; //date('md')<$row_get_years_diagnosed['diagnosed_month'].$row_get_years_diagnosed['diagnosed_day'] ? $age--:null; if((date("m") < $row_get_years_diagnosed->diagnosed_month) && (date("day") < $row_get_years_diagnosed->diagnosed_day)){ $num_years--; } if($years_diagnosed >= $num_years){ $get_id[] = $row_get_years_diagnosed->user_id; } } $val = implode(",", $get_id); $total_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.")")); $good_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.") AND ratings.rated_value = 'good' AND product_id = '{$product_id}'")); $bad_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.") AND ratings.rated_value = 'bad' AND product_id = '{$product_id}'")); $indifferent_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.") AND ratings.rated_value = 'indifferent' AND product_id = '{$product_id}'")); if(empty($total_count)){ $total_count = 0; } if($good_count != 0){ $good = ($good_count / $total_count) * 100; }else{ $good = 0; } if($bad_count != 0){ $bad = ($bad_count / $total_count) * 100; }else{ $bad = 0; } if($indifferent_count != 0){ $indifference = ($indifferent_count / $total_count) * 100; }else{ $indifference = 0; } }else if($filter_by == 'years-of-age'){ //$get_years = "SELECT * FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}' AND product_id = '{$product_id}'"; //$total_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_survey_ratings_meta WHERE user_id IN ($list)")); if($person_status == 'everybody'){ $get_years = "SELECT * FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id"; }else{ $get_years = "SELECT * FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE describes_you = '{$person_status}'"; } $row_get_years = $wpdb->get_results($get_years); $get_id = array(); foreach($row_get_years as $row_get_years){ $age = date('Y')- $row_get_years->born_year; //date('md')<$row_get_years_diagnosed['diagnosed_month'].$row_get_years_diagnosed['diagnosed_day'] ? $age--:null; if((date("m") < $row_get_years->born_month) && (date("day") < $row_get_years->born_day)){ $age--; } if($years_of_age == "plus"){ if($age > 100){ $get_id[] = $row_get_years->user_id; } }else{ if(($age >= $years_begin) && ($age <= $years_of_age)){ $get_id[] = $row_get_years->user_id; } } } $val = implode(",", $get_id); $total_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.")")); $good_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.") AND ratings.rated_value = 'good' AND product_id = '{$product_id}'")); $bad_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.") AND ratings.rated_value = 'bad' AND product_id = '{$product_id}'")); $indifferent_count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_filtering AS filtering LEFT JOIN wp_survey_ratings_meta AS ratings ON filtering.user_id=ratings.user_id WHERE filtering.user_id IN (".$val.") AND ratings.rated_value = 'indifferent' AND product_id = '{$product_id}'")); if(empty($total_count)){ $total_count = 0; } if($good_count != 0){ $good = ($good_count / $total_count) * 100; }else{ $good = 0; } if($bad_count != 0){ $bad = ($bad_count / $total_count) * 100; }else{ $bad = 0; } if($indifferent_count != 0){ $indifference = ($indifferent_count / $total_count) * 100; }else{ $indifference = 0; } } $good = number_format($good, 2, '.', ''); $bad = number_format($bad, 2, '.', ''); $indifference = number_format($indifference, 2, '.', ''); echo json_encode(array('total_count' => $total_count, 'good' => $good, 'bad' => $bad, 'indifference' => $indifference)); die(); } function user_has_filtering($user_id){ global $wpdb; $sql = "SELECT * FROM wp_users_filtering WHERE user_id = '{$user_id}'"; $row = $wpdb->get_row($sql); if(empty($row)){ return false; }else{ return true; } } /***************************** ADD FILTERING ACTION ********************************/ add_action('wp_ajax_update_filtering', 'update_filtering'); // When user login add_action('wp_ajax_nopriv_update_filtering', 'update_filtering'); // When user not loggin
I am stocked with this for a couple of days and i need this to work. Thank you.
- The topic ‘Ajax submit returns undefined value no data submitted’ is closed to new replies.