How to get/pass the value to modal - php

How can I pass the value of order_id from order.php to ordermodal.php, So I can use it in my query.
This is the order.php
UI of order.php
<?php
session_start();
$order_id = $_SESSION['order_id'];
if (!isset($_SESSION['user_id']))
{
header("Location: index.php");
}
$_SESSION['navMenu'] = "order";
ini_set('display_errors', 'On');
require_once 'includes/database.php';
include_once 'system_menu.php';
include_once 'ordermodal.php';
$sql2 = "SELECT * FROM cart_tbl";
$sql = "SELECT * FROM order_tbl WHERE order_status = 'Accepted' or order_status = 'Dispatched' or order_status = 'Pending' ORDER BY order_datetime DESC";
/*** * SET UP COMBO BOX FOR SEARCH */
$comboBox = isset($_REQUEST['comboBoxVal']) ? trim($_REQUEST['comboBoxVal']) : '';
$search_by = isset($_REQUEST['search_by']) ? addslashes($_REQUEST['search_by']) : 0;
$users = null;
if ($comboBox != '') { switch ($search_by) {
case 1://Order ID
$sql .= " WHERE order_id LIKE '%{$comboBox}%' ";
break;
}
}
$carts = mysqli_query(connection(), $sql2);
$orders = mysqli_query(connection(), $sql);
$search_filters = array(1 => 'Order ID');
/*** * END SET UP COMBO BOX */ ?>
<html>
<head>
<title>ORDERS</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
body {
background:url(images/jerica.jpg)fixed no-repeat center;
background-size:cover;
}
.asd {
color: white;
}
</style>
</head>
<body>
<?php
if (isset($_SESSION['message'])) {
$message = $_SESSION['message']; unset($_SESSION['message']);
}
else {
$message = "";
}
?> <div class="container" >
<h1 class="asd">Orders</h1>
</div> <div class="container" >
<div class="panel panel-default" >
<div class="panel-heading"> <form method="post"> <div class="input-group">
<div class="input-group-addon"> <select name="search_by">
<?php
foreach ($search_filters as $key => $value): ?>
<option value="<?= $key ?>" <?= $key == $search_by ? 'selected' : '' ?> > <?= $value ?> </option>
<?php
endforeach;
?>
</select>
</div>
<input type="search" name="comboBoxVal" value="<?= $comboBox ?>" class="form-control" placeholder="Search">
</div>
</form>
</div>
<div class="panel-body">
<?php
if ($message != ''): ?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"> ×</span></button>
<?= $message ?> </div>
<?php endif; ?>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Order ID</th>
<th>User ID</th>
<th>Order Date</th>
<th>Order Time</th>
<th>Delivery Charge</th>
<th>Total Amount</th>
<th>Address</th>
<th>Coordinates</th>
<th>Driver Number</th>
<th>Order Status</th>
<th>Action</th>
</tr>
</thead>
<?php
/*** * DISPLAY TABLE */
?>
<tbody>
<?php if ($orders): ?>
<?php
while ($order = mysqli_fetch_array($orders, MYSQLI_BOTH)):
?>
<?php
$order_datetime = $order['order_datetime'];
$date = date('Y-m-d', strtotime($order_datetime));
$time = date('H:i:s', strtotime($order_datetime));
?>
<tr>
<td><?= $order['order_id'] ?></td>
<td><?= $order['user_id'] ?></td>
<td><?= $date ?></td>
<td><?= $time ?></td>
<td><?= $order['order_deliveryCharge'] ?></td>
<td><?= $order['order_totalAmount'] ?></td>
<td><?= $order['address'] ?></td>
<td><?= $order['coordinates'] ?></td>
<td><?= $order['driver_number'] ?></td>
<td><?= $order['order_status'] ?></td>
<td><button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal" onclick="viewOrder( '<?= $order['order_id'] ?>', '<?= $order['order_id'] ?>', '<?= $date ?>', '<?= $time ?>', '<?= $order['order_deliveryCharge'] ?>', '<?= $order['order_totalAmount'] ?>', '<?= $order['address'] ?>', '<?= $order['coordinates'] ?>', '<?= $order['driver_number'] ?>', '<?= $order['order_status'] ?>')"> View Order </button>
</td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="panel-footer">
</div>
</div>
</div>
<script>
function viewOrder(order_id, user_id, order_date, order_time, order_deliveryCharge, order_totalAmount, address, coordinates, driver_number, order_status) {
document.getElementById("titleModal").innerHTML = "viewOrder";
document.getElementsByName("order_id")[0].setAttribute("value", order_id);
document.getElementsByName("user_id")[0].setAttribute("value", user_id);
document.getElementsByName("order_date")[0].setAttribute("value", order_date);
document.getElementsByName("order_time")[0].setAttribute("value", order_time);
document.getElementsByName("order_deliveryCharge")[0].setAttribute("value", order_deliveryCharge);
document.getElementsByName("order_totalAmount")[0].setAttribute("value", order_totalAmount);
document.getElementsByName("address")[0].setAttribute("value", address);
document.getElementsByName("coordinates")[0].setAttribute("value", coordinates);
document.getElementsByName("driver_number")[0].setAttribute("value", driver_number);
document.getElementsByName("order_status")[0].setAttribute("value", order_status);
document.getElementsByName("viewOrder")[0].setAttribute("name", "viewOrder");
/*x = document.getElementsByName("order_status").value;
if(x == "Accepted"){
document.getElementsByName("submitAccept").disabled = true;
}*/
}
</script?
</body>
</html>
And this is the ordermodal.php
UI of ordermodal.php
<?php
/** *ordermodal.php **/
$id = "";
$order_date = "";
$order_time = "";
$order_id = "";
$order_deliverCharge = "";
$order_status = "";
$order_totalAmount= "";
$coordinates = "";
$driver_number = "";
$address = "";
$food_name="";
$special_request="";
$quantity="";
$amount="";
$orders="";
?>
<!-- MODALS --> <!-- DETAILS -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form action="" method="post" class="form-horizontal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><center>×</button>
<h4 class="modal-title" id="titleModal"></h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="order_id" class="col-sm-2 control-label">Order ID</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="order_id" id="order_id" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="id" class="col-sm-2 control-label">ID</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="user_id" id="user_id" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="order_date" class="col-sm-2 control-label">Order Date</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="order_date" id="order_date" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="order_time" class="col-sm-2 control-label">Order Time</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="order_time" id="order_time" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="order_deliverCharge" class="col-sm-2 control-label">Delivery Charge</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="order_deliveryCharge" id="order_deliveryCharge" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="order_totalAmount" class="col-sm-2 control-label">Total Amount</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="order_totalAmount" id="order_totalAmount" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="address" class="col-sm-2 control-label">Address</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="address" id="address" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="coordinates" class="col-sm-2 control-label">Coordinates</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="coordinates" id="coordinates" placeholder="" value="" required="required" maxlength="11" readonly>
</div>
</div>
<div class="form-group">
<label for="driver_number" class="col-sm-2 control-label">Driver Number</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="driver_number" id="driver_number" placeholder="" value="" required="required" readonly>
</div>
</div>
<div class="form-group">
<label for="order_status" class="col-sm-2 control-label">Order Status</label>
<div class="col-lg-3">
<input type="text" input style="width:500px" class="form-control" name="order_status" id="order_status" placeholder="" value="" required="required" readonly>
</div>
</div>
<?php
// This is where I want to get the value of oder_id from order.php page
$sql = "SELECT food_name, special_request, quantity, amount
FROM cart_tbl
WHERE order_id=$order_id";
$result = mysqli_query(connection(), $sql);
?>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Food</th>
<th>Special Request</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<?php
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["food_name"];?></td>
<td><?php echo $row["special_request"];?></td>
<td><?php echo $row["quantity"];?></td>
<td><?php echo $row["amount"];?></td>
</tr>
<?php
}
}
?>
</table>
<tbody>
</div>
<div class="modal-footer">
<button type="submit" name="showOrder" id="showOrder" class="btn btn-secondary" onclick="" > Show Order </button>
<button type="submit" name="submitAccept" id="submitAccept" class="btn btn-primary" onclick="if(!confirm('Are you sure you want to accept order?')){return false;}" > Accept </button>
<button type="submit" name="submitSent" class="btn btn-primary"onclick="if(!confirm('Are you sure you want to send order?')){return false;}" >Sent</button>
<button type="submit" name="submitCancel" class="btn btn-danger" onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel</button>
<?php
if(isset($_POST['showOrder'])){
$order_id = trim(addslashes($_POST['order_id']));
}
if(isset($_POST['submitAccept'])){
$order_id = trim(addslashes($_POST['order_id']));
$query = "UPDATE order_tbl SET `order_status`='Accepted' WHERE `order_id` = $order_id";
if (mysqli_query(connection(), $query)) {
mysqli_query(connection(), "COMMIT");
$_SESSION['message'] = "Order Accepted"; }
else {
$_SESSION['message'] = mysqli_error(connection());
mysqli_query(connection(), "ROLLBACK");
}
}
if(isset($_POST['submitSent'])){
$order_id = trim(addslashes($_POST['order_id']));
$query = "UPDATE order_tbl SET `order_status`='Dispatched' WHERE `order_id` = $order_id";
if (mysqli_query(connection(), $query)) {
mysqli_query(connection(), "COMMIT");
$_SESSION['message'] = "Order Dispatched"; }
else {
$_SESSION['message'] = mysqli_error(connection());
mysqli_query(connection(), "ROLLBACK");
}
}
if(isset($_POST['submitCancel'])){
$order_id = trim(addslashes($_POST['order_id']));
$query = "UPDATE order_tbl SET `order_status`='Cancelled' WHERE `order_id` = $order_id";
if (mysqli_query(connection(), $query)) {
mysqli_query(connection(), "COMMIT");
$_SESSION['message'] = "Order Cancelled"; }
else {
$_SESSION['message'] = mysqli_error(connection());
mysqli_query(connection(), "ROLLBACK");
}
}
?>
</div>
</form>
</div>
</div>
</div>
So the function of this as the moment is when clicked the view order the value of the order_id from order.php didnt get, you need first to click the button inside the ordermodal.php(accept,send,cancel) in order to get the value of order_id.
"When I click the view order, I'll get right away the value of order_id so that I can use it in my sql query. This is what supposed to be the real output."
Hope you guys can help me, I stacked at this error for a couple of days already. TIA!

