I have a Checkbox field in html input form. In this the value are fetched from another table.I want to select the multiple user with the help of this check box.What is wrong with my code
<div class="form-group col-md-6">
<label> Supervised BY( At IUAC)<span style="color:red;">*</span></label>
<input type="checkbox" name="user" > <br>
<checkbox value=""> </checkbox>
<?php
$sql = "SELECT * from tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
<?php }} ?>
I tried this code bu it is not showing the check box
Use input type Checkbox field Like this
<?php
foreach($results as $result)
{ ?>
<input type="checkbox" name="<?php echo $result->name; ?>" value="<?php echo $result->name; ?>"> <?php echo $result->name; ?> </br>
<?php } ?>
<input type="checkbox">
Correct code:
<div class="form-group col-md-6">
<label> Supervised BY( At IUAC)<span style="color:red;">*</span></label>
<?php
$sql = "SELECT * from tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{
?>
<input type="checkbox" name="user" value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?><br>
<?php
}}
?>
Edit:
Try with multiple attribute of <select>
<?php
$sql = "SELECT * from tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
echo "<select name='user' multiple>";
foreach($results as $result)
{
?>
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
<?php
}
echo "</select>";
}
?>
You can use bootstrap-multiselect Like This
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dropdown Multi Select</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.12/css/bootstrap-multiselect.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>
</head>
<body>
<form id="formone">
<div style="padding:20px">
<select id="chkone" multiple="multiple">
<?php
$sql = "SELECT * from tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
<?php }} ?>
</select>
</div>
</form>
</body>
<script type="text/javascript">
$(function() {
$('#chkone').multiselect({
includeSelectAllOption: true
});
});
</script>
</html>
Just Try this
<div class="form-group col-md-6">
<label> Supervised BY( At IUAC)<span style="color:red;">*</span></label>
<?php
$sql = "SELECT * from tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<input type="checkbox" name="user[]" value="<?php echo htmlentities($result->id);?>" ><?php echo htmlentities($result->name);?>
<?php }} ?>
when you will submit the form, you will get $_POST['user'] as an array of user ids's that are checked.
Related
JQuery connected, AJAX probably works correctly, because when I change select in the console the values are correct. But the query to the database doesn't change. Checking var_dump($_POST['filter']) shows that there value from previous request.
If I wrap select in a form and send it via POST using submit button, the requests are sent.
Where can there be an error here?
And how to add a condition, so that when selecting "All" the query will just be to all items "SELECT * FROM orders, without the condition "WHERE?
<script>
$('#filter').change(function() {
$.ajax({
method: "POST",
url: "thispage.php",
data: {
filter: $("#filter").val()
},
success: function(response) {
console.log($("#filter").val())
}
});
});
</script>
<?php
require_once __DIR__ . "/database/database.php";
$item1 = $_POST['item1'];
$worker_name = $_POST['worker_name'];
$worker_company = $_POST['worker_company'];
$errors = [];
if (empty($item1)) {
$errors['item1'] = true;
}
if (empty($errors)) {
$sql = "INSERT INTO `orders`(`order_dish1`, `order_name_worker`, `order_company`) VALUES (:order1, :order_name_worker, :worker_company)";
$params = [
"order1" => $item1,
"order_name_worker" => $worker_name,
"worker_company" => $worker_company
];
$dbh->prepare($sql)->execute($params);
}
$order_dishes1 = [];
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<select type="text" name="filter" id="filter">
<option value="*">All</option>
<?php
$sql = "SELECT * FROM `companies`";
$rows = $dbh->query($sql);
foreach ($rows as $row) {
?>
<option value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?>
</option>
<?php } ?>
</select>
// In this form it works
<form action="thispage.php" method="POST">
<select type="text" name="filter" id="filter">
<option value="*">All</option>
<?php
$sql = "SELECT * FROM `companies`";
$rows = $dbh->query($sql);
foreach ($rows as $row) {
?>
<option value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?>
</option>
<?php } ?>
</select>
<button type="submit">submit</button>
</form>
<div class="orders__wrapper" id="result">
<?php
$company = $_POST['filter'];
var_dump($_POST['filter']);
$sql = "SELECT * FROM `orders` WHERE `order_company` = '$company'";
$rows = $dbh->query($sql);
foreach ($rows as $row) {
?>
<div class="order-items">
<div class="order-items__header">
<div class="order-items__wrapper">
<div class="order-items__number">
Order №: <?php echo $row['order_id'] ?> </div>
</div>
<div class="order-items__wrapper">
<div class="order-items__name">
<?php echo $row['order_name_worker'] ?> </div>
<div class="order-items__company">
Company:
<span>
<?php echo $row['order_company'] ?>
</span>
</div>
</div>
</div>
<div class="order-items__orders">
<div class="orders-item orders-item-1">
Order №1:
<span class="order1">
<?php echo $row['order_dish1'] ?>
</span>
</div>
</div>
</div>
<?php
$order_dishes1[] = $row['order_dish1'];
}
$content = ob_get_contents();
ob_end_clean();
?>
<div class="result__all" id="result__all">
<div class="result__all-company">
<?php echo $row['order_company'] ?>
</div><br>
<div class="result__all__wrapper">
<div class="result__all-view">
<div class="result__all-title">
Soups:
</div>
<?php
$count_dishes1 = array_count_values($order_dishes1);
foreach ($count_dishes1 as $key => $val) echo '<div class="result__all-item">' . $key . ' - ' . $val . ' шт.,</div>';
?>
</div><br>
</div>
</div>
<?= $content ?>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</body>
</html>
I have two input named uin and order date these two field fetch the data from a table.Second field is depends on th value of first field and the value of second field is fetched using ajax.There is one button (Generate)hyperlink which navigates to new page viewstationerypdf.php carrying the value selected in these two field.When i click on this button it does not react it is not taking me into new page .Data is fetching in both the input field .My code is-
<div class="form-group col-md-12">
<label> UIN<span style="color:red;">*</span></label>
<select class="form-control" name="uin" id="uin">
<option value=""> </option>
<?php
$sql = "SELECT uin from tblstationerystock group by uin order by uin asc";
$query = $dbh->prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->uin);?>">
<?php echo htmlentities($result->uin);?>
</option>
<?php }} ?>
</select>
</div>
<div class="form-group col-md-12">
<label> ORDER DATE<span style="color:red;">*</span></label>
<select class="form-control" name="orderdate" id="orderdate" value="" ;>
</select>
</div>
<a href="stationerypdf.php?uin=<?php echo $_POST[uin];?> & orderdate= <?php echo $_POST[orderdate];?> target="popup" onclick="window.open('stationerypdf.php?uin=<?php echo $_POST[orderdate];?> & orderdate= <?php echo $_POST[orderdate];?> ,'popup','height=600,width=900 top=15 left=300,location=no,toolbar=no,resizable=no, scrollbars=no'); return false;" > GENERATE</a>
Ajax code is
<?php
include('includes/config.php');
$uin = $_POST['uin'];
echo "<option>Select ORDER DATE</option>";
$sql = "SELECT orderdate from tblstationerystock WHERE uin=$uin group by orderdate";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->orderdate);?>">
<?php echo htmlentities($result->orderdate);?>
</option>
<?php }}
?>
Objective Inserting name(input-text) and info(textarea-contains multiple lines) into the database, and after submission of the form, at the same page, 2 columns are for displaying the same data in columns, name & info, but under info column. I have made buttons for each row in front of the names, which is used as slideToggle for showing/hiding which contains the data retrieved from the 'info' column
Problem - when I am clicking the button of 1st row, instead of displaying the info related to 1st entry only, it is sliding and showing all info(s) related to all entries at only click.
*others - one more input has been added to the form but as hidden used for id(auto increment)
----index.php-----
<?php include('php_code.php'); ?>
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM records WHERE id=$id");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$acc = $n['acc_no'];
$info = $n['info'];
}
}
?>
<html>
<head>
<title>JSK</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form').hide();
$('p').hide();
$('#sp').hide();
$("#inf").click(function(){
$("p").slideToggle();
$('#sp').slideToggle();
});
$("#fo").click(function(){
$("form").slideToggle();
});
});
</script>
</head>
<body>
<div class="container">
<div class="left">
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>
<?php $results = mysqli_query($db, "SELECT * FROM records"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Account No</th>
<th>Info</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['acc_no']; ?></td>
<td><button id="inf" onclick="myFunction()">show</button></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
Delete
</td>
</tr>
<tr id="sp"> <td colspan="4"><p> <?php echo $row['info']; ?> </p></td>
</tr>
<?php } ?>
</table>
<div id="fotable" align="center">
<button id="fo">Add New/Edit</button>
</div>
<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" autocomplete="off" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Account No</label>
<input type="text" name="acc" value="<?php echo $acc; ?>">
</div>
<div class="input-group">
<label for="info">Info</label>
<textarea class="form-control" rows="8" name="info" id="info"><?php echo $row['info']; ?></textarea>
</div>
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Add Account</button>
<?php endif ?>
</div>
</form>
</div><!-- left closed -->
<div class="right">
hello
</div> <!-- right closed -->
</div> <!-- container closed -->
</body>
</html>
---php_code.php-----
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'jskrecords');
// initialize variables
$name = "";
$acc = "";
$info = "";
$id = 0;
$update = false;
if (isset($_POST['save'])) {
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "INSERT INTO records (name, acc_no, info) VALUES ('$name', '$acc', '$info')");
$_SESSION['message'] = "Account saved";
header('location: index.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "UPDATE records SET name='$name',acc_no='$acc',info='$info' WHERE id=$id");
$_SESSION['message'] = "Account updated!";
header('location: index.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM records WHERE id=$id");
$_SESSION['message'] = "ACC deleted!";
header('location: index.php');
}
?>
Concept:
If you are creating multiple form from same mysql table using php script, You need to give each form a unique id.
e.g.
<form method="post" action="php_code.php" id="form<?= $id ?>">
Then add data-target="#form" to button with class 'inf'. It will store id of form.
<button class="inf" data-target="#form<?= $id ?>">show</button>
When button is clicked we know which form to open from data-target.
<script>
$('.container').on('click','button.inf',function(e){
e.preventDefault();
var formid=$(this).attr('data-target'); //get value of data-target attribute
//...proceed to play toggle with this form 'formid'
I have 4 pages: register.php, view.php , edit.php and display.php
On view.php i have a form that displays all data from database.
I have a search box, results of which are displayed in display.php(it displays just one row from database).
In display php i have a form with edit button for the entry. The edit button redirect me to edit.php where i can change my data. When i save, it redirect me on view.php but i want in display php with saved values of edited entry.
I tried in but it desnt work.
The second problem i have here is to keep in edit.php the dropdown option that i selected in in register.php
I;m new in programation and i hope someone will help me in this problem. THX for help.
My pages:
DISPLAY.PHP
<?php
include('connect-db.php');
$client = $_POST['client'];
$contract = $_POST['contract'];
if(
$contract = $_POST['contract'] )
{$query = "select * from users where contract = '$contract'"; }
else{
$query= "select * from users where client = '$client'";
}
$result = mysql_query($query);
echo "<table>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// echo out the contents of each row into a table
echo '<label>Contract</label><input readonly="true" value=' . $row['contract'] . '>';
echo '<label>Client</label><input readonly="true" type="text" value="' . $row['client'] . '">';
echo '<label>Step</label><input type="text" readonly="true" value=' . $row['Step'] . '>';
echo '<br><input type="submit"value="Change">';
echo '<br>';
echo "</table>";
}
?>
EDIT.PHP
<?php
function renderForm($contract, $client, $step
{
?>
<!DOCTYPE html>
<body>
<form id="base" name="base" method="post" action="">
<input data-validate="number" value="<?php echo $contract; ?>" readonly="true" id="contract">
<input data-validate="text" value="<?php echo $client; ?>" id="client">
<select name="step">
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
<input type="submit" value="Change">
</form>
</body>
</html>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['contract']))
{
$contract = mysql_real_escape_string(htmlspecialchars($_POST['contract']));
$client = mysql_real_escape_string(htmlspecialchars($_POST['client']));
$step = mysql_real_escape_string(htmlspecialchars($_POST['step']));
if ($contract == '' || $client == '' )
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($contract, $client, $step, $error);
}
else
{
mysql_query("UPDATE users SET
contract='$contract', client='$client', step='$step', WHERE contract='$contract'")
or die(mysql_error());
header("Location: view.php");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['contract']) && is_numeric($_GET['contract']) && $_GET['contract'] > 0)
{
$contract = $_GET['contract'];
$result = mysql_query("SELECT * FROM users WHERE contract=$contract")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$contract = $row['contract'];
$client = $row['client'];
$step = $row['step'];
renderForm($contract, $client, $step, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
VIEW.PHP
<!DOCTYPE html>
<html>
<?php
include('connect-db.php');
$sql="SELECT * FROM users";
$result =mysql_query($sql);
{
?>
<table>
<thead>
<tr>
<th span style="font-weight: normal;">Contrat</th>
<th span style="font-weight: normal;">Client</th>
<th span style="font-weight: normal;">Step</th>
</tr>
</thead>
<?php
}
while ($data=mysql_fetch_assoc($result)){
?>
<tbody>
<tr>
<td><?php echo $data['contract'] ?></td>
<td><?php echo $data['client'] ?></td>
<td><?php echo $data['step'] ?></td>
<td><input type="button" value="Change"></td>
</tr> <?php } ?>
</tbody>
</table>
</body>
</html>
REGISTER.PHP
<!DOCTYPE html>
<html>
<form id="base" method="post" action="insert.php">
<br>
<br>
<input data-validate="number" id="contract" name="contract">
<input data-validate="text" id="client" name="client">
<select name="step">
<option value="option1">OPTION1</option>
<option value="option2">OPTION2</option>
</select>
<button data-validate="submit">Register</button>
</form>
</body>
</html>
use this instead of your edit.php
<?php
function renderForm($contract, $client, $step
{
?>
<!DOCTYPE html>
<body>
<form id="base" name="base" method="post" action="">
<input data-validate="number" value="<?php echo $contract; ?>" readonly="true" id="contract">
<input data-validate="text" value="<?php echo $client; ?>" id="client">
<select name="step">
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
<input type="submit" value="Change">
</form>
</body>
</html>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['contract']))
{
$contract = mysql_real_escape_string(htmlspecialchars($_POST['contract']));
$client = mysql_real_escape_string(htmlspecialchars($_POST['client']));
$step = mysql_real_escape_string(htmlspecialchars($_POST['step']));
if ($contract == '' || $client == '' )
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($contract, $client, $step, $error);
}
else
{
mysql_query("UPDATE users SET
contract='$contract', client='$client', step='$step', WHERE contract='$contract'")
or die(mysql_error());
header("Location:display.php");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['contract']) && is_numeric($_GET['contract']) && $_GET['contract'] > 0)
{
$contract = $_GET['contract'];
$result = mysql_query("SELECT * FROM users WHERE contract=$contract")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$contract = $row['baza_contract'];
$client = $row['baza_client'];
$step = $row['step'];
renderForm($contract, $client, $step, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
I need the code for getting the addr and pnum of the patient when I choose the pname in the combo box.
How can I do that?
<script>
function getVal() {
document.getElementById("text2").value = document.getElementById("model").value;
}
</script>
<body>
//code in opening and getting my addr and pnum in dbase
<?php
include('connect.php');
$pname=$_GET['tpname'];
$result = mysql_query("SELECT * FROM tblnpatient where pname='$pname'");
while($row = mysql_fetch_array($result))
{
$pnum=$row['pnum'];
$addr=$row['addr'];
}
?>
//code for choosing pname
<tr><td>Patient Name:
<div id="ma">
<select name="pname" class="textfields" id="model" style="width:180px;" onchange="getVal();">
<option id="0" >--Select Patient Name--</option>
<?php
$con=mysqli_connect("localhost","root","","dbnpatient");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$pnum=$_GET['pnum'];
$query = mysqli_query($con, "SELECT * FROM tblnpatient");
while($row = mysqli_fetch_array($query)){
$pnum = $row['pnum'];
$pname = $row['pname'];
?>
<option id="<?php echo $pnum; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?> </option>
<?php } ?>
</select>
//code for getting pname and addr
Address:<input type="text" name="ename" size="20" id="ma" value="<?php echo $addr ?>"/>
Name:<input type="text" name="ename" size="20" id="ma" value="<?php echo $pname ?>"/>
In your while loop add the following (if you have that column otherwise replace with something similar)
$paddress = $row['paddress'];
You can store the needed information by using the data attribute in your options
<option id="<?php echo $pnum; ?>" data-pname="<?php echo $pname; ?>" data-paddress="<?php echo $paddress; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?></option>
Then change your getVal() function
function getVal() {
var options = document.getElementById("model").options;
if (options.selectedIndex != -1) {
var addr = document.getElementById('paddress');
var name = document.getElementById('pname');
addr.value = options[options.selectedIndex].getAttribute('data-paddress');
name.value = options[options.selectedIndex].getAttribute('data-pname');
}
}
Now change the id's of the input fields for the address and the name to paddress and pname. It is important to know to never have duplicate id's
I hope that helped