I have the following code to create a drop-down list:
<body>
<form method="post">
<table>
<tr>
<td>Firm Name:</td>
<td><input type="text"class="input_text_long" name="name" value="<?php echo $name ?>"/></td>
</tr>
<tr>
<td>Hub Name:</td>
<select>
<option value="">---Select---</option>
<?php
$list=mysql_query("SELECT * FROM hub");
while($row = mysql_fetch_assoc($list)) {
?>
<option value=<?php echo $row_list['name'];?>
</option>
<?php } ?>
</select>
</tr>
<td> </td>
<td><input type="submit" name="save" value="Save" /></td>
</tr>
</table>
</form>
</body>
This, however, does not display any dropdown list. It only displays a text box. Can anyone tell me what I am doing wrong, please? Or how to create a dropdown box in a form.
Your option tag is wrong.
Right syntax <option value="VALUE">OPTION NAME</option>
<select>
<option value="">---Select---</option>
<?php
$list = mysql_query("SELECT * FROM hub");
while ($row = mysql_fetch_assoc($list)) {
$name = $row['name'];
?>
<option value="<?php echo $name; ?>"><?php echo $name; ?></option>
<?php
}
?>
</select>
Change this line
<option value=<?php echo $row_list['name'];?></option>
to
<option value="<?php echo $row['name'];?>"><?php echo $row['name'];?></option>
I think the issue is with this line:
<?php echo $row_list['name'];?>
It should be:
<?php echo $row['name'];?>
Also, your opening <option> tag does not have a closing >
<option value=<?php echo $row['name'];?>></option>
Related
i need to add data to database from array(table). but this POST.php is not working properly. so can you guys please give me any solution for this.
this is the error that it give.
Illegal string offset 'h_name' in C:\wamp\www\confirm\post.php on line 11
line 11 is: VALUES ('$row[h_name]', '$row[room]', '$row[nors]', '$row[nights]', '$row[euro]', '$row[date]', '09090')";
all inserted value give that error. Array to string conversion
Thank you.
</br>
<h4 id="italic">Hotel Details :</h4>
</br>
<div class="reqtable">
<table>
<tr ><td>Hotel Name</td><td>Room Type</td><td>Number of Rooms</td><td>Nights</td><td>EURO</td><td>Date</td></tr>
<tr><td><?php
include "conn.php";
$query = "SELECT h_id, h_name FROM hotels";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn)); // Run your query
echo '<select name="list[h_name]" id="h_name" ">';
echo '<option value=""> Choose a Hotel </option>';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['h_id'].'">'.$row['h_name'].'</option>';
}
echo '</select>';/
?>
</td>
<td>
<?php echo '
<select name="list[room]" id="room" >
<option value="">Choose a Room Type</option>
<option value="1">Single Room</option>
<option value="2">Double Room</option>
<option value="3">Triple Room</option>
<option value="4">Family Room</option>
<option value="5">Custom Room</option>
</select>';
?>
</td>
<td><input type="text" name="list[nors]" placeholder="Number of Rooms"></td>
<td> <input type="text" class="zxc" nname="list[nights]" placeholder="Nights"></td>
<td><input type="text" name="list[euro]" placeholder="euro"></td>
<td><input type="date" name="list[date]" placeholder="Date"></td>
<td><pre> </pre></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
<tr><td>Total</td><td></td><td></td><td><input name="result" id="result"></td><td></td><td></td></tr>
</table>
</div>
</br>
<input type="submit" id="submit" name="submit" value="Register" color="red" style="width: 77px; height: 50px"></div>
</form>
post.php(php process)
<?php
include "conn.php";
print_r($_POST['list']);
foreach ($_POST as $row) {
$query = "INSERT INTO reqhotels (reqh_h_name, reqh_rtype, reqh_nor, reqh_nights, reqh_euro, reqh_date, reqh_req_no)
VALUES ('$row[h_name]', '$row[room]', '$row[nors]', '$row[nights]', '$row[euro]', '$row[date]', '09090')";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
if($result > 0) {
echo"successfull";
}
else {
echo"fail";
}
}
?>
EDIT : ported from answer to question : Aug.05
this is my html codes. (html table). in here used a jquery to add some extra rows to this table. so i want to insert all data to database.
table columns:
rq_h_name, rq_rtype, rq_nors, rq_nights, rq_euro, rq_date
<form action="post.php" method="POST" id="register-form" novalidate="novalidate">
</br>
<h4 id="italic">Hotel Details :</h4>
</br>
<div class="reqtable">
<table>
<tr >
<td>Hotel Name</td>
<td>Room Type</td>
<td>Number of Rooms</td>
<td>Nights</td>
<td>EURO</td>
<td>Date</td>
</tr>
<tr>
<td>
<?php
include "conn.php";
$query = "SELECT h_id, h_name FROM hotels";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn)); // Run your query
echo '<select name="list[0][]" id="h_name" ">';
echo '<option value=""> Choose a Hotel </option>';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['h_id'].'">'.$row['h_name'].'</option>';
}
echo '</select>';
?>
</td>
<td>
<?php echo '
<select name="list[0][]" id="room" >
<option value="">Choose a Room Type</option>
<option value="1">Single Room</option>
<option value="2">Double Room</option>
<option value="3">Triple Room</option>
<option value="4">Family Room</option>
<option value="5">Custom Room</option>
</select>';
?>
</td>
<td><input type="text" name="list[0][]" placeholder="Number of Rooms"></td>
<td> <input type="text" class="zxc" name="list[0][]" placeholder="Nights"></td>
<td><input type="text" name="list[0][]" placeholder="euro"></td>
<td><input type="date" name="list[0][]" placeholder="Date"></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
<tr>
<td>Total</td>
<td><input name="result" id="result"></td>
</tr>
</table>
</div>
</br>
probably you want to do the following
foreach ($_POST['list'] as $row) {
or similar instead of current
foreach ($_POST as $row) {
In your current manner you have an array in array so and the array could not be substituted directly to a string. I'm not sure this will exactly work, but that's the idea you shoud follow and shows the problem you have. Your code is a bit messy so I'won't guarantee you have no other errors.
I have the following code in the view of an application I am developing using the codeigniter framework
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></form></td>
</tr>
<?php } ?>
Have made edit to the code based on the answers below now it is like this
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close();} ?>
But the output is still the same
However when the view is loaded, the code comes out like this
<tr>
<form action="http://sms-dev.anovatesoft.com/index.php/MainController/AttendanceSet" method="post" accept-charset="utf-8"></form>
<input type="hidden" name="child_nric" value="A12">
<input type="hidden" name="teachernric" value="T001">
<input type="hidden" name="progcode" value="FEE001">
<td>A12</td>
<td>
<select name="attendance">
<option value="1" selected="">Present</option>
<option value="0" selected="">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
Notice the difference in the </from> position? In my code I have it after the submit button but when it renders it closes just after the <form> opens making the form useless as I am not able to submit it.
What is the reason behind this strange behavior? And how do I fix it?
The View
<?php
$this->load->helper('form');
?>
<script>
function ListView(){
//if($("#id").val() != "0"){
var formURL = "<?php echo base_url();?>MainController/Attendance/"+$("#program").val();
$.post(formURL).done(function(data){$("#body_view_paste").html(data); });
}
function fire(){
alert("I have been fired");
}
</script>
<div align="center">
<table align="center" style="max-width:80%">
<tr>
<td>Program</td>
<td>
<select id="program" name="program" onchange="ListView()">
<option value="0">Select</option>
<?php
$nricno = $_SESSION['username'];
$sql = "SELECT distinct programs.activities, programs.progcode FROM events
LEFT JOIN programs ON programs.progcode=events.progcode WHERE events.teacher_nric='$nricno'";
$activities = $this->db->query($sql);
foreach($activities->result_array() as $row){?>
<option value="<?php echo $row['progcode'];?>"><?php echo $row['activities'];?></option>
<?php } ?>
</select>
</td>
</tr>
</table>
</div>
<div>
<?php if(isset($program)){
$selected=3;
$query = $this->db->query("SELECT child_nric FROM child_reg_prog WHERE progcode = '$program'");
?>
<table>
<thead>
<tr>
<td>NRIC NO</td>
<td>Attendance</td>
<td>Action</td>
</tr>
</thead>
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close();} ?>
</table>
<?php } ?>
</div>
The Controller part
public function Attendance($program = "")
{
$this->main_model->pagePermissions("Attendance");
if ($program == "") {
$this->load->view('Attendance');
} else {
$data['program'] = $program;
$this->load->view('Attendance', $data);
}
}
public function AttendanceSet()
{
$child_nric = $_POST['child_nric'];
$attendance = $_POST['attendance'];
$teachernric = $_POST['teachernric'];
$progcode = $_POST['progcode'];
$ispresent="No";
if($attendance==1)
{
$ispresent="Yes";
}
$this->load->model("Attendance_model");
$this->Attendance_model->insert($progcode, $childnric, $teachernric, $ispresent);
$this->load->view('Attendance');
}
The model part
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Attendance_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function insert($progcode, $childnric, $teachernric, $ispresent)
{
$sql = "INSERT INTO attendence (progcode, childnric, teachernric, ispresent ) VALUES (?,?,?,?) ";
$query = $this->db->query($sql, array($progcode, $childnric, $teachernric, $ispresent));
}
}
?>
try this:
<?php foreach($query->result_array() as $row){ ?>
<?php echo form_open('MainController/AttendanceSet');?>
<tr>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close(); ?>
<?php } ?>
Both of the below statements should come outside the foreach loop
<?php echo form_open('MainController/AttendanceSet');?>
<?php echo form_close(); ?>// you need to close it
i have two tables as shown, when employee edits its details department_id save as 0 in database
employee department
id| name|department_id id|department_name
1 |abc |1 1|HR
2 |xyz |4 2|Finance
3 |asd |3 3|Developer
and this view part
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php $employee['department_id'] ;?>"
<?php if ($row['id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
after edit 0 value get store in database
forget echo <option value="<?php $employee['department_id']; ?>"
<SELECT name="department_id" style="width:180px;" >
<?php foreach($departments as $row): ?>
<option value="<?php echo $row['department_id']; ?>"
<?php if ($row['department_name']== $stakehholder['department_id']) {
echo 'selected';
}
?>>
<?php echo $row['department_name']; ?>
</option>
<?php endforeach ?>
</SELECT>
updated line is in as below
<option value="<?php echo $employee['department_id'] ;?>
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $employee['department_id'] ;?>"
<?php if ($row['department_name']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
you have forgot to echo department_id in option value attribute and second you are comparing name to id which never match if id is int and name not.
try this updated code:
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $row['department_id'] ;?>"
<?php if ($row['department_id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
I need to make a edit / update function based from searching ticket number. When user type a ticket number he had, would appear a form that contains data from database based on ticket number that he had.I can see name value, ticket number value and date value from database but I can't see a clock time value in select tag.
<?php
$no = $_GET['ticket'];
$st = "SELECT * FROM event WHERE no='$no'";
$check = mysql_query($st,$connection) or die("Failed");
$c = mysql_fetch_array($check);
?>
<form name="form" method="POST" action="">
<table>
<tr>
<td>Reference Number</td>
<td> : </td>
<td>
<input type="text" name="no" value="<?php echo $c['no'] ;?>" disabled>
<input type="hidden" name="no" value="<?php echo $c['no'] ;?>">
</td>
</tr>
<tr>
<td>Name</td>
<td> : </td>
<td>
<input type="text" name="name" value="<?php echo $c['name'] ;?>" disabled>
<input type="text" name="name" value="<?php echo $c['name'] ;?>" disabled>
</td>
</tr>
<tr>
<td>Date</td>
<td> : </td>
<td>
<input type="text" id="date" name="date" value="<?php echo $c['date'] ;?>">
</td>
</tr>
<tr>
<td>Clock Time</td>
<td> : </td>
<td>
<select name="time" value="<?php echo $c['time'] ;?>">
</td>
</tr>
I want to see the clock time value on database in select tag. After that, if user want to make changes on clock time based on the date he chosen, he can do that thing.
Thanks before,
The stucture of drop down in htm is
<select name ="postname">
<option value="postvalue"> display value</option>
more options
.
.
.
.
</select>
use following code in your case
<select name="postname" >
<option value="<?php echo $c['date'] ;?>" > <?php echo $c['date'] ;?> </option>
</select>
select tags must have options. you need to popuate the select tag with options and then check it against your value to set the selected option.
this method is wrong <select name="time" value="<?php echo $c['date'] ;?>">
<select name="time">
<?php foreach($options as $option) { ?>
<?php $sel = ''; if ($option->value = $c['date']) { $sel = 'selected'; } ?>
<option value="<?php echo $option->value; ?>" <?php echo $sel; ?>><?php echo $option->name; ?></option>
<?php } ?>
</select>
put your select like this:-
<select name="time" >
<option value=value="<?php echo $c['date'] ;?>" selected > <?php echo $c['date'] ;?> </option>
</select>
Dear friends with this function I retrieve the values selected by my users and I put it in a hidden field to use it in my form:
<SCRIPT type="text/javascript"> function updateHidden(sel){
var f = document.contract;
f.sel_value.value = sel.options[sel.selectedIndex].value;
} </SCRIPT>
The problem is that my select is inside a while cycle and therefore repeated. The js function set this way reads only the value selected in the first select.
I need to add a loop in the function, but I don’t know where to put it.
This is my form:
<form action="update.php" method="POST" name="contract">
<table class="myp-table">
<tr>
<td class="head-act">Contract</td>
</tr>
<?php do { ?>
<tr>
<td class="head-act"><select name="playerContract[]" onchange="updateHidden(this)">
<option value="0" <?php if (!(strcmp(0, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>0</option>
<option value="1" <?php if (!(strcmp(1, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>1</option>
<option value="2" <?php if (!(strcmp(2, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>2</option>
<option value="3" <?php if (!(strcmp(3, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>3</option>
</select>
<input type="hidden" name="sel_value"></td>
<td><input name="id[]" type="hidden" value="<?php echo $row_datacontract['id']; ?>"/> </td>
</tr>
<?php } while ($row_datacontract = mysql_fetch_assoc($datacontract)); ?>
<tr class="zebra">
<td><input class="linkbuttonmp" name="contract" id="submit" type="submit" value="Invio" /></td>
</tr>
</table>
</form>
Thank you.
The problem is that you will have n number of <input type="hidden" name="sel_value">, so how does it know which name="sel_value" to change? It would be best to change that to either sel_value[] or unique names.
But, your function can be changed easily using .nextElementSibling
function updateHidden(sel){
sel.nextElementSibling.value = sel.options[sel.selectedIndex].value;
}
see this jsFiddle - http://jsfiddle.net/aFNdt/1/