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/
Related
i've searched and searched and maybe i found actually the right post but nothing worked for me so sorry for any duplication but maybe someone can help me out.
I have a php that gets me a form with multiple rows (based on an form input before).
Here's the php that creates the input table:
<html>
<head></head>
<body>
New report with EEM Number:<input type="text" name="eemnumber" />
<tr>
<td>Expense Date</td>
<td>Category</td>
<td>Vendor</td>
<td>Receipt Amount</td>
<td>Currency</td>
<td>Payment Amount</td>
<td>Currency</td>
<td>Payment Type</td>
<td>Country</td>
</tr>
<?php
$rows = $_POST['rows'];
$n = 0;
while($n < $rows)
{
$n++;
?>
<tr>
<td><input type="date" name="expensedate"/><br /></td>
<td><select name="category">
<option value="AirFare">AirFare</option> />
<option value="AutoRental">Auto Rental - Employee</option> />
<option value="DailyAllowance">Daily Allowance Standard</option> />
<option value="Gasoline">Gasoline - Auto Rental</option> />
<option value="Lodging">Lodging (Hotel) - Room / Tax</option> />
<option value="Meals">Meals / Entertain - HP only, Travel</option> />
<option value="Misc">Miscellaneous</option> />
<option value="Taxi">Taxi / Subway / Bus / Train</option> />
<br/>
</td>
<td><input type="text" name="vendor"/><br /></td>
<td><input type="text" name="receiptamount"/>
</td>
<td><select name="currrec">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><input type="text" name="paymentamount"/>
</td>
<td><select name="currpay">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><select name="paytype">
<option value="cash">Cash</option> />
<option value="cc">Credit Card</option> />
<br/>
</td>
<td><select name="country">
<option value="DE">Germany</option> />
<option value="SE">Sweden</option> />
<option value="US">USA</option> />
<br/>
</td>
</tr>
<?PHP
}
?>
</table>
</div>
<p>
<input type="submit"/>
</form>
<!-- End of FORM -->
Back
</body>
</html>
So once all data was entered I click on Submit and would of course like to get every line into my DB. Here's the script i tried do use but only gets me the last...
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$eemnumber=$_POST['expensedate'];
$expensedate=$_POST['expensedate'];
$category=$_POST['category'];
$vendor=$_POST['vendor'];
$receiptamount=$_POST['receiptamount'];
$currrec=$_POST['currrec'];
$paymentamount=$_POST['paymentamount'];
$currpay=$_POST['currpay'];
$paytype=$_POST['paytype'];
$eemnumber=$_POST['eemnumber'];
$country=$_POST['country'];
$sql="INSERT INTO tbl_eem_data
(ExpenseDate, Category, Vendor, ReceiptAmount, CurrRec, PaymentAmount, CurrPay, PayType, EEMNumber, Country)
VALUES
$expensedate,
$category,
$vendor,
$receiptamount,
$currrec,
$paymentamount,
$currpay,
$paytype,
$eemnumber,
$country)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header("location:index.php");
?>
Anyone any clue?
Thanks!!!
You need to change form Structure and form Fields
<?php
$rows = $_POST['rows'];
$n = 0;
while($n < $rows)
{
$n++;
?>
<tr>
<td><input type="date" name="expensedate[<?echo $n?>]"/><br /></td>
<td><select name="category">
<option value="AirFare">AirFare</option> />
<option value="AutoRental">Auto Rental - Employee</option> />
<option value="DailyAllowance">Daily Allowance Standard</option> />
<option value="Gasoline">Gasoline - Auto Rental</option> />
<option value="Lodging">Lodging (Hotel) - Room / Tax</option> />
<option value="Meals">Meals / Entertain - HP only, Travel</option> />
<option value="Misc">Miscellaneous</option> />
<option value="Taxi">Taxi / Subway / Bus / Train</option> />
<br/>
</td>
<td><input type="text" name="vendor[<?echo $n?>]"/><br /></td>
<td><input type="text" name="receiptamount[<?echo $n?>]"/>
</td>
<td><select name="currrec">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><input type="text" name="paymentamount[<?echo $n?>]"/>
</td>
<td><select name="currpay[<?echo $n?>]">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><select name="paytype[<?echo $n?>]">
<option value="cash">Cash</option> />
<option value="cc">Credit Card</option> />
<br/>
</td>
<td><select name="country[<?echo $n?>]">
<option value="DE">Germany</option> />
<option value="SE">Sweden</option> />
<option value="US">USA</option> />
<br/>
</td>
</tr>
<?PHP
}
?>
<input type="hidden" name="rows" value="<?echo $n?>" />
Then apply similar loop to fetch those values.
You also need to have hidden variable which stores total number of rows you have.
Thanks
You seem to be missing an open parenthesis ( in your SQL right after VALUES.
Fix it and hopefully it will work: VALUES ( . . . )
Basically, run this test:
HTML:
<input type="text" name="key" value="1" />
<input type="text" name="key" value="2" />
PHP:
<?
print_r($_POST);
?>
And then run this:
HTML:
<input type="text" name="key[]" value="1" />
<input type="text" name="key[]" value="2" />
PHP:
<?
print_r($_POST);
?>
See if you can make any conclusions with this, and let me know what you think.
You are generating form fields based on the number of $rows received from the Request.
<form ... <!- this line was missing in your code -->
$rows = $_POST['rows'];
while($n < $rows)
...
</form>
And after submit, you are not reading parameters into arrays.
$eemnumber=$_POST['expensedate']; /* this is wrong again */
$expensedate=$_POST['expensedate'];
$category=$_POST['category'];
$vendor=$_POST['vendor'];
Which lead the last value from the submitted array values assigned to the variable in concern.
And hence you see that only last row read and pushed into DB.
Change your code accordingly and it should be working.
Refer to:
PHP: Reading POST data with arrays
My Selectbox Items from aircraft table
<select name="selectaircraft">
<option id="0">-- Select Aircraft -- </option>
<?php
require("dbc.php");
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
?>
<option id="<?php echo $viewallaircraft['ID']; ?>">
<?php echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>
hi guys im trying to use the the value of selectaircraft to search and display the result to textbox.. here is my code so far
<?php
require("dbc.php");
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE 'PR200'");
while($viewallconfig= mysql_fetch_array($getallconfig)){
?>
<input type="text" name="aconfig "value="<?php echo $viewallconfig['config']; ?>" />
<?php } ?>
As you can see, I only put PR200 to search, my question is how can i use the select value instead? I also want the search query to happen everytime i click the select box.
guys can some1 help me? i know how to code this in VB6 it will be like this
assume : select name or in VB6 combobox = selectbox
Private Sub Combo3_Click()
adodc1.recorsource ="Select * from aircrafttable where aircraft = " & selectbox & "'"
adodc1.refresh
Text1.Text = Adodc1.Recordset("aircraft")
everytime i click combo/selectbox the query will perform and give different answer to textbox.
i dont know how to do this in PHP pls help!
Edited Added all codes
<form action="addrecord.php" method="post" >
<table border=1>
<tr><td bgcolor="#999999">
<strong>Area:</strong> </td>
<td>
<input type="text" required="required" name="aarea" size=10 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Aircraft:</strong></td>
<td>
<form method="post" action="aircraft.php">
<select name="selectaircraft" onchange="this.form.submit();">
<option id="0">Aircraft</option>
<?php
require("dbc.php");
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
?>
<option id="<?php echo $viewallaircraft['ID']; ?>">
<?php
echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>
<?php
require("dbc.php");
if(isset($_POST)){
$takeaircraft = mysql_real_escape_string($_POST['selectaircraft']);
{
?>
<input type="text" name="aaircraft" size=3 value="<?php echo $takeaircraft; ?>" />
<?php }
}?>
</form>
</td></tr>
<tr><td bgcolor="#999999">
<strong>Flight:</strong> </td>
<td><input type="text" name="aflight" size=10 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Configuration:</strong> </td>
<td>
<?php
require("dbc.php");
if(isset($_POST)){
$selectaircraft = mysql_real_escape_string($_POST['selectaircraft']);
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '".$selectaircraft."'");
while($viewallconfig= mysql_fetch_array($getallconfig)){
?>
<input type="text" name="aconfig" value="<?php echo $viewallconfig['config']; ?>" />
<?php }
}?>
<br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Month:</strong> </td>
<td>
<select name="amonth">
<option value="na">Month</option>
<option value="January">January</option>
</select>
</td></tr>
<tr><td bgcolor="#999999">
<strong>Frequency:</strong> </td>
<td><span style="font-size:11px; font-weight:bolder" >
Mon<input type="checkbox" id='check1' onClick='checkmon()'>
<input type="hidden" id="txt1" name="hiddenmon" value="n/a">
Tue<input type="checkbox" id='check2' onClick='checktue()'>
<input type="hidden" id="txt2" name="hiddentue" value="n/a">
Wed<input type="checkbox" id='check3' onClick='checkwed()'>
<input type="hidden" id="txt3" name="hiddenwed" value="n/a">
Thu<input type="checkbox" id='check4' onClick='checkthu()'>
<input type="hidden" id="txt4" name="hiddenthu" value="n/a">
Fri<input type="checkbox" id='check5' onClick='checkfri()'>
<input type="hidden" id="txt5" name="hiddenfri" value="n/a">
Sat<input type="checkbox" id='check6' onClick='checksat()'>
<input type="hidden" id="txt6" name="hiddensat" value="n/a">
Sun<input type="checkbox" id='check7' onClick='checksun()'>
<input type="hidden" id="txt7" name="hiddensun" value="n/a">
</span>
<br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Menu:</strong> </td>
<td><input type="text" name="amenu" size=10 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Cycle:</strong> </td>
<td>
<select name="acycle">
<option value="na">Cycle</option>
<option value="Cycle 1">Cycle 1</option>
</select>
<tr><td bgcolor="#999999">
<strong>Items:</strong> </td>
<td>
<select name="aitem">
<option value="na">Items</option>
</select>
<tr><td bgcolor="#999999">
<strong>STD Time:</strong> </td>
<td><input type="text" name="astdtime" size=5 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Quantity:</strong> </td>
<td><input type="text" name="aqty" size=5 /><br /></td></tr>
<tr>
<td colspan="2" align="right" bgcolor="#999999">
<input type='submit' name="add" value="Add Records" />
</td>
</tr>
</table>
</form>
trigger submit on change of select box as below
<form action="path/to/your/query/file.php" method="post"> //if query in same page leave your action blank
<select name="selectaircraft" onchange="this.form.submi();">
<option id="0">-- Select Aircraft -- </option>
<?php
require("dbc.php");
$selected_aircraft=isset($_POST['selectaircraft'])?$_POST['selectaircraft']:"";
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
$selectstr="";
if($selected_aircraft==$viewallaircraft['aircraft'] ){
$selectstr="selected='selected'";
}
?>
<option id="<?php echo $viewallaircraft['ID']; ?>" <?php echo $selectstr ?> >
<?php
echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>
</form>
and change you query as below:
<?php
require("dbc.php");
if(isset($_POST)){
$selectaircraft = mysql_real_escape_string($_POST['selectaircraft']);
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '{ $selectaircraft}'");
while($viewallconfig= mysql_fetch_array($getallconfig)){
?>
<input type="text" name="aconfig "value="<?php echo $viewallconfig['config']; ?>" />
<?php }
}?>
Update updated code to maintain state of select box.
Try like this;
$aircraft = mysql_real_escape_string($_POST['selectaircraft']); //or $_GET
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '".$aircraft."'");
You can keep the select box(1st) inside a form , on change submit the form to the next page .
And get the POST/GET value , and use it for select query for searching.
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>
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>
I am working on a certain form that accepts multiple values using checkbox and I find it hard to get all the values of the checked boxes and store them into the database.
Everytime my insertion query is being executed.. the only value that is stored in the database is the last checkbox the user selects thus ignoring the others..
here's my code:
----form-----------------------------------------------------------------'
<form name="addRes" method="post" action="addReservation_code.php">
<table>
<tr><td>Activity Date:</td>
<td><select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value"December">December</option>
</select>
<select name="day">
<?php for($ctr=1;$ctr<=31;$ctr++){ ?>
<option value="<?php print($ctr); ?>"><?php print($ctr); ?></option>
<?php } ?>
</select> -
<select name="day2">
<?php for($ctr=1;$ctr<=31;$ctr++){ ?>
<option value="<?php print($ctr); ?>"><?php print($ctr); ?></option>
<?php } ?>
</select>
<input type="text" name="year"/></td>
</tr>
<tr><td>Activity Name:</td><td><input type="text" name="act_name" /></td></tr>
<tr><td>Activity Time:</td><td><input type="text" name="act_time" /></td></tr>
<tr><td>Room:</td>
<td>
<select name="room">
<?php
include 'RRSdbconnect.php';
$query = "SELECT room_name from room";
$result=mysql_query($query) or die('Invalid query'.mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
echo "<option>$row[room_name]";
}
?>
</option>
</select>
</td>
</tr>
<tr>
<td>Resources Needed:</td>
<td><input type="checkbox" name="resources" value="cpu" />CPU<br />
<input type="checkbox" name="resources" value="lcd" />LCD<br />
<input type="checkbox" name="resources" value="sounds" />Sounds<br />
<input type="checkbox" name="resources" value="sounds" />Others, Pls. specify<input type="text" name="others" /></td>
</tr>
<tr><td>No. of Person</td><td><input type="text" name="noOfPerson"/></td></tr>
<tr><td>Reserved by:</td><td><input type="text" name="reservedby" /></td></tr>
<tr><td></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"/><input type="reset" name="clear" value="Clear"/></td></tr>
</table>
</form>
------------action code----------------------------------------------------
<?php
include 'RRSdbconnect.php';
session_start();
$date = $_POST['month'] . ' ' .$_POST['day']. '-' .$_POST['day2']. ', ' . $_POST['year'] ;
$name=$_POST['act_name'];
$time=$_POST['act_time'];
$room=$_POST['room'];
$resources=$_POST['resources'];
$noOfPerson=$_POST['noOfPerson'];
$reservedby=$_POST['reservedby'];
//insertions for add reservations
$query="insert into reservation (activity_date, activity_name, activity_time, room, resources_needed, no_person, reserved_by) values('$date','$name','$time','$room','$resources','$noOfPerson','$reservedby')";
$result=mysql_query($query,$link) or die("Error!".mysql_error());
?>
PS.
I am using checkbox on the column resources needed..
any help will be appreciate..tnx
If you change the name attribute of the checkbox to name="resources[]" you should get an array in the $_POST['resources']
Once you have the array in $_POST['resources']you can store/retrieve it in the database how you like. For example either as a string using implode() / explode() or serialize() / unserialize().