Different Text area for each row - php

I am working with PHP and I am able to gather the info of my database table and show it into my webpage.
For each row I am displaying a button which leads to a modal popup with a textarea containing a WYSIWYG ( I used TinyMCE ) so that I can add Details about that specific row.
This works just with 1 row when I am using this script
This is my script:
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['savechanges'])) {
$results = $link->query("UPDATE mytalbename SET details='$_POST[textarea]' WHERE id=44");
}
?>
My problem is: how to extend this concept to all the rows in the way that each row has a button. Each button leads to a modal popup with a text area. From each text area I can modify the column "details" referring to that specific row.
And this is the code of the text area into the Form
<div class="modal-body">
<form action="" method="post" name="savechanges">
<textarea name="textarea"></textarea>
<button type="submit" class="btn btn-primary" name="savechanges"> Save changes</button>
</form>
</div>
Here I display the button:
<a class='btn btn-primary btn-sm get_info2' data-toggle='modal' data-target='#myModal2' name='job_id' value='[$job_id]'>Add Details</a>
<a class='btn btn-primary btn-sm get_info3' data-toggle='modal' data-target='#myModal3' name='job_id' value='[$job_id]'>View Details</a>
I Attach picture:
Table
[Modal popup with textarea][2]
----UPDATE 1 ----
$results = $link->query("UPDATE job SET details='$_POST[textarea]' WHERE id='$_POST[hidden]'" );
and
<div class="modal-body">
<form action="" method="post" name="savechanges">
<textarea name="textarea"></textarea>
<button type="submit" class="btn btn-primary" name="savechanges"> Save changes</button>
<input type="hidden" name="hidden" value=" . $row['id'] . ">
</form>
</div>
-----------Update 2---------------
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['savechanges'])) {
$results = $link->query("UPDATE job SET details='$_POST[textarea]' WHERE id='44'" );
}
?>
----------------UPDATE 3--------------------
<div class="modal-body info_data2">
<form action="" method="post" name="savechanges">
<textarea name="textarea"> </textarea>
<button type="submit" class="btn btn-primary" name="savechanges"> Save changes</button>
<input type="hidden" name="hidden" value="<?php echo $row['id'];?>">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div></div></div></div> </div>
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['savechanges'])) {
$results = $link->query("UPDATE job SET details='".$_POST['textarea']."' WHERE id=".$_POST['hidden']);
}
?>
---------------UPDATE 4 ------------ ALL CODE
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' }); tinyMCE.activeEditor.getContent();</script>
</head>
<body>
<!-- ------SCRIPT that fetch data from database and show table-->
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');}
if (isset($_POST['update'])) {
$results = $link->query("UPDATE customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'");
}
$sql = "SELECT * from job";
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>"; }
echo " </tr>
</thead>
<tbody>";
while ($row = $result->fetch_assoc()) {
$job_id = $row['id'];
echo "<form action='' method=post>";
echo "<tr class='info'>
<input type=hidden name=hidden value=" . $row['id'] . ">
<td>" . $row['id'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['model'] . "</td>
<td>" . $row['problem'] . "</td>
<td>
<select class='form-control col-sm-10' id='status' name='status'>
<option value='new' ". ($row['status'] == 'new'? 'selected ': '') .">New</option>
<option value='progress' ". ($row['status'] == 'progress'? 'selected ': '') .">Progress</option>
<option value='wait' ". ($row['status'] == 'wait'? 'selected ': '') .">Wait</option>
<option value='done' ". ($row['status'] == 'done'? 'selected ': '') .">Done</option>
<option value='close' ". ($row['status'] == 'close'? 'selected ': '') .">Close</option>
</select>
</td>
<td>
<button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button>
</td>
<td>
<a class='btn btn-primary btn-sm get_info' data-toggle='modal' data-target='#myModal' name='job_id' value= '[$job_id]'>Customer Info</a>
<a class='btn btn-primary btn-sm get_info2' data-toggle='modal' data-target='#myModal2' name='job_id' value= '[$job_id]'>Add Details</a>
</td>
</tr>";
echo "</form>"; }
echo "
</tbody>
</table>";
?>
<!-- ------SCRIPT FOR BUTTON "CUSTOMER INFO"-->
<script>
$(document).ready(function(){
$('.get_info').click(function(){
var job_id = $(this).parent().siblings('input[name=hidden]').val();
$.ajax({
url: 'responses.php',
data: 'job_id=' + job_id,
type: 'POST',
success: function(data){
// console.log(data);
$('.info_data').html(data);
}
});
});
});
</script>
<!-- MODAL THAT SHOW UP AFTER CLICKING ON "CUSTOMER INFO"-->
<div class="container">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Customer Information</h4>
</div>
<div class="modal-body info_data">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<!-- ---SCRIPT FOR BUTTON "ADD DETAILS" -->
<script>
$(document).ready(function(){
$('.get_info2').click(function(){
var job_id = $(this).parent().siblings('input[name=hidden]').val();
$.ajax({
url: 'responses2.php',
data: 'job_id=' + job_id,
type: 'POST',
success: function(data){
// console.log(data);
$('.info_data2').html(data);
}
});
});
});
</script>
<!-- MODAL THAT SHOW UP AFTER CLICKING BUTTON "ADD DETAILS"
which allow you to send data to database through the text area -->
<div class="container">
<div class="modal fade" id="myModal2" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Job Details</h4>
</div>
<div class="modal-body info_data2">
<form action="" method="post" name="savechanges">
<textarea name="textarea"> </textarea>
<button type="submit" class="btn btn-primary" name="savechanges"> Save changes</button>
<input type="hidden" name="hidden" value="<?php echo $row['id'];?>">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<!-- SCRIPT THAT SHOULD be able to send what is written in the text area to the database,-->
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['savechanges'])) {
$results = $link->query("UPDATE job SET details='".$_POST['textarea']."' WHERE id=".$_POST['hidden']);
}
?>
</body>
</html>
--------------------UPDATE 5 ------------------
SECTION 1
PHP code that fetch data from the table JOB
and show for each row 3 button:
update button for updating the status and the priority
customer info button (fetch data from table "customer")
add details button (send information with through a modal pop up with a wusiwug to the column "details of the table JOB")
view details button: fetch data from the column "details of the table JOB"
SECTION 2:
SCRIPT FOR BUTTON "CUSTOMER INFO"
SECTION 3:
MODAL THAT SHOW UP AFTER CLICKING ON "CUSTOMER INFO"
SECTION 3:
SCRIPT FOR BUTTON "ADD DETAILS"
SECTION 4:
MODAL THAT SHOW UP AFTER CLICKING BUTTON "ADD DETAILS"
which allow you to send data to database through the text area
SECTION 5:
SCRIPT THAT SHOULD be able to send what is written in the text area to the database
OVERVIEW

