Skip to content
Advertisement

Ajax request to php with empty POST data

I am new to javascript and I have problem with an ajax POST to php.

I am trying tosend javascript variables to php via an ajax POST but it doesn’t work.

The ajax post is sent but it sends empty POST. I got alert “Error occured ! 202” when trying to save data to bdd.

Thanks in advance for helping me.

here are my javascript and php codes :

<script type="text/javascript">
function save_marker(mId, vId)
    {
        //Save new marker using jQuery Ajax
        var myData = { 'markerId' : mId, 'vendorId' : vId }; //post variables   
        console.log(myData);
        jQuery.ajax({
          type: "POST",
          url: "https://marchad.fr/wp-content/plugins/wc-multivendor-marketplace/includes/map_process.php",
          data: myData,
          cache: false,
          dataType: "json",
          contentType : "application/json; charset=utf-8",
          success: function(dataResult){
                    var stringified = JSON.stringify(dataResult);
                    var dataResult = JSON.parse(stringified);
                    if(dataResult.statusCode==200){
                        alert("ok"+dataResult);                 
                    }
                    else if(dataResult.statusCode==201){
                       alert("Error occured !");
                    } else if(dataResult.statusCode==202){
                       alert("Error occured ! 202");
                    }  else if(dataResult.statusCode==203){
                       alert("Error occured ! 203");
                    }
                    
          },
          error:function (xhr, ajaxOptions, thrownError){
              alert('Ooops, something happened: ' + ajaxOptions + ' ' +thrownError);
          }
        });
    }
</script>

<?php
global $WCFM, $WCFMmp, $wpdb;
$mId    = filter_var($_POST["markerId"], FILTER_SANITIZE_STRING);
$vId    = filter_var($_POST["vendorId"], FILTER_SANITIZE_STRING);

################ Save & delete markers #################
if( isset($mId) && isset($vId) ) //run only if there's a post data
{
    if( !empty($mId) && !empty($vId) ){
        //make sure request is comming from Ajax
        $xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; 
        if (!$xhr){ 
            header('HTTP/1.1 500 Error: Request must come from Ajax!'); 
            exit(); 
        }



        $results = $wpdb->query("INSERT INTO markers_users (id, mark_id, user_id, user_actif) VALUES ('', $mId , $vId , 'oui')");
        if (!$results) {  
              echo json_encode(array("statusCode"=>201));
        } else {
            echo json_encode(array("statusCode"=>200));
        }
    } else {
        echo json_encode(array("statusCode"=>202));
    }

} else {
        echo json_encode(array("statusCode"=>203));
}
?>

Advertisement

Answer

ok so the solution is :

<script type="text/javascript">
function save_marker(mId, vId)
    {
        //Save new marker using jQuery Ajax
        var myData = { markerId : mId, vendorId : vId }; //post variables   
        console.log(myData);
        jQuery.ajax({
          type: "POST",
          url: "https://marchad.fr/wp-content/plugins/wc-multivendor-marketplace/includes/map_process.php?markerId="+ mId +"&vendorId="+ vId,
          data: myData,
          cache: false,
          dataType: "json",
          contentType : "application/json; charset=utf-8",
          success: function(dataResult){
                    var stringified = JSON.stringify(dataResult);
                    var dataResult = JSON.parse(stringified);
                    if(dataResult.statusCode==200){
                        alert("ok"+dataResult);                 
                    }
                    else if(dataResult.statusCode==201){
                       alert("Error occured !"+dataResult.statusCode+dataResult.mId+dataResult.vId+dataResult.rawData+dataResult.rawDataMid+dataResult.rawDataVid+dataResult.datamId+dataResult.datavId);
                    } else if(dataResult.statusCode==202){
                       alert("Error occured ! 202"+dataResult.statusCode+dataResult.mId+dataResult.vId+dataResult.rawData+dataResult.rawDataMid+dataResult.rawDataVid);
                    }  else if(dataResult.statusCode==203){
                       alert("Error occured ! 203"+dataResult.statusCode+dataResult.mId+dataResult.vId+dataResult.rawData+dataResult.rawDataMid+dataResult.rawDataVid);
                    }
                    
          },
          error:function (xhr, ajaxOptions, thrownError){
              alert('Ooops, something happened: ' + ajaxOptions + ' ' +thrownError);
          }
        });
    }
</script>

<?php
//mysqli
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);

if (mysqli_connect_errno()) 
{
     die('Erreur de connexion : ' . $mysqli->connect_errno);
}

$rawData = file_get_contents("php://input");

$data = array();
parse_str($rawData, $data);

$mId    = $data['markerId'];
$vId    = $data['vendorId'];


################ Save & delete markers #################
if( isset($mId) && isset($vId) ) //run only if there's a post data
{

    if( !empty($mId) && !empty($vId) ) //run only if there's a post data
    {

        $results = $mysqli->query("INSERT INTO markers_users (id, mark_id, user_id, user_actif) VALUES ('', $mId , $vId , 'oui')");
        if (!$results) { 
            echo json_encode(array("statusCode"=>201, "mId"=>$mId, "vId"=>$vId, 'rawData' => $rawData, 'rawDataMid' => $rawData["markerId"], 'rawDataVid' => $rawData["vendorId"], "datamId"=> $data['markerId'], "datavId"=> $data['vendorId']));
        } else {
            echo json_encode(array("statusCode"=>200));
        }
    } else {
        echo json_encode(array("statusCode"=>202));
    }

} else {
        echo json_encode(array("statusCode"=>203));
}

?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement