I have this code to pull values and labels from a MySQL DB and populate a drop down box, on change it put the value in a text field, but I want the label not the value.
Any pointers would be good..
<select name="CompanyInternalID" autofocus class="textBox" id="CompanyInternalID" style="width:300px" onchange="document.form1.CompName.value=this.value">
<?php
do {
?>
<option value="<?php echo $row_rsCustomerList['AKA']?>"><?php echo $row_rsCustomerList['CustomerName']?></option>
<?php
} while ($row_rsCustomerList = mysql_fetch_assoc($rsCustomerList));
$rows = mysql_num_rows($rsCustomerList);
if($rows > 0) {
mysql_data_seek($rsCustomerList, 0);
$row_rsCustomerList = mysql_fetch_assoc($rsCustomerList);
}
?>
</select>
<input type="text" name="CompName" class="textBox" style="width:180px" id="CompName" />
Thanks
You can use jQuery to get the selected value and put in the required text field.
Suppose the id of dropdown is "drop" and id of text field is "txt_id" . Now you can use below code:
$("#drop").change(function () {
$("#txt_id").val($(this).val());
});
You can use below code:
You can also learn jQuery at http://www.w3schools.com/jquery/
<!doctype html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
</head>
<body>
<select name="CompanyInternalID" autofocus class="textBox" id="CompanyInternalID" style="width:300px" > <?php do { ?> <option value="<?php echo $row_rsCustomerList['AKA']?>"><?php echo $row_rsCustomerList['CustomerName']?></option> <?php } while ($row_rsCustomerList = mysql_fetch_assoc($rsCustomerList)); $rows = mysql_num_rows($rsCustomerList); if($rows > 0) { mysql_data_seek($rsCustomerList, 0); $row_rsCustomerList = mysql_fetch_assoc($rsCustomerList); } ?> </select> <input type="text" name="CompName" class="textBox" style="width:180px" id="CompName" />
<script>
$("#CompanyInternalID").change(function () { $("#CompName").val($(this).val());
}
);
</script>
</body>
</html>
Related
I have created a drop-down list that shows the members which have been added from the user. The script for this is this one:
<?php include('server.php'); ?>
<?php
$connect = mysqli_connect("localhost", "root", "", "geofence");
?>
<html>
<head>
<title>Add members</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br /><br />
<h2 align="center">Please, choose a family member</h2>
<br /><br />
<div class="form-group" align="center">
<form name="dropdown" action="" method="post">
<select name="choose" id="choose" width="150" style="width: 150px">
<option value="" selected disabled hidden>Choose Member</option>
<?php
$res=mysqli_query($connect,"select *
from member
where user_id=".$_SESSION['user_id']."");
while($row=mysqli_fetch_array($res)) {
?>
<option><?php echo $row["name"]; ?></option>
<?php
}
?>
</select>
</form>
<input type="button" name="ok" id="ok" class="btn btn-info" value="OK" />
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#ok').click(function(){
$(location).attr('href', 'http://localhost/Houston/index.php')
});
});
</script>
Now, what I'm trying to do is to create a session ($_SESSION['member_id']) which refers to the name that is selected from the list. I have tried several things so far but with no success. An example is this:
if (isset($_POST['choose'])) {
if (count($errors)==0) {
$query="SELECT * FROM member where name=".$_POST['choose']."";
$result=mysqli_query($db,$query);
if (mysqli_num_rows($result)==1) {
$row=mysqli_fetch_assoc( $result);
$member_id=$row['member_id'];
$_SESSION['member_id'] = $row['member_id'];
}
}
}
I need the $_SESSION['member_id'] so I can use it on another file and populate my database. What is missing?
The problem is in the option tag, you have to specify de value of each option in the select tag , like this:
<option value="<?php echo $row["member_id"];?>"> <?php echo $row["name"]; ?> </option>
So, next you just have to declare the $_SESSION for your option value
if (isset($_POST['choose'])) {
$_SESSION['member_id']=$_POST["choose"];
}
Okay so I have 1 html page setup with a HTML multiple Attribute from. This is how it is setup:
<!DOCTYPE html>
<html>
<body>
<form action = "otherpage.php" method= "post">
<select name = "cars[]" multiple="multiple" size="4">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button type = "submit" value= "Submit">Submit</button>
<button type = "reset" value= "Clear">Clear</button>
</form>
</body>
</html>
I have a second php page, lets call it "otherpage.php" and this is how it is setup:
<!DOCTYPE html>
<html>
<body>
<form action = "" method= "post">
Name: <input type = "text" name = "yourname" />
<button type = "submit" value= "Submit2">Submit</button>
<input type="hidden" name="f" value=<?php $cars= array(); $cars= $_POST['cars']; print_r($cars); ?> />
<?php
if(isset($_POST['f'])) {echo ($_POST['f']);}
?>
</body>
</html>
When I hit the first submit on the first html page the array prints out fine. Then when I hit the other submit again on "otherpage.php", the array no longer exists and I get an error. How can I get the cars array to stay forever no matter how many times I submit on otherpage.php?
The easiest way would be to store the data in a session variable.
You have to have session_start() at the top of every file you want to use it on.
Then when you get the $_POST values:
$_SESSION['post'] = $_POST;
After that, you can do what you want with it on any other file.
You need to implode the array to string and explode it again to get back your array or encode it to a json string and reencode it again either way will work
<?php
$cars = isset($_POST['cars']) ?
$_POST['cars'] : isset($_POST['f']) ? explode(',',$_POST['f']) : [];
?>
DOCTYPE html>
<html>
<body>
<form action = "" method= "post">
Name: <input type = "text" name = "yourname" />
<button type = "submit" value= "Submit2">Submit</button>
<input type="hidden" name="f" value="<?php echo implode(',', $cars)?>"/>
</body>
</html>
I know you already accepted an answer, but it would be petty to give up my tests without showing you my version too.
The echo '<pre>'... is just for displaying results. Notice the use of value="<?php echo print_r($cars, true); ?>".
<?php
if (isset($_POST['cars'])) {
$cars = $_POST['cars'];
} elseif (isset($_POST['f'])) {
$cars = $_POST['f'];
} else {
$cars = array();
}
echo '<pre>' . print_r($cars, true) . '</pre>';
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
Name: <input type="text" name="yourname" />
<?php
foreach ($cars as $car) {
echo '<br/>' . $car . '<input type="radio" name="car" value="' . $car . '" />';
}
?>
<button type="submit" value="Submit2">Submit</button>
<input type="hidden" name="f" value="<?php echo print_r($cars, true); ?>" />
</form>
</body>
</html>
Good luck.
EDIT 2: Using session
You don't need a hidden input anymore. I changed page names and a bit of html also.
Page cars_select.php:
<!DOCTYPE html>
<html>
<head>
<title>Cars selecting page</title>
</head>
<body>
<form action="cars_list.php" method= "post">
<select name = "cars[]" multiple="multiple" size="4">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button type="submit" id="selectSubmit" name="selectSubmit" value="Submit">
Cars selected. List them!
</button>
</form>
</body>
</html>
Page cars_list.php
<?php
session_start();
// Upon clicking on "selectSubmit" button.
if (isset($_POST['selectSubmit'])) {
// Set POST received cars list as session variable.
$_SESSION['cars'] = isset($_POST['cars']) ? $_POST['cars'] : array();
echo 'Called by CARS SELECT page.';
}
// Upon clicking on "listSubmit" button.
if (isset($_POST['listSubmit'])) {
echo 'Called by CARS LIST page.';
}
// Test display of SESSION array.
echo '<br/><br/>Session is: <pre>' . print_r($_SESSION, true) . '</pre>';
// Read cars list from session.
$cars = $_SESSION['cars'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Cars listing page</title>
</head>
<body>
<hr/>
<form action="" method="post">
Name: <input type="text" name="yourname" />
<?php
foreach ($cars as $car) {
echo '<br/>' . $car . '<input type="radio" name="car" value="' . $car . '" />';
}
?>
<button type="submit" id="listSubmit" name="listSubmit" value="Submit">Refresh page!</button>
</form>
</body>
</html>
I have populated a drop down from my MySQL Database successfully. I am now wanting to be able to extracte the value from this dropdown. When ever I extract the value from the dropdown it is only the position value of the value in the list.
Here is the working drop down below:
<?php
include_once("connection.php");
function fill_name($connect)
{
$output = '';
$sql = "SELECT name, id FROM notes";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
$output .= '<option value="'.$row["id"].'">'.$row["name"].'</option>';
}
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Patient Notes View</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</script>
</head>
<body>
<br /><br />
<div class="container">
<form action="dropdown_selection.php" method="post">
<select name="name" id="name">
<option value="">Show All Patients</option>
<?php echo fill_name($connect); ?>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
And here is the page it then refers to:
<?php
$patient_name = $_POST['name'];
echo $patient_name
?>
The output is either 1,2,3,4, but I am wanting it to output what is is selected in the dropdown.
Thnks for your help
You can either change the value of your elements, or remove them. If the element has no value its content will be sent as the value.
The content of this attribute represents the value to be submitted with the form, should this option be selected. If this attribute is omitted, the value is taken from the text content of the option element.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
So either:
$output .= '<option value="'.$row["name"].'">'.$row["name"].'</option>';
or
$output .= '<option>'.$row["name"].'</option>';
The output is number is correct as you set option value to be ID, not NAME -> <option value="'.$row["id"].'"> therefore you get IDs
I've tried using the method GET in my form, and $_GET in my PHP page, but it doesn't link both of them together. I want to print out the elements in the form inputted by the user when the user clicks the submit button. Here is the code I used for my form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MY Form</title>
</head>
<body>
<form action="q4.php" method="get">
<label><b>First Name:</b></label><br />
<input type="text" name="first_name"><br />
<label><b>Last Name:</b></label><br />
<input type="text" name="second_name">
<br>
<label for="status">Status</label><br>
<select name="status" id="status">
<option value="undergraduate">UnderGraduate</option>
<option value="postgraduate">Postgraduate</option>
<option value="alumni">Alumni</option>
</select>
<br><br><br>
<input type="radio" name="continuing">Continuing<br>
<input type="radio" name="continuing">Not Continuing<br>
<br><br>
<label>Satisfaction Level <br></label>
<label>0</label><input type="range" name="satisfaction" min="0" max="100"/><label>100</label>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the code I used for my PHP:
<?php
$names = array(first_name => '', second_name => '');
reset($names);
while (list($key, $val) = each($names)) {
echo "$key => $_GET\n";
}
?>
If you want to print out all results you could just do something like
<?php
foreach($_GET as $key => $value){
echo $key.": ".$value;
}
?>
But if you would rather only show specific data then perhaps something like this
<?php
$names = array(first_name => '$_GET['first_name']', second_name => 'second_name');
foreach($names as $name => $value){
echo $name.": ".$value;
}
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Avoiding form resubmit in php when pressing f5
I need help on how to prevent the DOUBLE DATA SUBMISSION to mysql database.
This usually occurs when after the submit, user hits REFRESH button.
Whenever I send data using html web forms to handle them in sever side I'd better omit action attribute of a form which explains that the file will be processed by itself and starting from the first line i place server side php scripts. Before sending data I sctrictly check if all fields are filled correctly and then send that data to future processing. Using php script I'll check if web form button which was clicked is not null and then via established connection to mysql execute manipulation queries.
Is using header function to redirect to itself is an optimal way to prevent Double Data Submission???
Haven't you any ideas?
In addition I'll include the code
<?php
include_once '../config.php';
include_once '../functions.php';
if (isset($_POST['bDep'])){
$flt=$_POST['flt'];
$num=$_POST['fltNum'];
$acr=$_POST['acr'];
$city=$_POST['city'];
$fTime=$_POST['fTime'];
$bReg=$_POST['bReg'];
$eReg=$_POST['eReg'];
$acomp=$_POST['aircomp'];
$gate=$_POST['gate'];
$dte=$_POST['data'];
$tm=explode(":",$fTime);
if ($tm[0]<3) $p=1;
else $p=0;
$otmena=$_POST['cancel'];
if ($otmena==1) $stat=4;
else $stat=7;
$sql="INSERT INTO fltdep(FID, FLT, NUM, DEP, TO_FROM, IN_OUT, dte, Stat, BReg, EReg, Gate, AType, ALn, Cir, Hide ) VALUES (0, '$flt', '$num','$fTime', $city,1, '$dte',$stat,'$bReg','$eReg','$gate',$acr,$acomp,$p,0)";
if (mysql_query($sql)){
echo "<p style='text-color:red;text-align:center;'>Успешно добавлена запись № </p>";
}else {
echo "<p style='text-color:red;text-align:center;'>Ощибка при добавлении к базе</p>";
}
unset($_POST['bDep']);
unset($_POST['flt']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Заполнение вылетов</title>
<link rel="stylesheet" href="../css/validationEngine.jquery.css" type="text/css"/>
<link rel="stylesheet" href="../css/template.css" type="text/css"/>
<link rel="stylesheet" href="../themes/base/jquery.ui.all.css" type="text/css">
<link rel="stylesheet" href="../themes/base/flts.css" type="text/css">
<link rel="stylesheet" href="../themes/base/tipsy.css" type="text/css" />
<link rel="stylesheet" href="../themes/base/tipsy-main.css" type="text/css" />
<link href="../themes/base/table.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.6.min.js" type="text/javascript"></script>
<script src="../js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery.maskedinput-1.3.js" type="text/javascript"></script>
<script src="../ui/jquery.ui.core.js"></script>
<script src="../ui/jquery.ui.widget.js"></script>
<script src="../ui/jquery.ui.button.js"></script>
<script src="../ui/jquery.ui.tabs.js"></script>
<script src="../ui/jquery.ui.datepicker.js"></script>
<script type="text/javascript" language="javascript">
jQuery(document).ready(function(){
// binds form submission and fields to the validation engine
$("#data").datepicker({ numberOfMonths: 1, dateFormat: 'yy-mm-dd' });
jQuery("#Departures").validationEngine();
});
$(function(){
$('#fTime').mask('99:99',{placeholder:"_"});
$('#bReg').mask('99:99',{placeholder: "_"});
$('#eReg').mask('99:99',{placeholder: "_"});
});
function SetAline()
{
var fl=new String($('#flt').val());
if (fl.substr(0,2)=='TD' || fl.substr(0,2)=='td' ) { $('#aircomp').val(1); $('#gate').val(2);}
else if (fl.substr(0,2)=='TJ' || fl.substr(0,2)=='tj' ) { $('#aircomp').val(1); $('#gate').val(1);}
else if (fl.substr(0,2)=='SM' || fl.substr(0,2)=='sm' ) { $('#aircomp').val(2); $('#gate').val(1);}
else if (fl.substr(0,2)=='SB' || fl.substr(0,2)=='sb' ) { $('#aircomp').val(3); $('#gate').val(1);}
else if (fl.substr(0,2)=='OR' || fl.substr(0,2)=='or' ) { $('#aircomp').val(4); $('#gate').val(1);}
else if (fl.substr(0,2)=='TA' || fl.substr(0,2)=='ta' ) { $('#aircomp').val(5); $('#gate').val(1);}
else if (fl.substr(0,2)=='SV' || fl.substr(0,2)=='sv' ) { $('#aircomp').val(6); $('#gate').val(1);}
else if (fl.substr(0,2)=='UT' || fl.substr(0,2)=='ut' ) { $('#aircomp').val(7); $('#gate').val(1);}
else if (fl.substr(0,2)=='SD' || fl.substr(0,2)=='sd' ) { $('#aircomp').val(8); $('#gate').val(1);}
else if (fl.substr(0,2)=='YA' || fl.substr(0,2)=='ya' ) { $('#aircomp').val(9); $('#gate').val(1);}
else if (fl.substr(0,2)=='CS' || fl.substr(0,2)=='cs' ) { $('#aircomp').val(10);$('#gate').val(1);}
else if (fl.substr(0,2)=='D9' || fl.substr(0,2)=='d9' ) { $('#aircomp').val(11);$('#gate').val(1);}
}
function SetRegTime()
{
var depTime=$('#fTime').val();
var fl=new String($('#flt').val());
var start,end;
if (fl.substr(0,2)=='TD' || fl.substr(0,2)=='td'){
start='01:30';
end='00:20';
}else {
start='03:00';
end='00:40';
}
$('#bReg').val(TimeDiff(depTime,start));
$('#eReg').val(TimeDiff(depTime,end));
}
function TimeDiff(a,b)
{
var first = a.split(":")
var second = b.split(":")
var xx;
var yy;
if(parseInt(first[0]) < parseInt(second[0])){
if(parseInt(first[1]) < parseInt(second[1])){
yy = parseInt(first[1]) + 60 - parseInt(second[1]);
xx = parseInt(first[0]) + 24 - 1 - parseInt(second[0])
}else{
yy = parseInt(first[1]) - parseInt(second[1]);
xx = parseInt(first[0]) + 24 - parseInt(second[0])
}
}else if(parseInt(first[0]) == parseInt(second[0])){
if(parseInt(first[1]) < parseInt(second[1])){
yy = parseInt(first[1]) + 60 - parseInt(second[1]);
xx = parseInt(first[0]) + 24 - 1 - parseInt(second[0])
}else{
yy = parseInt(first[1]) - parseInt(second[1]);
xx = parseInt(first[0]) - parseInt(second[0])
}
}else{
if(parseInt(first[1]) < parseInt(second[1])){
yy = parseInt(first[1]) + 60 - parseInt(second[1]);
xx = parseInt(first[0]) - 1 - parseInt(second[0])
}else{
yy = parseInt(first[1]) - parseInt(second[1]);
xx = parseInt(first[0]) - parseInt(second[0])
}
}
if(xx < 10)
xx = "0" + xx
if(yy < 10)
yy = "0" + yy
return (xx + ":" + yy);
}
</script>
</head>
<body>
<form id="Departures" class="formular" method="post" action="">
<fieldset>
<legend>
ЗАПОЛНЕНИЕ ВЫЛЕТОВ <i>Прилет</i>
</legend>
<label>
<span>Дата : </span>
<input class="validate[required] text-input" type="text" name="data" id="data" maxlength="12" value="<? (isset($_POST['data']))?($_POST['data']): date('Y-m-d') ;?>" />
</label>
<label>
<span>Рейс : </span>
<input value="" class="validate[required] text-input" type="text" name="flt" id="flt" maxlength="5" onkeyup="SetAline();"/>
</label>
<label>
<label>
<span>№ рейса : </span>
<input value="" class="validate[required] text-input" type="text" name="fltNum" id="fltNum" maxlength="5"/>
</label>
<label>
<span>Тип ВС :</span>
<select name="acr" id="acr" class="validate[required]">
<option value="">Выберите</option>
<?php
$AC= & getAC();
while ($rowAC=mysql_fetch_array($AC)){
$i++;
echo "<option value='".$rowAC['acode']."'>".$rowAC['name_ru']."</option>";
}
?>
</select>
</label>
<label>
<span>А/П прибт :</span>
<select name="city" id="city" class="validate[required]">
<option value="">Выберите</option>
<?php
$city= & getCity();
while ($rowCi=mysql_fetch_array($city)){
$i++;
echo "<option value='".$rowCi['code']."'>".$rowCi['abbr_ru']."</option>";
}
?>
</select>
</label>
<label>
<label>
<span>Время отправления : </span>
<input value="" class="validate[required] text-input" type="text" name="fTime" id="fTime" maxlength="5" onBlur="SetRegTime();"/>
</label>
<label>
<label>
<span>Начало регистрации : </span>
<input value="" class="validate[required] text-input" type="text" name="bReg" id="bReg" maxlength="5" readonly="readonly" />
</label>
<label>
<label>
<span>Конец регистрации : </span>
<input value="" class="validate[required] text-input" type="text" name="eReg" id="eReg" maxlength="5" readonly="readonly"/>
</label>
<label>
<span>Авиакомпания :</span>
<select name="aircomp" id="aircomp" class="validate[required]">
<option value="">Выберите авиакомпанию</option>
<option value="option1">Таджик Эйр</option>
<?php
$Aline= & getAComp();
while ($rowAline=mysql_fetch_array($Aline)){
$i++;
echo "<option value='".$rowAline['acode']."'>".$rowAline['name_ru']."</option>";
}
?>
</select>
</label>
<label>
<span>Терминал :</span>
<select name="gate" id="gate" class="validate[required]">
<option value="">Выберите</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</label>
<label>
<span>Отмена</span>
<input class="checkbox" type="checkbox" name="cancel" id="cancel" value="1"/>
</label>
</fieldset>
<input name="bDep" class="submit" type="submit" value="Заполнить"/><hr/>
</form>
</body>
</html>
Your method (Header redirect after post) is necessary, but insufficient.
Some users press links twice (doubleclick). And your code sometimes might insert the same record twice in this case. You need to write a JavaScript that would disable submit button after first click.