OK, if everything is working and you just need change it to work for any record, you just need to change 44 in your php code with $_POST['hidden']
So your html and php would be something like this:
HTML
<div class="modal-body">
<form action="" method="post" name="savechanges">
<textarea name="textarea"></textarea>
<button type="submit" class="btn btn-primary" name="savechanges"> Save changes</button>
<input type="hidden" name="hidden" value="<?php echo $row['id'];?>">
</form>
</div>
PHP
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['savechanges'])) {
$results = $link->query("UPDATE job SET details='".$_POST['textarea']."' WHERE id=".$_POST['hidden']);
}
?>
But I do encourage you to practice mysqli parameterized query and not using variables directly inside your queries...
UPDATE
Since your code is really hard to understand, I will just update my answer to add some assumptions, just in case they are right!
I do not know how you are showing <div> with the title of <h4 class="modal-title">Add Job Details</h4>. So let's assume that $('.info_data2').html(data); is doing what you want to do!
So in your JavaScript you have:
<script>
$(document).ready(function(){
$('.get_info2').click(function(){
var job_id = $(this).parent().siblings('input[name=hidden]').val();
$.ajax({
url: 'responses2.php',
data: 'job_id=' + job_id,
type: 'POST',
success: function(data){
// console.log(data);
$('.info_data2').html(data);
}
});
});
});
</script>
Here you are just passing job_id as the parameter, so in the responses2.php you have two issues:
1- you have to change $_POST['hidden'] to $_POST['job_id']
2- you do not have any parameter for the textarea!
Then you have to change your JavaScript to something like this:
<script>
$(document).ready(function(){
$('.get_info2').click(function(){
var job_id = $(this).parent().siblings('input[name=hidden]').val();
var textarea = $(this).parent().siblings('[name=textarea]').val();
$.ajax({
url: 'responses2.php',
data: { "job_id": job_id, "textarea" : textarea} ,
type: 'POST',
success: function(data){
// console.log(data);
$('.info_data2').html(data);
}
});
});
});
</script>
Remember, since your HTML code is really confusing, you have to work on how to get correct value of job_id and textarea. with this code I'm sure these are not returning correct values. it's just a sample of how you do what you want to do and just to put you on a correct Track!!!
as you can see, I have have added this line: var textarea = $(this).parent().siblings('name=textarea').val(); and also changed the data line to this: data: { "job_id": job_id, "textarea" : textarea} , to be able to send the textarea value to the responses2.php file...
Then in responses2.php you have to change it like this:
<?php
include("connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['savechanges'])) {
$results = $link->query("UPDATE job SET details='".$_POST['textarea']."' WHERE id=".$_POST['job_id']);
}
?>

