Using web service as a unique field validation, where is it going wrong? | XM Community
Skip to main content

I'm trying to use web service as a validation for a field (unique).
Highlevel: User types in an ID, web server checks if that exists already in the db then returns true or false. If it exists, user cannot proceed. If it's new, the new ID is saved.
My survey flow is in this way;

  1. Ask for the user ID from a person (screening).

  2. Use web service (datapull.php) to send user ID and check if it exists. If it does, will return true and if not false. The data is stored in a SQL database and PHP, I've included code below.

  3. If ID is new, they complete it and then at the end of it. Web service block sends user ID to the datasave.php to save the info to the SQL server.

I've attached my blocks and code, it doesn't seem to be doing either data saving or data pull properly. I've been trying different ways but couldn't figure out. Any help would be highly appreciated.
Screen Shot 2022-09-15 at 11.34.35 PM.pngScreen Shot 2022-09-15 at 11.35.10 PM.pngdatasave.php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$database = "yyyyyy";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}
$mturk_id = $_POSTO'MTurkID'];
$batch_id = $_POSTO'BatchID'];
$complete = $_POSTO'Flag'];
$politicallean = $_POSTO'Political'];

// echo "Connected successfully";
 
$sql = "INSERT INTO pldl_data (mturk_id,batch_id,political,completed) VALUES ({$mturk_id}, {$batch_id}, {$politicallean},{$complete})";
mysqli_query($conn, $sql);

mysqli_close($conn);
?>
datapull.php

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}
$mturk_id = $_POST 'MTurkID'];
$batch_id = $_POST 'BatchID'];
$complete=1;

$sql = "SELECT * FROM pldl_data";
$query_result = FALSE;

if($result = mysqli_query($conn, $sql)){
   if(mysqli_num_rows($result) > 0){
     while($row = mysqli_fetch_array($result)){
      if($rowr'mturk_id'] == $mturk_id){
         if($row{'batch_id'] == $batch_id and $rowt'completed']==$complete) $query_result=TRUE;
      }
     }
     mysqli_free_result($result);
   }
 } else{
   echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
 }

mysqli_close($conn);

$data = array("duplicate_flag_ext" => $query_result);

header("Content-Type: application/json");
echo json_encode($data);
exit();
?>

Not sure where it is going wrong, but some feedback:

  • You should test your php scripts to make sure they are operating correctly. I prefer to test outside Qualtrics. If you test within Qualtrics, use actual values instead of piped values when you test.

  • datapull.php doesn't need to get all records. It should query for the specific id and batch. Then just check if the number of returned rows is zero.

  • datapull should return "1" or "0" in $query_result instead of a boolean. That makes the embedded data field easy to check in Qualtrics.

  • In the webservice call, you shouldn't send duplicate query and body parameters. Since you are using $_POST just send body parameters.


Leave a Reply