how to insert data in database using dropdown with foreach loop - php

here i am creating a attendance sheet that insert data into the database
after clicking submit button. The insertion of the data is base on the 2
radio button which is absent/present, the problem is after clicking submit
button the other component works accordingly except for the dropdown button,
the dropdown button insert only a single string instead of the service time
by the way i declare the service time in the database as varchar so i am
confident that it's not the database that causes the error. maybe someone
can help me any help is much appreciated. thank you in advance
enter code here
<?php
include("Sampledb.php");
include("Sample.php");
$flag=0;
if(isset($_POST['submit']))
{
foreach($_POST['attendance_status'] as $id1=>$attendance_status)
{
$name=$_POST['name'][$id1];
$id=$_POST['id'][$id1];
$date=date("Y-m-d H:i:s");
$servicetime=$_POST['servicetime'][$id1];
$result=mysqli_query($objconn,"insert into
attendrecord(id,name,attendance_status,attendance_date,servicetime)
values('$id','$name','$attendance_status','$date','$servicetime')");
if($result)
{
$flag=1;
}
}
header("Location:sampleindex.php");
exit;
}
?>
<div class="panel panel-default">
<div class="panel panel-heading">
<form action="sampleindex.php" method="Post">
<label>Service time:</label>
<select name="servicetime">
<option>00:00</option>
<option value="7:30-9:30">7:30-9:30</option>
<option value="10:30-12:30">10:30-12:30</option>
<option value="1:30-3:30">1:30-3:30</option>
<option value="4:30-6:30">4:30-6:30</option>
</select>
<a class="btn btn-info pull-right" href="view.php">View all<a/>
</div>
</div>
<div id="alert" style="display:none;" class="alert alert-success" ">
×
<strong>Success!</strong>Save
</div>
<h3>
<div class="well text-center">Date:<?php echo date("Y-m-d"); ?> </div>
</h3>
<div class="panel panel-body">
<tr>
<table class="table table-striped">
</tr>
<th>ID Number</th><th>Member Name</th><th>Attendace Status</th>
<?php $result=mysqli_query($objconn,"select* from attend");
$id = 0;
$counter=0;
while($row=mysqli_fetch_array($result))
{
$id++;
?>
<tr>
<td><?php echo $row['id']; ?></td>
<input type="hidden" value="<?php echo $row['id']; ?>"name="id[]">
<td><?php echo $row['name']; ?></td>
<input type="hidden" value="<?php echo $row['name']; ?>"name="name[]">
<td>
<input type="radio" name="attendance_status[<?php echo $counter; ?>]"
value="present">Present
<input type="radio" name="attendance_status[<?php echo $counter; ?>]"
value="absent">Absent
</td>
</tr>
<?php
$counter++;
}
?>
</table>
<input type="submit" name="submit" value="submit" onclick="return mess();">
</div>
<script type="text/javascript">
function mess()
{
if($flag=1){
alert ("Record Save!");
return true;}
else
alert ("Record Not Save!");
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
$('#alert').css('display','block');
});
});
</script>
</div>
</div>

Your service time is not an array as it keeps same for all record. So you need to change your PHP code as below:
$servicetime=$_POST['servicetime'];
Hope it helps you.

Instead of $_POST['servicetime'][$id] use $_POST['servicetime']

Related

How to send a form in a foreach loop using PHP

