1

I have created a plugin to insert and fetch records. When I save record, record is getting saved from localhost but when I try it on online server it is not getting saved. Here is what I am trying to do.

function data_custom_ajax(){      
        global $wpdb;
        $tbl = "tripplan";
        $table_name=$wpdb->prefix . $tbl;
        $custom_val = $_POST['text'];
        $totaltime = $_COOKIE['totalduration'];
        $totaldistance = $_COOKIE['totaldistance'];
        $origin_address = $custom_val['originaddress'];
        $end_address = $custom_val['destinationaddress'];
        $waypoints = $custom_val['waypts'];
          $wpdb->insert($table_name,
                          array(
                            'startpoint' => $origin_address,
                            'endpoint' => $end_address,
                            'waypoints' => json_encode($waypoints),
                            'totaldistance' => $totaldistance,
                            'totalduration' => $totaltime
                            ),
                            array('%s','%s','%s','%f','%f')
                          );
          echo "data has been saved";

    }
3
  • when you are calling this function? Commented Sep 6, 2017 at 12:35
  • I have added it for logined user as well not logined user add_action('wp_ajax_data_custom_ajax', 'data_custom_ajax'); add_action('wp_ajax_nopriv_data_custom_ajax', 'data_custom_ajax'); Commented Sep 6, 2017 at 12:39
  • is it working for you? Commented Sep 6, 2017 at 12:47

2 Answers 2

1
function data_custom_ajax(){      
    global $wpdb;
    $tbl = "tripplan";
    $table_name=$wpdb->prefix . $tbl;
    $custom_val = $_POST['text'];
    $totaltime = $_COOKIE['totalduration'];
    $totaldistance = $_COOKIE['totaldistance'];
    $origin_address = $custom_val['originaddress'];
    $end_address = $custom_val['destinationaddress'];
    $waypoints = $custom_val['waypts'];

    $data = array(
        'startpoint' => $origin_address,
        'endpoint' => $end_address,
        'waypoints' => json_encode($waypoints),
        'totaldistance' => $totaldistance,
        'totalduration' => $totaltime
        )

    $lastInsertedId = $wpdb->insert($table_name,$data);


     if($lastInsertedId != '')
     {
        echo "data has been saved";
     }else{
         $wpdb->print_error();
     }
      die();

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

5 Comments

record is not getting saved and it doesn't show error. after doing above no response is coming. It doesn't echo.
check with updated code. and be cool. every issue has solution.
still it is not getting saved.
write echo $wpdb->print_error(); and let me know what response is coming.
it doesn't echo anything.
0

After lot of reading and debugging I got to know the problem. My cookie was not getting saved correctly. Even though it was working on localhost, it was not working on website. After reading this [PHP cannot read javascript cookies and then after I modified my cookie while saving. I was setting cookie like this and it was not working-

document.cookie="cookiename="+value

after setting this way it worked -

document.cookie = 'cookiename='+value+'; path=/'

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.