Saya telah membuat formulir pertanyaan di situs web e-niaga saya untuk menambahkan produk ke keranjang.

Berikut adalah screenshotnya:

masukkan deskripsi gambar di sinimasukkan deskripsi gambar di sini

Kode Fungsi:

public function addToCartOrderPackage($data, $proId){

    // Note: $plan_days and $days_names are that 2 div's which i am hiding and showing and wanna validate only on if these are showing 

    $pro_id = mysqli_real_escape_string($this->db->link, $proId);
    $plan_name = mysqli_real_escape_string($this->db->link, $data['plan_name']);
    $plan_days = mysqli_real_escape_string($this->db->link, $data['plan_days']);
    $days_names = mysqli_real_escape_string($this->db->link, $data['days_names']);
    $calOrderPrice = mysqli_real_escape_string($this->db->link, $data['calculated_order_price']);
    $food_time = mysqli_real_escape_string($this->db->link, $data['food_time']);
    $starts_from = mysqli_real_escape_string($this->db->link, $data['starts_from']);

    $query = "SELECT * FROM products WHERE pro_id = '$pro_id' AND pro_type = 'Plan'";
    $result = $this->db->select($query);
    $value = $result->fetch_assoc();
    $pro_name = mysqli_real_escape_string($this->db->link, $value['pro_name']);
    $pro_desc = mysqli_real_escape_string($this->db->link, $value['pro_desc']);

    $withLogin = $this->withLoginAddToCart($pro_id, $pro_name, $calOrderPrice, $pro_desc, $plan_name, $plan_days, $days_names, $food_time, $starts_from);
    return $withLogin;
}

public function withLoginAddToCart($pro_id, $pro_name, $calOrderPrice, $pro_desc, $plan_name, $plan_days, $days_names, $food_time, $starts_from){
    $userid = Session::get("user_id");
    $user_id = mysqli_real_escape_string($this->db->link, $userid);
    $arraynames = explode(",", $days_names);
    $countdays = count($arraynames);

    // Note: $plan_days and $days_names are that 2 div's which i am hiding and showing and wanna validate only on if these are showing 

    if($plan_name == "" || $plan_days == "" || $days_names == "" || $calOrderPrice == "" || $food_time == ""){
        $msg = "<div class='alert alert-danger'>All fields are required. Please fill all fields.</div>";
        return $msg;
    } else {
        if($plan_days == '5' && $countdays != 5){
            $msg = "<div class='alert alert-danger'>Please select 5 days names.</div>";
            return $msg;
        } elseif($plan_days == '6' && $countdays != 6){
            $msg = "<div class='alert alert-danger'>Please select 6 days names.</div>";
            return $msg;
        } elseif($plan_days == '7' && $countdays != 7){
            $msg = "<div class='alert alert-danger'>Please select 7 days names.</div>";
            return $msg;
        } else {
            $queryO = "INSERT INTO plan_cart(user_id, pro_id, pro_name, pro_desc, pro_price, plan_name, plan_days, days_names, food_time, starts_from, pro_type) "
                    . "VALUES('$user_id', '$pro_id', '$pro_name', '$pro_desc', '$calOrderPrice', '$plan_name', '$plan_days', '$days_names', '$food_time', '$starts_from', 'Plan')";
            $resultO = $this->db->insert($queryO);
            if($resultO != false){
                header("location: plan-cart.php");
            } else {
                $msg = "<div class='alert alert-danger'>Something's went wrong, Please try again.</div>";
                return $msg;
            }
        }
    }
}
-2
Zain Shabir 20 September 2019, 09:25

1 menjawab

Jawaban Terbaik

Ini sebenarnya bukan pertanyaan jquery dan jawabannya bisa sangat sederhana menurut saya. Anda tahu bahwa div bersembunyi ketika pengguna telah memilih pengiriman harian, jadi di kode server Anda, validasi kami dapat dilakukan

if($plan_name == "" || (($plan_days == "" || $days_names == "")&&$plan_name!='Daily') || $calOrderPrice == "" || $food_time == "")

Atau sesuatu di sepanjang garis ini.

1
Hans Dash 20 September 2019, 07:01