I want to send an Id by the form in foreach loop but when I click on each button it sends me the first form.
It means when I click on the second button it sends me the value of the first form the value of input hidden is right but the first value every time sent.
<?PHP
if ($transactions) {
foreach ($transactions as $transaction) {
?>
<td class="text-left">
<?php echo $transaction['invoice_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['invoice_type']; ?>
</td>
<td class="text-left">
<?php echo $transaction['ref_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['gateway']; ?>
</td>
<td class="text-left">
<?php echo $transaction['payment_date']; ?>
</td>
<td class="text-right">
<?php
if($transaction['payment_status'] == 'not paid') {
?>
<form
method="POST"
action="<?php echo $gate_url; ?>"
>
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
<button class="btn btn-md btn-primary" type="submit" >
<?php echo $pay; ?>
</button>
</form>
<?php
}
?>
</td>
<?php
}
You have an array of the values, so this:
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
replace with:
<input type="hidden" name="invoiceId[]" value="<?php echo $transaction['invoice_id']; ?>">

add input fields dynamically using jquery ajax

I'm trying to inserting multiple items connected with quantity and price for each. all of the items will be showing the same invoice number and vendor id.
<form name="order" id="order">
<label>Invoice Number</label>
<input type="text" name="invoicenumber" value="">
<p><label>Vendor's ID</label>
<select name="vendorid" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM customers");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $id; ?> "><?php echo $vendorname; ?></option>
<?php
} ?>
</select> </p>
<table class="table table-bordered" id="dynamic_field">
<!-- Product code begining -->
<td><tr><label>Items sold</label>
<select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select> </td>
<td><label>Quantity</label>
<input type="text" name="tquantity[]" value=""></td>
<td><label>Price</label>
<input type="text" name="saleprice[]" value=""></td>
<td><button name="add" id="add" class="btn btn-success">ADD MORE</button></td></tr>
</table>
<!-- Product code END -->
<input type="submit" name="submit" value="submit">
</form>
<!--Jquery code to add more fields -->
<script>
$(document).ready(function(){
var i = 1;
$(#add).click(function(){
i++;
$(#dynamic_field).append('<tr id="row'+i+'"><td>
<select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select> </td>
<td><label>Quantity</label>
<input type="text" name="tquantity[]" value=""></td>
<td><label>Price</label>
<input type="text" name="saleprice[]" value=""></td>
<td><button name="remove" id="remove" class="btn btn-danger btn_remove">Remove</button></td></tr>');
});
$(document).on('click','.btn_remove',funcion(){
var button_id= $(this).attr("id");
$("#row"+button_id+"").remove();
});
$(#submit).click(function(){
$.ajax({
url:"processinvoice.php",
method:"POST",
data:$('#order').serialize(),
success:function(data){
alert(data);
$('#order')[0].reset();
}
});
});
}
</script>
So I'm trying to add input fields dynamically using jQuery ajax following a tutorial but i can't seem to make it work
When I click add to add fields only the link changes to /admin/addinvoice.php?invoicenumber=&vendorid=+2+&proid[]=+10+&tquantity[]=&saleprice[]=&add=
and it's not adding a field

how to display contents from another table (codeigniter)?

I want to do crud on my page and I have to get content from another table and display it in a select tag. my plan was to make an update page wherein it will get the data from another table to display (in this case the category_name, cat_id was the foreign key at the menu table).
I already tried to join because that's what other forums have said but I think I was wrong.
help would really be appreciated.
this is my model:
function inputMenuToUpdate($menu_id){
$this->db->select('cat_id, category.category_id, category.category_name AS category_name');
$this->db->from('menu');
$this->db->join('category', 'menu.cat_id = category.category_id');
$this->db->where('menu_id',$menu_id);
return $query->row();
}
this is my controller:
public function inputToUpdate($menu_id){
$data['row'] = $this->adminMenuModel->inputMenuToUpdate($menu_id);
$this->load->view('adminEdit', $data);
}
this is my view:
<tbody>
<!-- $result is from the $data in CrudController-->
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row->menu_name; ?></td>
<td><?php echo $row->category_name; ?></td>
<td><?php echo $row->price; ?></td>
<td><?php echo $row->description; ?> </td>
<td>
<i class="fa fa-pencil"></i>
</td>
</tr>
<?php } ?>
</tbody>
and this is my update page:
<div class="container" style="width: 40rem;">
<form method="POST" action="<?php echo site_url('adminMenuController/update'); ?> / <?php echo $row->category_id; ?>">
<button type="button" class="btn btn-link"> Go Back </button>
<h1> UPDATE MENU </h1>
<div class="border-bottom"></div>
<br>
<br>
<label for="upMName">New Drink Name</label>
<input type="text" name="mn" class="form-control" id="upMName" value="<?php echo $row->menu_name ?>"></input>
<!-- <label for="cat"> Category </label>
<select id="cat" class="form-control">
<option> <?php echo $row->category_name; ?> </option>
</select> -->
<label for="upPrice">New Price</label>
<input type="number" name="price" class="form-control" id="upPrice" value="<?php echo $row->price ?>"> </input>
<label for="updesc">New Description </label>
<input type="text" name="desc" class="form-control" id="updesc" value="<?php echo $row->description ?>"> </input>
<br>
<button type="submit" class="btn btn-info"> Update </button>
<button type="reset" class="btn btn-danger"> Cancel </button>
</form>
</div>

how to sent data in multiple search options

Hi guys i coded a search php in custom page for my site and want do search job in same page (i don't want send data to action page).
but i have problem i don't know why this is not work pleas can some one tell me what's problem and what is the current one?!
Thank you.
<div class="row-fluid">
<div class="page-header">
<h1>Search Users: </h1>
</div>
<center>
<h2>Find user(s)</h2>
<form name="search" method="post" action="">
Seach for: <input type="text" name="find" /> with
<Select NAME="field">
<Option VALUE="user_id">User ID</option>
<Option VALUE="username">Username</option>
<Option VALUE="serialnumber">SerialNumber</option>
<Option VALUE="ip_address">IP Address</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" class="btn btn-success" style="vertical-align: super;" />
</form>
<span><?php echo $error; ?></span>
</center>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>E-mail</th>
<th>IP</th>
<th>serialnumber</th>
<th>Actions</th>
</tr>
</thead>
<?php
if (isset($_POST['submit'])) {
if (empty($_POST['find']) || empty($_POST['field'])) {
$error = "Pleas choice options";
}
else
{
$find=$_POST['find'];
$value=$_POST['field'];
$sql = "SELECT * FROM login_log WHERE '$value' = '$find'";
$result = $db->sql_query($sql);
while($row = $db->sql_fetchrow($result)) {
?>
<tbody>
<tr class="pending-user">
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['ip_address']; ?></td>
<td><?php echo $row['serialnumber']; ?></td>
<td>Ban User</td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
</div>
$_POST will contain elements from the form where you use the name= attribute for your form fields. $_POST['submit'] won't exist unless you have name="submit" for one of the fields or buttons. Do a var_dump($_POST) to see what values are being sent back to the form.
Change isset($_POST['submit']) to isset($_POST['search']
See a tutorial on how to use forms.

Codeigniter: Create a dynamic ID

I just want to ask on how can I create a dynamic ID in the fields that I have loaded.
Here is my view:
<div id="content">
<form action="" method="POST">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" onclick="location.reload();"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
</div>
<div id="div1">
<fieldset>
<legend><?php // echo $tablename; ?></legend>
<table id="table_main">
<tr>
<th>Column Name:</th>
<th>Type:</th>
<th>Length/Values:</th>
</tr>
<?php foreach($updatemaintenance1 as $field) { ?>
<tr>
<td><input type="text" value="<?php echo $field->name; ?>" style="width: 80%"/></td>
<td>
<select style="width: 80%">
<option><?php echo $field->type; ?></option>
<option>INT</option>
<option>VARCHAR</option>
<option>TEXT</option>
<option>DATE</option>
</select>
</td>
<td><input type="text" value="<?php echo $field->max_length; ?>" style="width: 100%"/></td>
</tr>
<?php } ?>
<tr>
<?php //foreach ($tablename as $row1) { ?>
<td><input type="text" id="ninja79" name="ninja79" value="<?php echo $table_name1; ?>"/></td>
<?php// } ?>
</tr>
</table>
<div class="modal-footer">
<button type="button" class="btn_editinventorytype btn-info btn-sm">Update</button>
<button type="button" class="btn_deleteinventorytype btn-danger btn-sm">Delete</button>
</div>
</fieldset>
</div>
</form>
</div>
As you can see I have a foreach loop where I placed all the values from my database. What I want is to generate an ID on the textboxes and the selectbox. And I have this code in the model where I can modify the loaded fields.
Here is my model:
public function modify_table($modify){
$fields = array($modify[''] = array('name' => $modify[''],
'type' => $modify[''],
'constraint' => $modify['']),
);
$this->dbforge->modify_column($modify['ninja79'], $fields);
}
This codes are not as simple as 'SELECT * FROM 'tablename' I mean I'm talking about modifying tables in the database, so please could you help me? All I want is to have a dynamic ID in my model and in my view. I have no idea on how to code that. This is confusing so PLEASE I need your help. Thank you in advance!

Categories