Couldn't you still use the session var you did in the first chunk...unless you are reseting that value - but didn't see that...
Alternatively... the url you are calling to get the url for the modal you could put a query param of OID=# and than do the typical...
$orderID = $_GET['old'] ? $_GET['old'] : $_POST['old']; // not sure if your posting or just doing a get....
Also no sure if your pulling this in through an iframe or what not but either way query param or session var should work.
Hope this helps.

Related

How to display a table using AJAX in modal?

officer_cashier.php
This is my modal form I want to display a table upon clicking the button add from cashier_template.php in DIV tag id=add_table
<div class="modal fade" id="fee_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Process payment</h5>
<button type="button" class="close get_close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" action="officer_cashier.php" id="reg">
<fieldset class="scheduler-border">
<legend class="scheduler-border">Student information</legend>
<input type="hidden" class="form-control" id="id" name="id">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" readonly="readonly">
</div>
<div class="form-group">
<label for="course">Course</label>
<input type="text" class="form-control" id="course" name="course" readonly="readonly">
</div>
<div class="form-group">
<label for="sem">Semester</label>
<input type="text" class="form-control" id="sem" name="sem" readonly="readonly">
</div>
<div class="form-group">
<label for="year">Year</label>
<input type="text" class="form-control" id="year" name="year" readonly="readonly">
</div>
</fieldset>
<button class="btn btn-sucess add_fees" id="add_fees">add</button >
<div class="form-group" id="display_table"></div><!-- I want to display the table inside of this DIV tag -->
</form>
</div>
</div>
</div>
</div>
script
This is my AJAX the course,sem,year data is what i need to display the table, so if those three fetch successfully I want to display it in my DIV tag #display_table
$(document).on('click', '.add_fees', function(){
$.ajax({
type: "post",
url: "../templates/cashier_template.php",
data: {
"course": $("#course").val(),
"semester": $("#sem").val(),
"year": $("#year").val(),
},
success: function(data) {
$("#display_table").html(data);
}
});
});
cashier_template.php
This is the cashier template once the AJAX pass the data and matcher the query it should display in modal but I wasnt getting
<?php
ob_start();
include("../include/userlogin.php");
if(!isset($_SESSION))
{
session_start();
}
if($_SESSION['usertype'] != 1){
header("location: login.php?success=1");
$_SESSION['message'] = "You cannot access this page unless you are a officer!";
}
ob_end_flush();
$yearId = $_POST['year'];
$courseId = $_POST['course'];
$semesterId = $_POST['semester'];
$result = $connect->query("SELECT id, total_fees, fee_names FROM manage_fees WHERE year_lvl = '$yearId' AND course = '$courseId' AND semester = '$semesterId'") or die($connect->error());
while($row = $result->fetch_assoc()):
?>
<div class="table-sorting table-responsive" style="margin-top: 1rem;">
<table class="table table-striped table-bordered table-hover" id="table1">
<thead>
<tr class="p-4">
<th scope="col">Select</th>
<th scope="col">School fees</th>
<th scope="col">Amount</th>
<th scope="col">type</th>
</tr>
</thead>
<tbody>
<?php
$result = $connect->query("SELECT * FROM fees;") or die($connect->error());
while($row = $result->fetch_assoc()){
?>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input check_amount" name="local_fees">
<label class="custom-control-label" for="check_amount"></label>
</div>
</td>
<td name="selected_fees"><?php echo $row['fee_name']; ?></td>
<td name="amount"><?php echo $row['amount']; ?></td>
<td><?php echo $row['type']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php endwhile; ?>
<script src="../js/datable.js"></script>
You are overwriting the $result object (and subsequently the $row obect) when you query fees. So the big loop at the top:
while($row = $result->fetch_assoc()):
is getting overwritten down the line by
$result = $connect->query("SELECT * FROM fees;");
while($row = $result->fetch_assoc()){
But really, there's no need to query FEES in every loop. Why not first get all the fees data into an array, then just access it in the other loop. So first, at the top of your php script:
<?php
$fees=array();
$result = $connect->query("SELECT * FROM fees;");
while($row = $result->fetch_assoc()){ $fees[]=$row; }
?>
Then in your main loop
<?php
foreach ($fees as $fee) {
?>
...
<td name="selected_fees"><?php echo $fee['fee_name']; ?></td>
<td name="amount"><?php echo $fee['amount']; ?></td>
<td><?php echo $fee['type']; ?></td>
</tr>
<?php } ?>

Updating a text field based on Drop-down menu choice using ajax

I'm trying to update a text field (Price) whenever a drop-down menu (List of Products) item is selected. the text filed will show the selected item's price. I want to do this using ajax and i'm still new to this.
My code so far:
index.php
<?php
include 'Connection.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="Style.css">
<title>Shopping Cart</title>
</head>
<body>
<div id="mainPadding">
<h1 class="Shopping Title">The Shopping Items</h1>
<form class="form-horizontal">
<fieldset class="border p-2">
<legend class="w-auto">Contact Details:</legend>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-3">
<input type="name" required class="form-control" id="name" placeholder="Enter Your Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-3">
<input type="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" class="form-control" required id="email" placeholder="e.g. shop#shop.shop">
</div>
</div>
</fieldset>
<fieldset class="border p-2">
<legend class="w-auto">Available Items:</legend>
<div class="form-group">
<label class="col-lg-3 control-label">Item:</label>
<div class="col-sm-3">
<div class="ui-select">
<select id="ItemListDropDown" class="selectpicker" data-live-search="true">
<div id="itemList">
<?php
$sql = "SELECT Product_Name FROM products WHERE Availability = 1";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
while ($row = mysqli_fetch_assoc($result))
echo "<option>" . $row["Product_Name"] . "</option>";
?>
</div>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="price">Price</label>
<div class="col-sm-3">
<input type="text" disabled class="form-control" id="priceTxt">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="price">Quantity (between 1 and 5):</label>
<div class="col-sm-3">
<input type="number" maxlength="1" min="1" max="5" class="form-control" id="priceTxt">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input id="addToCart" type="button" class="btn btn-primary" value="Add to the Cart">
</div>
</div>
</fieldset>
<fieldset class="border p-2">
<legend class="w-auto">Invoice Details:</legend>
<div class="form-group">
<table class="table table-striped" id="ItemTable">
<thead>
<tr>
<th scope="col">Sr.</th>
<th scope="col">Item Name</th>
<th scope="col">Price</th>
<th scope="col">Count</th>
<th scope="col">Total</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"></th>
<td id="ItemName"></td>
<td id="Price"></td>
<td id="Count"></td>
<td id="Total"></td>
<td id="DeleteButton"></td>
</tr>
</tbody>
</table>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input id="PrintAndSend" type="submit" class="btn btn-primary" value="Print and Send to Email">
</div>
</div>
</div>
</fieldset>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="Javascript.js"></script>
</body>
</html>
Javascrpt.js
$(document).ready(function() {
$("#ItemListDropDown").change(function() {
$("#itemList").load("load-product.php")
// $("#priceTxt").attr("value", price);
});
});
load-product.php
<?php
include 'Connection.php';
$sql = "SELECT Product_Name FROM products WHERE Availability = 1";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
while ($row = mysqli_fetch_assoc($result))
echo "<option>" . $row["Product_Name"] . "</option>";
?>
I'm trying to do a few things using ajax, solving this will set me up for the rest.
Snake, you don't need Ajax to get your product price, instead just use a data-* attribute:
in your php (NOT the load-product.php file):
<?php
$sql = "SELECT Product_id, Product_Name, Product_price FROM products WHERE Availability = 1";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
echo "<option value='default' data-price='default'>Choose a item </option>";
while ($row = mysqli_fetch_assoc($result))
echo "<option value='{$row["Product_id"}' data-price='{$row["Product_price"]}'>" . $row["Product_Name"] . "</option>";
?>
then in your javascript do this to get your item price:
// JavaScript using jQuery
$(function(){
$('#ItemListDropDown').change(function(){
var selected = $(this).find('option:selected');
var extra = selected.data('price');
$("#priceTxt").val(extra); // set your input price with jquery
$("#priceTxt").prop('disabled',false);//set the disabled to false maybe you want to edit the price
...
});
});
// Plain old JavaScript
var sel = document.getElementById('ItemListDropDown');
var selected = sel.options[sel.selectedIndex];
var extra = selected.getAttribute('data-price');
var priceInput = document.getElementById('priceTxt');
priceInput.disabled = false;
priceInput.value =extra;
Hope it helps =)

Using radio buttons to show approved or unapproved users in admin panel in php mysql

I have a project wherein I need to show unapproved and approved user from the database. I have to show it by radio buttons such as All, Approved and Unapproved. I want to know how to list each of them ,as the radio choice is clicked, on the same page. I am a fresher in PHP and its my first project and so highly confused.
I am trying to put in switch for radio and no idea about how to write MySQL query in each switch case. Is there any solution or code available? Is there any way to solve this?
Here is my code:
<?php include 'blocks/headerInc.php' ; ?>
<?php
$errmsg = "" ;
$module_id = '';
$query = '';
$date_from = '';
$date_to = '';
//Search section start here
$sqlQuery = "SELECT * FROM tbl_user WHERE type =3 " ;
if(isset($_REQUEST['submit']))
{
if(!empty($_REQUEST['date_from']))
{
$date_from = date("Y-m-d", strtotime($_REQUEST['date_from'])) ;
}
if(!empty($_REQUEST['date_to']))
{
$date_to = date("Y-m-d", strtotime($_REQUEST['date_to'])) ;
}
if(!empty($date_to) && empty($date_from))
{
$errmsg = "Please select valid date range.";
}
if(!empty($date_to) && (strtotime($date_from)> strtotime($date_to)))
{
$errmsg = "Please select valid date range.";
}
if($errmsg =='')
{
if(!empty($date_to) && (strtotime($date_from)<= strtotime($date_to)))
{
$sqlQuery .= " AND created_on BETWEEN '$date_from' AND '$date_to'";
}
$sqlQuery .= " order by id DESC";
}
$date_from = date("m/d/Y",strtotime($date_from));
$date_to = date("m/d/Y",strtotime($date_to));
$date_from = $date_from != '01/01/1970' ? $date_from : '';
$date_to = $date_to != '01/01/1970' ? $date_to : '';
}
?>
<div class="container pagecontainer">
<!-- Static navbar -->
<div class="row row-offcanvas row-offcanvas-right">
<!--/.col-xs-12.col-sm-9-->
<div class="col-sm-3 col-md-3 sidebar" id="sidebar">
<div id="left_panel" class="clearfix left">
<?php include 'blocks/leftnavInc.php' ; ?>
</div>
</div>
<div class="col-xs-12 col-sm-9 page-right">
<div class="panel panel-primary">
<div class="panel-heading">Search Registered Candidate</div>
<div class="panel-body">
<div class="column col-sm-offset-0">
<?php
if($errmsg!="")
{
echo "<div class='error'>".ucwords($errmsg)."</div>";
}
?>
<form class="form-horizontal" method="get" action="">
<div class="form-group">
<div class="col-md-6">
<div class="col-md-4">
<label for="username" class="control-label">Date From:</label>
</div>
<div class="col-md-8">
<div class="input-group date">
<input class="form-control datepicker" data-val="true" data-val-date="The field Dob must be a date." data-val-required="The Dob field is required." id="Dob" name="date_from" placeholder="Date From" type="text" value="<?php echo $date_from ; ?>" >
</div>
</div>
</div>
<div class="col-md-6">
<div class="col-md-4">
<label for="username" class="control-label">Date To:</label>
</div>
<div class="col-md-8">
<div class="input-group date">
<input class="form-control datepicker" data-val="true" data-val-date="The field Dob must be a date." data-val-required="The Dob field is required." id="Dob" name="date_to" placeholder="Date To" type="text" value="<?php echo $date_to ; ?>" >
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<div class="col-md-8 text-left">
<button type="submit" name="submit" value="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Search</button>
<button type="reset" onClick="javascript:window.location.href='reportRegisteredUsers.php'" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Cancel</button>
</div>
</div>
<div class="col-md-6">
<div class="col-md-4">
<label for="username" class="control-label"> </label>
</div>
<div class="col-md-8 text-right">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Report:Candidate Reports</div>
<div class="panel-body">
<input type="radio" name="gender" value="male" checked="checked"> All Candidates<br>
<input type="radio" name="gender" value="female"> Approved Candidates<br>
<input type="radio" name="gender" value="other"> Unapproved Candidates<br>
<div class="column col-sm-offset-0">
<table id="example" class="table table-striped table-hover table-bordered dataTableReport dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>S.No.</th>
<th>Email ID</th>
<th>SBI Employee ID</th>
<th>Name</th>
<th>Mobile No.</th>
<th>Date of Birth</th>
<th>Registration Date</th>
</tr>
</thead>
<tbody>
<?php
$sq = $db->query($sqlQuery);
$i = 1 ;
if($db->affected_rows > 0)
{
while($row=mysql_fetch_array($sq))
{
extract($row);
?>
<tr>
<td><?php echo $i ; ?></td>
<td><?php echo $email ; ?></td>
<td><?php echo $employee_id ; ?></td>
<td><?php echo $first_name." ".$middle_name." ".$last_name ; ?></td>
<td><?php echo $mobile ; ?></td>
<td><?php if($dob !='1970-01-01'){echo date("d-m-Y", strtotime($dob)) ; }?></td>
<td><?php echo date("d-m-Y", strtotime($created_on)) ; ?></td>
</tr>
<?php $i++;}} ?>
</tbody>
</table>
</div>
</div>
</div>
<div> <button type="reset" onClick="javascript:history.go(-1)" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Go Back</button> </div>
<!--/row-->
</div>
<!--/.sidebar-offcanvas-->
</div>
</div>
<?php include 'blocks/footerInc.php'
This would be a very very simple page for what you want to archive.
<?php
//connection with database here.
if(isset($_POST['users']) && $_POST['users'] == 'approved'){
$sql = "your query";
$result = mysqli_query($mysql, $sql);
while($row = result->fetch_assoc()){
$users[] = $row;
}
}elseif(isset($_POST['users']) && $_POST['users'] == 'unapproved'){
$sql = "your query";
$result = mysqli_query($mysql, $sql);
while($row = result->fetch_assoc()){
$users[] = $row;
}
}elseif(isset($_POST['users']) && $_POST['users'] == 'other'){
$sql = "your query";
$result = mysqli_query($mysql, $sql);
while($row = result->fetch_assoc()){
$users[] = $row;
}
}
?>
<div>
<form action="" method="POST">
Approved<input type="radio" name="users" value="approved">
Unapproved<input type="radio" name="users" value="unapproved">
Other<input type="radio" name="users" value="other">
<input type="submit" value="Submit">
</form>
</div>
<div class="results">
<?php foreach ($users as $key => $value) {
?>
<div class="user">
<div><?php echo $value['firstName']; ?></div>
<div><?php echo $value['lastName']; ?></div>
</div>
<?php
} ?>
</div>

update form with 2 parts

hello everyone I am just starting PHP this year and I try to make form that is content 2 part first part main data second part it content table that I can add new row to enter data and this row content multi-select input and I try many times to write PHP code that I can update this form but I failed every time I try
<div class="modal-body">
<div class="form-group">
<label class="control-label col-sm-2" for="shop_name">اسم المحل:
<a style="color: red">*</a>
</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="shop_name" value="<?php echo $strshop; ?>" name="shop_name" placeholder="اسم المحل" data-index="8">
</div>
<label class="control-label col-sm-2" for="cus_name">اسم العميل:
</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="cus_name" name="cus_name" value="<?php echo $strcus; ?>" placeholder="اسمع العميل" data-index="9">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="cus_address">العنوان:<a style="color: red">*</a></label>
<div class="col-sm-3">
<input type="text" class="form-control" id="cus_address" name="cus_address" value="<?php echo $straddres; ?>" placeholder="عنوان" data-index="10">
</div>
<label class="control-label col-sm-2" for="tel"> التليفون:</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="tel" name="tel" value="<?php echo $strtel; ?>" placeholder="تليفون" data-index="11">
</div>
</div>
</div>
<div class="modal-footer">
<h2>مياه</h2>
<div class="row clearfix">
<div class="col-md-12 column">
<?php
if (!empty($strid)) {
$q2 = "SELECT * FROM product_table WHERE form_id = $strid;";
$result = mysqli_query($con, $q2);
$count = mysqli_num_rows($result);
?>
<table class="table table-bordered table-hover" id="data_table">
<thead>
<tr>
<th class="text-center">
المنتج
</th>
<th class="text-center">
SKus
</th>
<th class="text-center">
الكمية
</th>
<th class="text-center">
ثلاجات
</th>
<th class="text-center">
مواد دعائية
</th>
<th class="text-center">
كمية الدعايه
</th>
<th>
action
</th>
</tr>
</thead>
<tbody>
<?php
if ($count > 0) {
while ($wrow = mysqli_fetch_array($result)) {
?>
<tr id='row'>
<td>
<input hidden name="wid" value="<?php echo $wrow['id'] ?>">
<select class="form-control" id="product_name" name="product_name[]" data-index="12">
<option selected disabled hidden>اختار المنتج .....</option>
<?php
$select_water = $con->query("select * from water");
if ($select_water->num_rows > 0) {
// output data of each row
while ($w = $select_water->fetch_assoc()) {
?>
<option <?php if ($wrow['product_name'] == $w['water_name']) echo "selected";?> ><?php echo $w['water_name']?></option>
<?php
}
} else {
echo "0 results";
}
?>
</select>
</td>
<td>
<select class="form-control" id="sk" name="sk[]"
data-index="6">
<option selected disabled hidden>اختار Skus</option>
<?php
$select_area = $con->query("select * from watersk");
if ($select_area->num_rows > 0) {
// output data of each row
while ($row2 = $select_area->fetch_assoc()) {
?>
<option <?php if ($wrow['sk'] == $row2['sk_name']) echo "selected"; ?> ><?php echo $row2 ['sk_name'] ?></option>
<?php
}
} else {
echo "0 results";
}
?>
</select>
</td>
<td>
<input type="number" id="qty" name="qty[]" value="<?php echo $wrow['qty']; ?>" placeholder='الكميه' class="form-control" data-index="16"/>
</td>
<td>
<input type="number" id="friz" name="friz[]" value="<?php echo $wrow['frizer']; ?>" placeholder='ثلاجه' class="form-control" data-index="17"/>
</td>
<td>
<?php
// var_dump($row['id']);
$water_id = $wrow['id'];
$test_array=array();
$select_water_market = $con->query("SELECT * FROM product_market WHERE water_id = $water_id;");
while ($row3 = $select_water_market->fetch_assoc()) {
$test_array[]= $row3['watermarket_media'];
}
// var_dump($test_array);
?>
<select multiple class="form-control" id="market_medi" name="market_media[0][]" data-index="6">
<?php
$select_media = $con->query("select * from market_media");
if ($select_media->num_rows > 0) {
// output data of each row
while ($row = $select_media->fetch_assoc()) {
?>
<?php if(in_array($row['media_name'],$test_array)){?>
<option selected>
<?php echo $row ['media_name'] ?>
</option>
<?php
}
else
{?>
<option>
<?php echo $row ['media_name'] ?>
</option>
<?php }
}
}else {
echo "0 results";
}
?>
</select>
</td>
<td>
<input style="float: right" type="number" id="market" name="market[]" placeholder="مواد دعائيه" class="form-control" value="<?php echo $wrow['mark_qty']; ?>" data-index="18"/>
</td>
<td>
<a class="btn btn-warning btn-sm" href='save/deleterow.php?id=".$delete['id']." data-toggle="modal" style="float: left;"><span class="glyphicon glyphicon-pencil"></span></a>
<a class="btn btn-danger btn-sm" href='save/deleterow.php?id=<?php echo $wrow['id'];?>&stid=<?php echo $strid;?>' data-toggle="modal" style="float: left;"><span class="glyphicon glyphicon-remove"></span></a>
</td>
</tr>
<?php
}
}
}
?>
</tbody>
</table>
<a class="btn btn-success btn-sm" href="" data-toggle="modal" onclick="addVariablesRow();" style="float: left;"><span class="glyphicon glyphicon-plus"></span></a>
</div>
<script>
function deleterow2(o) {
//no clue what to put here?
var p = o.parentNode.parentNode;
p.parentNode.removeChild(p);
}
var i = 1;
function addVariablesRow() {
var $t = $("#data_table");
var $row = $t.find("tbody tr").first();
var $r = $row.clone();
$r.attr('id', 'rrow' + i);
var $newRow = $r.find('input').val('').end().append("<td><a class='btn btn-danger btn-sm delete' onclick='deleterow2(this);' style='float: left;'><span class='glyphicon glyphicon-trash'></span></a></td>");
$newRow.appendTo($t).show();
$("#data_table tr:last td:first select").focus();
$('#market_medi').each(function () {
$(this).attr('name', 'market_media[' + i + '][]');
});
i++;
}
</script>
</div>
</div>

Update statement not working properly and changes the other records

I'm wondering why my update statement changes the venue_type of all the other records whenever I saved the edited record.
Been working on this update statement for quite sometime now and I just can't get it to work. It would be great if you guys could help me on this.
Thanks for the help! :)
<body>
<head>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/smoothscroll.js"></script>
<script src="js/resetOnClick.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet"/>
<link href="css/styles.css" rel="stylesheet"/>
<link href="css/notifbox.css" rel="stylesheet"/>
</head>
<center><label class="control-label" style="font-size:30px">Reservation</label></center>
<center><label class="control-label" style="font-size:15px">Edit Record</label></center>
<br/>
<br/>
<?php
$id = isset($_GET['id'])? $_GET['id'] : "";
include('config/config1.php');
$sel = "SELECT idReservation, venue.venue_type, reservation.reservation_date, reservation.reservation_time
FROM venue
INNER JOIN reservation ON reservation.Venue_idVenue = venue.idVenue where idReservation = '$id';";
$rsvtn = isset($_POST['idReservation']);
$query = mysqli_query($conn,$sel);
while($detail = mysqli_fetch_array($query))
{
$rid = $detail['idReservation'];
$ven = $detail['venue_type'];
$res_d = $detail['reservation_date'];
$res_t = $detail['reservation_time'];
?>
<form class="form-horizontal form-label-left" method="post">
<div class="form-group">
<!--BACK Button-->
Back
<!--BACK Button-->
</div>
<!--<input type="hidden" name="submitted" value="true"/>-->
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Venue<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="venue_type" data-content="<?php echo $ven;?>" class="form-control col-md-7 col-xs-12" name="venue_type" placeholder="Venue" required="required" type="text" value="<?php echo $ven;?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Reservation Date<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reservation_date" data-content="<?php echo $res_d;?>" class="form-control col-md-7 col-xs-12" name="reservation_date" placeholder="Reservation Date" required="required" type="date" value="<?php echo $res_d;?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Reservation Time<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reservation_time" data-content="<?php echo $res_t;?>" class="form-control col-md-7 col-xs-12" name="reservation_time" placeholder="Reservation Time" required="required" type="time" value="<?php echo $res_t;?>">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<input type="hidden" name="updRsvtn" value="<?php echo $detail['idReservation'];?>">
<input type="submit" class="btn btn-primary submits" value="Save Edited" onClick="Reservation Records.php" >
</div>
</div>
<?php
}
?>
<?php
if(isset($_POST['updRsvtn']))
{
include('config/config1.php');
$ven = $_POST['venue_type'];
$res_d = $_POST['reservation_date'];
$res_t = $_POST['reservation_time'];
if(!mysqli_query($conn,"UPDATE reservation, venue SET venue_type = '$ven', reservation_date = '$res_d', reservation_time = '$res_t' WHERE idReservation = '$id'"))
{
echo "Not Queried: Wrong variables !";
}
else
{
header("Location: Reservation Records.php");
}
mysqli_close($conn);
}
?>
</form>
<table class = "notif pos">
<thead>
<tr>
<th><center/><b>*Old Record</b></th>
</tr>
</thead>
<tbody>
<tr>
<td><center/><b>Venue: <?php echo $ven;?></td>
</tr>
<tr>
<td><center/><b>Reservation Date: <?php echo $res_d;?></td>
</tr>
<tr>
<td><center/><b>Reservation Time: <?php echo $res_t;?></td>
</tr>
</tbody>
</table>
</body>
if its not working that means your their is not value in
$_GET['id']. then $id is having blank value that's why your query
update for all rows.
if(isset($_POST['updRsvtn']))
{
$id = isset($_GET['id'])? $_GET['id'] : "";
$ven = $_POST['venue_type'];
$res_d = $_POST['reservation_date'];
$res_t = $_POST['reservation_time'];
if(!mysqli_query($conn,"UPDATE reservation, venue SET
venue_type = '$ven', reservation_date = '$res_d',
reservation_time = '$res_t' WHERE idReservation = '$id'"))
{
echo "Not Queried: Wrong variables !";
}
else
{
header("Location: Reservation Records.php");
}
mysqli_close($conn);
}
You could try JOIN in your query
UPDATE reservation t1
JOIN venue t2 ON (t1.Venue_idVenue = t2.idVenue)
SET t1.reservation_date = '$res_d',
t1.reservation_time = '$rest_t',
t2.venue_type = '$ven'
WHERE t1.idReservation = '$id'"

Categories