How to validate if certain value is duplicate or if certain input field is not filled using PHP AJAX Javascript

o7jaxewo  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(120)

This is a simple REST API for listing, creating and deleting products in/from database. I am having issues with validating/checking if certain values duplicate or if certain input field is not filled. I've attempted doing that with rowCount for the duplicate, in the php file, however I think there must be a better way for this, however I am having issues finding out how to do that.
here is the post.php

$query_check = 'SELECT * FROM skandi WHERE sku = :sku'; 
$stmt_check = $this->conn->prepare($query_check);
$stmt_check->bindParam(':sku', $this->sku);
$stmt_check->execute();

if ($stmt_check->rowCount() > 0) {
    echo 'product name already exists!';
} else {
$query = 'INSERT INTO ' . $this->table . '
        SET             

        sku = :sku,
        name = :name,
        price = :price,
        productType = :productType,
        size = :size,
        weight = :weight,
        height = :height,
        length = :length,
        width = :width';

        $stmt = $this->conn->prepare($query);

        $this->sku;
        $this->name; 
        $this->price; 
        $this->productType; 
        $this->size;
        $this->weight;
        $this->height;
        $this->length;
        $this->width;

        $stmt->bindParam(':sku', $this->sku);
        $stmt->bindParam(':name', $this->name);
        $stmt->bindParam(':price', $this->price);
        $stmt->bindParam(':productType', $this->productType);
        $stmt->bindParam(':size', $this->size);
        $stmt->bindParam(':weight', $this->weight);
        $stmt->bindParam(':height', $this->height);
        $stmt->bindParam(':length', $this->length);
        $stmt->bindParam(':width', $this->width);

        if($stmt->execute()) {
            return true;

        } else {
            ini_set('display_errors',1);

            return false;
        }

    }

this is the ajaxcall for posting

$(document).ready(function () {
$("#saveBtn").click(function (e) {
e.preventDefault();

//serialize form data
var url = $("form").serialize();

//function to turn url to an object
function getUrlVars(url) {
  var hash;
  var myJson = {};
  var hashes = url.slice(url.indexOf("?") + 1).split("&");
  for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split("=");
    myJson[hash[0]] = hash[1];
  }
  return JSON.stringify(myJson);
}

//pass serialized data to function
var test = getUrlVars(url);

//post with ajax
$.ajax({
  type: "POST",
  url: "/api/post/create.php",
  data: test,
  ContentType: "application/json",

  success: function () {
    alert("successfully posted");
  },
  error: function () {

    console.log("Could not be posted");
  },
});

}); });

368yc8dk

368yc8dk1#

You can check if certain input fields are not filled using the isset() function in PHP: For example:

if (isset($_POST['sku']) {
      //exists
   }

While to check if the product already exists you have to check if it exists in database so you must necessarily make a query.

相关问题