I am new in php, I just starting learn php and have some problems.
In admin panel I have some form for adding info, after that in output I have radio buttons for selecting which row show into website. The problem is in update function the query is ok but it doesn't work, and I need you help.
thanks.
submit if.
if(isset($_POST['update_info'])){
$selected_info=$_POST['selected'];
$selected_id = $_POST['id'];
updateHomeInfo($selected_info, $selected_id);
}
output added rows.
<div class="home-output">
<form action="/admin/" method="post">
<table class="table table-hover">
<thead>
<tr>
<td>#</td>
<td>slected</td>
<td>title</td>
<td>descriptiom</td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<?php foreach($homeInfo as $row) { ?>
<tr>
<td><?php echo $row['id']; ?><input type="hidden" name="id" value="<?php echo $row['id']; ?>"></td>
<td><input type="radio" name="selected" value="home-info" <?php if($row['selected'] == 1) echo "checked"; ?> /></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" class="btn btn-default" name="clear_info" value="delete all">
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<input type="submit" class="btn btn-default" name="update_info" value="update">
</td>
</tr>
</tbody>
</table>
</form>
</div>
and update function.
function updateHomeInfo($selected_info, $selected_id) {
global $db;
$db->exec("UPDATE home SET selected = '$selected_info' where id = '$selected_id'");
}
All your hidden inputs have the same name; they'll all be submitted, and the value of $_POST['id'] will just be the last one, not the one next to the selected radio button. And all the radio buttons have the same value as well.
You should put the ID into the value of the radio button. You only need one copy of the hidden input, and it can contain the selected info.
<form action="/admin/" method="post">
<input type="hidden" name="selected" value="home-info">
<table class="table table-hover">
...
<td><?php echo $row['id']; ?>
<td><input type="radio" name="id" value="<?php echo $row['id']; ?>" <?php if($row['selected'] == 1) echo "checked"; ?> /></td>
...
Actually, it seems like you don't need the selected input at all, if selected is a boolean. It should just be:
function updateHomeInfo($selected_id) {
$db->exec("UPDATE home SET selected = (id = '$selected_id')");
}
This will set selected to 1 for the selected ID, and 0 for all the other IDs.
Related
i want to make a cart and my product is pizza.
so i'm listing all the menu to the table, and add a button to submit menu to cart.
here's my index.php
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<form action="cart.php" method="get">
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
<?php } ?>
</table>
</form>
But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php
can anyone help me how to get the right quantity value when i pressed the button add to cart.
Make separate form for each of the item. Try below code.
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<form action="cart.php" method="get">
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
</form>
<?php } ?>
</table>
Try to use array in name then submit it will give you seperate quantity.
<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">
Output:
quantity['pizza1'] = 10
quantity['pizza2'] = 20
....
Another Option is use dynamic name for number.
<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">
Output:
quantity_pizza1 = 10
quantity_pizza2 = 20
....
I am creating a list of users that I would like to be able to either approve, deny, or delete. I don't want to do one by one, because it would be too much work.
I have the following code for the approve, deny, and delete columns:
<tr>
<input type="hidden" name="user_id" value="'.$row['user_id'].'">
<td style="text-align:center; padding:20px; background-color:#DFF0D8;">
<input type="radio" name="approve[]" value="1">
</td>
<td style="text-align:center; padding:20px; background-color:#FCF8E3;">
<input type="radio" name="approve[]" value="0">
</td>
<td style="text-align:center; padding:20px; background-color:#FCDEDE;">
<input type="radio" name="approve[]" value="3">
</td>
</tr>
I want to be able to set the "approved" column in the table "users" in MySQL to 1 if it is approved and to keep it to 0 if it is denied. If "delete" is chosen, then I want to be able to delete that row of data. How can I do this for a list of users? If it was just one by one it would be easy, but I don't know how to do it for multiple users.
I thought of looping through the "approve" array, but I can't change the values in the database because I don't know how to match this to a user_id.
Any help is appreciated.
here is a sample for multi delete:
index.php
<?php include('dbcon.php'); ?>
<form method="post" action="delete.php" >
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<div class="alert alert-info">
<strong>Check the radion Button and click the Delete button to Delete Data</strong>
</div><thead>
<tr>
<th></th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Address</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from member")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['member_id'];
?>
<tr>
<td>
<input name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['firstname'] ?></td>
<td><?php echo $row['lastname'] ?></td>
<td><?php echo $row['middlename'] ?></td>
<td><?php echo $row['address'] ?></td>
<td><?php echo $row['email'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" class="btn btn-danger" value="Delete" name="delete">
delete.php
<?php
include('dbcon.php');
if (isset($_POST['delete'])){
$id=$_POST['selector'];
$N = count($id);
for($i=0; $i < $N; $i++){
$result = mysql_query("DELETE FROM member where member_id='$id[$i]'");}
header("location: index.php"); } ?>
For multiple users you just need to add a dimension to all your form variable names:
<tr>
<td style="text-align:center; padding:20px; background-color:#DFF0D8;">
<input type="radio" name="approve[<?= $row['user_id'] ?>]" value="1">
</td>
<td style="text-align:center; padding:20px; background-color:#FCF8E3;">
<input type="radio" name="approve[<?= $row['user_id'] ?>]" value="0">
</td>
<td style="text-align:center; padding:20px; background-color:#FCDEDE;">
<input type="radio" name="approve[<?= $row['user_id'] ?>]" value="3">
</td>
</tr>
When the form is posted you will receive something like this:
approve[123]=1&approve[456]=2&...
This will turn $_POST['approve'] into an array:
array(
123 => 1
456 => 2
);
...
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.
how to save radio button has been selected at the time the page is reloaded? suppose I have selected some of the radio button and then reload the browser page. but after the page is reloaded radio buttons have been still the same. I use Codeigniter.
here is my view code
<div class="container">
<?php
$no=1;
foreach($hasil->result() as $row):
?>
<form action="<?php echo base_url();?>mahasiswa/hasil" method="post" class="form" enctype="multipart/form-data" name="form" onsubmit="stopCounter();">
<input type="hidden" name="id_soal[<?php echo $row->id_soal;?>]" value="<?php echo $row->id_soal;?>" />
<table>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><b><?php echo $no.'. ';?></b></td>
<td><?php echo $row->pertanyaan;?></td>
</tr>
<tr>
<td rowspan="5"> </td>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="A" />a. <?php echo $row->a;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="B" />b. <?php echo $row->b;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="C" />c. <?php echo $row->c;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="D" />d. <?php echo $row->d;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="E"/>e. <?php echo $row->e;?></label></span></td>
</tr>
<?php
$no++;
?>
<input type="hidden" name="id_sesi" value="<?php echo $row->id_sesi;?>" />
<input type="hidden" name="jumlah" value="<?php echo $jumlah;?>" />
<?php
endforeach;
?>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Selesai" class="btn btn-default"/></td>
</tr>
</table>
</form>
</div>
here my controller code
public function mulai_tes(){
$id=$this->uri->segment(3);
$this->load->model('m_mahasiswa');
$result=$this->m_mahasiswa->data_mahasiswa();
foreach($result->result() as $row):
$data['user']=$row->username;
$data['nama']=$row->nama;
endforeach;
$cek=$this->m_mahasiswa->validasi_tes($id,$data['user']);
foreach($cek->result() as $c){
if($c->id_sesi==$id){ ?>
<script type="text/javascript" language="javascript">
alert("Anda telah mengikuti tes soal ini");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."mahasiswa/tes'>";
}
}
$data['hasil']=$this->m_mahasiswa->mulaites($id);
$data['jumlah']=$data['hasil']->num_rows();
$data['judul']='Mulai Tes';
$this->load->view('elearning/template',$data);
}
How I set cookie and where I can put cookie code in controller?
Check which radio(s) was selected in controler (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html).
Pass this data to view (http://ellislab.com/codeigniter/user-guide/general/views.html).
Mark radio(s) as selected within view depending on data passed by controller (Assign an initial value to radio button as checked).
Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>