Related

Inserting table data to sql

I have a modal that will let user choose an equipment then input the quantity.
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="button-1 btn-lg" data-toggle="modal" data-target="#myModal" style="width: 26%; margin:0; height: 40px; font-size: 100%;">Choose Equipment</button>
<!-- MODAL -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- MODAL CONTENT-->
<div class="modal-content">
<div class="modal-header">
<p>Choose An Equipment Below</p>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>Available Equipments</p>
<div class="form-inline">
<!-- DROPDOWN LIST FROM DATABASE -->
<select id="equipment" name="equipment" class="form-control">
<option selected="" disabled="">Select Equipment</option>
<?php
$sql = mysqli_query($conn, "SELECT resource_name From resources WHERE resource_type = 'EQUIPMENT';");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option name = 'equipment' value ='". $row['resource_name'] ."'>" .$row['resource_name'] ."</option>" ;
}
?>
</select>
<br><br>
<input type="text" id="qnty-1" class="form-control" placeholder="Quantity" name="quantity" style="width:24%; margin-left: 50px;"><br/>
<br/><br/>
</div>
</div>
<div class="modal-footer">
<input class="submit-3" id="addToTable" type="submit" value="Submit" name="submit-2" onclick="passValue1(); trigData()" data-dismiss="modal" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
When submitted, the modal will disappear and the data that has been selected will display on the table. I can insert the first selected data to the database but if I choose 2 different data or same data, It will just insert one on the database. Is there a way I can insert all selected data to the database?
<div class="b-form-outline">
<h1>Equipment Borrowing Form</h1>
<div class="borrow-list-area">
<table class="container-2" id="container-2">
<thead>
<tr>
<style>
th,
td {
text-align: center;
}
</style>
<th>Equipment</th>
<th>Quantity</th>
<th hidden>Serial No.</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
function passValue1() {
document.getElementById("quantity").innerHTML =
document.getElementById("qnty-1").value;
}
$('#addToTable').click(function() {
var eqpmnt = $('#equipment').val(),
qnt = $('#qnty-1').val();
$('table tbody').append('<tr><td>' + eqpmnt + '</td><td>' + qnt + '</td>
</tr>');
});
I've tried this insert code but again, it will only insert one row of data from the table. If anyone can please help me. Thanks a lot!
<?php
include('dbconnector.php');
$id_num = $_POST['idnumber'];
$s_date = $_POST['startdate'];
$e_date = $_POST['enddate'];
$s_time = $_POST['starttime'];
$e_time = $_POST['endtime'];
$prpse = $_POST['purpose'];
$equipment = $_POST['equipment'];
$qnty = $_POST['quantity'];
$subj = $_POST['subject'];
$onCamp = isset($_POST['check-1']) ? $_POST['check-1'] : "Off-Campus";
// CREATES A NEW TRANSACTION THAT WAS SUBMITTED USING THE BORROW FOWM
$query = "SELECT * FROM resources;"; // ACCESS TABLE RESOURCES
$con = mysqli_query($conn, $query);
if ($con->num_rows > 0){
while($row = mysqli_fetch_array($con)){
$rsname = $row['resource_name'];
if (strcmp($equipment, $rsname) == 0) {
$sql = "INSERT INTO transactions (id_number, start_date, end_date, start_time, end_time, purpose, quantity, subject, use_place) VALUES ('$id_num', '$s_date', '$e_date', '$s_time', '$e_time', '$prpse', '$qnty', '$subj', '$onCamp');";
}
}
}
if (!mysqli_query($conn,$sql)) {
$message = "Error Sending Form";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else {
header("refresh: 1; url=borrow.php");
}
?>
Adjusted for #Jon Stirling
$query = "SELECT * FROM resources;"; // ACCESS TABLE RESOURCES
$con = mysqli_query($conn, $query);
if ($con->num_rows > 0){
while($row = mysqli_fetch_array($con)){
$rsname = $row['resource_name'];
if (strcmp($equipment, $rsname) == 0) {
$sql = "INSERT INTO transactions (id_number, start_date, end_date, start_time, end_time, purpose, quantity, subject, use_place) VALUES ('$id_num', '$s_date', '$e_date', '$s_time', '$e_time', '$prpse', '$qnty', '$subj', '$onCamp');";
if (!mysqli_query($conn,$sql)) {
$message = "Error Sending Form";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else {
header("refresh: 1; url=borrow.php");
}
}
}
}

When Clicked edit button, corresponding details must be filled to the form for edit

I have a form which can submited.and have list,that listed all submited form details.
I tried it in different ways.I want to fill the form with the corresponding details when I clicked the edit button.
Here is my php file
<div class="row">
<div class="col-sm-9">
<b>Leader Name : </b><?php echo($row["lead_name"]); ?><br>
<b>Phone Number : </b><?php echo($row["phone_number"]); ?><br>
<b>Email : </b><?php echo($row["email"]); ?><br>
<b>Part Created Time : </b><?php echo($row["create_date_and_time"]); ?>
<br>
</div>
<div class="col-sm-3 ">
<form role="form" action='index.php' method='POST'>
<input type='hidden' name='party_id' value='<?php echo($row["party_id"]); ?> '>
<input type="submit" class="btn btn-sm btn-success btn-block" id="edit" name="edit" value="Edit" style="font-size: 12px; padding: 3px;">
<input type="submit" class="btn btn-sm btn-danger btn-block" id="delete" name="delete" value="Delete" style="font-size: 12px; padding: 3px;">
</form>
<?php
if (isset($_POST['delete'])) {
print("<script> alert('delete'); </script>");
$party_id = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$queryDelete = "DELETE FROM party_details WHERE party_id='$party_id'";
if ($conn->query($queryDelete)) {
$_SESSION['party'] = "";
$_SESSION['trips'] = [];
print("<script>
alert('Party removed');
window.location.href='../tripCreate';
</script>");
} else {
print("<script>alert('Error when remove ! ');</script>");
}
$_POST = array();
}
if (isset($_POST['edit'])) {
$party_id1 = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$query1 = "SELECT * FROM party_details WHERE party_id='$party_id1'";
$result1 = $conn->query($query1);
$row1 = $result1->fetch_assoc();
}
?>
</div>
first of all, you should specify not only result you want to achieve, but also what kind of problem you are facing.
is it php error, or information not being displayed in resulted page?
one thing i spotted is that you got $row1 = $result1->fetch_assoc(); but in form you echo $row[] (instead of $row1[]), which i dont see being created anywhere.
also, did you try var_dump($row) in php and check its content (or $row1...)?

Show all equal values from table inside same div

This code gets all values from a table and for each row it shows its details inside a alert div and i can click a "order ready button" for that single product.
What I need to do is put in a single div all the products that are from the same order, and for that I'm thinking about using all the rows that have the same date value and when this value changes create a new div.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM kitchen");
while ($row = mysqli_fetch_array($result)) {
$table = $row['table'];
$customer = $row['customer'];
$product = $row['product_name'];
$code = $row['product_code'];
$size = $row['size'];
$id = $row['id'];
$date = $row['date'];
// It would have to open here in each first distinct $date
echo '<div class="alert alert-info" role="alert" id="'.$code.'">';
echo '<h4>'.'Table '.$table.'</h4>';
echo '<h4>'.'Name: '.$name.'</h4>';
// Repeat this for each equal $date value
if($code=="A01"||$code=="A02"||$code=="A03"||$code=="A04"){
echo '<h4>'.$code.' - '.$product.' ('.$size.')'.'</h4>';
}
else{
echo '<h4>'.$code.' - '.$product.'</h4>';
}
// Close here before each next distinct $date
echo '<form action="actionkitchen.php" method="post">';
echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data' type='submit'>Order Ready</button>";
echo '</form>';
echo '</div>';
}
?>
This is what I ended up with, not the most elegant solution but it's working.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM kitchen");
while ($row = mysqli_fetch_array($result)) {
$table[] = $row['table'];
$name[] = $row['name'];
$product[] = $row['product_name'];
$code[] = $row['product_code'];
$size[] = $row['size'];
$date[] = $row['date'];
}
$count = array_count_values($date);
$y = 0;
foreach ($count as $item){
for($i=0;$i<$item;$i++){
if($i==0){
echo '<div class="alert alert-info">';
echo '<h4>'.'Table '.$table[$y].'</h4>';
echo '<h4>'.'Name: '.$name[$y].'</h4>';
}
if($code[$y]=="A01"||$code[$y]=="A02"||$code[$y]=="A03"||$code[$y]=="A04"){
echo '<h4>'.$code[$y].' - '.$product[$y].' ('.$size[$y].')'.'</h4>';
}
else{
echo '<h4>'.$code[$y].' - '.$product[$y].'</h4>';
}
if($i==$item-1){
echo '<form action="actionkitchen.php" method="post">';
echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data[$y]' type='submit'>Order Ready</button>";
echo '</form>';
echo '</div>';
}
$y++;
}
}
?>
To set your products in the same order, I would group them by the key in an array. For our purposes, we'll use a multidimensional array so that we can add our products within the unique key (using "date" in the example). Below you will see me set the array, fetch the rows from the database (sorting by our group key so that we have some consistency on the front end) and begin placing them in their unique groups. When pushing a product into the date array, I am using array_merge() in combination of in_array() and a ternary operator to set the "product string" within the HTML.
<?php
/* Fetch/Set Kitchen */
$kitchen = array();
$sql = "SELECT * FROM `kitchen` ORDER BY `date`";
$query = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($query)) {
$kitchen[$row['date']][] = array_merge($row, array(
'product_string' => (in_array($row['product_code'], array('A01', 'A02', 'A03', 'A04')) !== FALSE)
? $row['product_code'] . ' - ' . $row['product_name'] . ' (' . $row['size'] . ')'
: $row['product_code'] . ' - ' . $row['product_name']
));
}
?>
To keep our HTML tidy and readable apart from our PHP, you'll see that I've chosen to use an alternative syntax for the control structures. This helps by using tab indentations from having put any awkwardly placed curly brackets in our code.
<?php foreach($kitchen as $date => $items): ?>
<div class="alert alert-info" role="alert" id="<?php echo $date; ?>">
<?php foreach($items as $item): ?>
<h4>Table <?php echo $item['table']; ?></h4>
<h4>Name: <?php echo $item['customer']; ?></h4>
<h4><?php echo $item['product_string']; ?></h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="<?php echo $item['data']; ?>" type="submit">Order Ready</button>
</form>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The above reference code will output HTML similar to:
<div class="alert alert-info" role="alert" id="2016-10-21">
<h4>Table Table 1</h4>
<h4>Name: Name 1</h4>
<h4>XXX1 - Product 1 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX1" type="submit">Order Ready</button>
</form>
<h4>Table Table 2</h4>
<h4>Name: Name 2</h4>
<h4>XXX2 - Product 2</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX2" type="submit">Order Ready</button>
</form>
<h4>Table Table 3</h4>
<h4>Name: Name 3</h4>
<h4>XXX3 - Product 3 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX3" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-10-27">
<h4>Table Table 4</h4>
<h4>Name: Name 4</h4>
<h4>XXX4 - Product 4</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX4" type="submit">Order Ready</button>
</form>
<h4>Table Table 5</h4>
<h4>Name: Name 5</h4>
<h4>XXX5 - Product 5 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX5" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-11-06">
...etc.

php-mysql insert multiple rows from echo radio button

Actually i want to develop a application from where user can set attendance for student. so the html form for attendance will come from my db query. and it's coming too but the prob is that how can i insert that form information to my db . actually i searched lot but i didn't get any result for this as perfect as i want i mean please can anyone help me . thanks in advance
<form action="attendance.php" method="post">
<?php include '../database-config.php';
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){
echo "<div>
<label>".htmlentities($row['student_id'])."</label>
<input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present
<input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent
</div></br>";
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
<form action="attendance.php" method="post">
<?php include '../database-config.php';
$result = mysql_query("SELECT * FROM student WHERE active_class='VII'");
foreach($result as $row)
{
?>
<div>
<label><?php echo $row['student_id']?></label>
<input type="radio" name="attend" value="present" checked>Present
<input type="radio" name="attend" value="absent">Absent
</div>
</br>
<?php
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
so in php you can get value like this
<?php
$attend = $_POST['attend'];
echo $attend;
?>
So in $attend it contain value(value="present") of radio button.
it may be present or either absent
damn getting tired xD
this should work though but you have to add the column attendency to the database table by yourself cheers
<form action="" method="post">
<?php
include '../database-config.php';
if(isset($_POST['attendency']) && isset($_POST['id']))
{
$id_to_update = $_POST['id'];
$status = $_POST['attendency'];
$ar = array('p','a');
$attend = !empty($status) && in_array($status,$ar) ? $status : 'p';
//you have to create a column named attendency for this to work
$sql = "INSERT INTO student(attendency) VALUES ('$attend ') WHERE user_id = '$id_to_update '";
$dbh->query($sql);
}
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row)
{
if($row['attendency'] == 'p')
{
$p = 'checked="checked"';
$a = '';
} else {
$a = 'checked="checked"'
$p = '';
} ?>
<div>
<input type="hidden" name="id" value="<?=$row['student_id']?>">
<label><?=$row['student_id']?></label>
<input type='radio' name='attendency' <?=$p?>>Present
<input type='radio' name='attendency' <?=$a?>>Absent
</div></br>
<?php } ?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>

POST and GET at the same time

hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated
Q.php
<?php
include("pagination.php");
if(isset($res))
{
while($result = mysql_fetch_assoc($res))
{
echo '<div class="shop-item">' ;
echo ' <div class="price">' ;
echo $result['Inquirer'] ;
echo ' </div>' ;
echo ' <div class="price">' ;
echo $result['question'] ;
echo ' </div>' ;
echo ' <div class="actions"> ';
echo '<input type="button" class="btn btn-large " value="More Info" onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
echo '</div> ';
echo ' </div> ';
}
}
?>
QR.php
<form action="QRR.php" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name ="answer" id="Test">
</textarea>
<br>
<div class="actions">
<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>
</div>
</form>
QRR.php
<?php
// variables
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];
//query
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>
if i removed passing hi from QR to QRR answer stored //$answer
if i removed storing answer from the text area the id from url stroed //$FURL
This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.
if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.
You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.
<?php
// variables
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
require ("coonection.php");
$FURL = $_POST['hi']; // there is no input name 'hi' set in your form. so this code will fail due to that.
//query
$query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data){
echo "Your Questions Has Been Successfully Added ";
}
}
?>
`
you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.
<form action="" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>
<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
</div>
</form>
NOTE: both of these snippets will go in the same page.

Categories