I've created a question form in my e-commerce website to add product to cart.
Here are the screenshots:
You can see these two screenshots above. If the user clicks on the Daily
radio button then two div
's are hidden, because on daily
option the user don't need to give an answer to those two questions. But if the user clicks on Weekly or Monthly
radio button, then those two div
's are appearing. I am hiding and showing these two div's with jQuery hide()
and show()
. So, I am validating it all but I wanna validate this properly that if user click on Daily
button so that the two hidden div
s should not validate as required in back-end but if user click on Weekly or monthly
then the two div
s should validate as required. I am unable to get logic how this can be implemented.
Here is my code:
<form method="post">
<div class="select_package_validity">
<input type="radio" class="custom-control-input plan_name" name="plan_name_selector" value="Daily">
<input type="radio" class="custom-control-input plan_name" name="plan_name_selector" value="Weekly">
<input type="radio" class="custom-control-input plan_name" name="plan_name_selector" value="Monthly">
<input type="hidden" name="plan_name" class="selected_plan_name" />
</div>
<div class="select_days">
<input type="radio" class="custom-control-input plan_days" name="plan_days_selector" value="5">
<input type="radio" class="custom-control-input plan_days"name="plan_days_selector" value="6">
<input type="radio" class="custom-control-input plan_days" name="plan_days_selector" value="7">
<input type="hidden" name="plan_days" class="selected_plan_days" />
</div>
<div class="days_names">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Monday">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Tuesday">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Wednesday">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Thursday">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Friday">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Saturday">
<input type="checkbox" class="custom-control-input" name="day_name_selector" value="Sunday">
<input type="hidden" class="selected_days_names" name="days_names" />
</div>
<div class="food_time">
<input type="checkbox" class="custom-control-input" name="food_time_selector" value="Breakfast">
<input type="checkbox" class="custom-control-input" name="food_time_selector" value="Lunch">
<input type="checkbox" class="custom-control-input" name="food_time_selector" value="Dinner">
<input type="hidden" name="calculated_order_price" id="calculatedFinalPrice" />
<input type="hidden" class="selected_food_time" name="food_time" />
</div>
</form>
Function Code:
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;
}
}
}
}