I am building a select option dynamically. I am selecting from the select option, one value. (values for list come from php
How can I pass that selected value to PHP?
<select id ="s1" name="swimopt" class="so">
<?php echo $options; ?>
</select>'
THe $options are coming from a MySQL and populating the dropdown
When I select a value, and try
echo $_POST['swimopt'];
does not show selected value
Please help
<form id="swimdata" method="POST" action="save.php">
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="girls">GIRLS
<table id="meetTable" style="width:auto">
<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">
</form>
This is my PHP getting the $options from mySQL
if ($result->num_rows > 0) {
$options= '';
// output data of each row
while($row = $result->fetch_assoc()) {
$options .= "<tr><td>" . $row["swimmer_id"]. "</td><td>" . $row["first_name"]. " " . $row["last_name"]. " </td><td> " . $row["school_name"]. "</td></tr>";
}
} else {
echo "0 results";
}
echo $options;
$conn->close();
ScreenShots:
Options echoed into select box:
<select id ="s1" name="swimopt" class="so">
<?php echo $options; ?>
</select>'
PHP file code
$sql = "select first_name, last_name from swimming where gender='m'";
$results = mysqli_query($link,$sql);
if ($results->num_rows > 0) {
$options = '';
// output data of each row
while($row = $results->fetch_assoc()) {
$fname=$row["first_name"];
$lname=$row["last_name"];
$options .= "<option value= >" . $row["first_name"] . " " . $row["last_name"]."</option>";
}
} else {
echo "0 results";
}
echo $options;
$link->close();
?>
I think I see what your problem is. You're not setting a select option.
Try this:
<?php
$options = 'John';
?>
<form id="swimdata" method="POST" action="save.php">
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="girls">GIRLS
<table id="meetTable" style="width:auto">
<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>
</tr>
</table>
<select id ="s1" name="swimopt" class="so" value="">
<?php
for($i=0;$i<10;$i++){
echo '<option value="user '.$i.'" >user '.$i.'</option>';
}
?>
</select>
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">
</form>
The select option must be wrapped with html tag <option> with a value attribute, which will be posted if it is selected.
According to your given code you are not generating options for a select. So try something like this:
if ($result->num_rows > 0) {
$options= '';
// output data of each row
while($row = $result->fetch_assoc()) {
$options .= "<option value=\"". $row["swimmer_id"] ."\">" . $row["first_name"]. " " . $row["last_name"]. " - " . $row["school_name"]. "</option>";
}
} else {
echo "0 results";
}
echo $options;
$conn->close();
Now if you $_POST it you will get the swimmer id
Related
I have an issue in a simple PHP MySQL project. When I try to fetch a record from a select option, it only gives me the id. I want the value from the select to be passed too.
<form class="form-style-9" method="POST" action="function.php?type=updatedistrict">``
<ul>
<li>ID:
<input type="hidden" name="dist_id" class="field-style field-full align-none" value="<?php echo $ids;?>" readonly />
</li>
<li>District Name:
<input type="text" name="name" class="field-style field-full align-none" value="<?php echo $names;?>" />
</li>
<li>Postal Code:
<input type="text" name="code" class="field-style field-full align-none" value="<?php echo $codes;?>"/>
</li>
<li>Province
<select name="province_id" class="form-control">
<option value="">Select Province</option>
<?php
$sql = mysqli_query($conn, "SELECT id, province From province");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['id'] ."'>" .$row['province'] ."</option>" ;
}
?>
</select>
<li>
<input type="submit" value="Update" name="submit" />
</li>
</ul>
</form>
This is the update function code:
case 'updateprovince':
if (isset($_POST['submit'])) {
$id = $_POST['dist_id'];
$prov_id= $_POST['province_id'];
$query=$conn->query("UPDATE province SET province='$prov_id' where id='$id'");
if ($query) {
echo '<script> window.location.replace("province.php");
</script>';
}
else
{
echo "issue";
}
}
break;
You can post your province name instead of the id this way:
echo "<option value='". $row['province'] ."'>" .$row['province'] ."</option>" ;
Your $_POST['province_id'] will then contain the province value instead of the current id.
A way to pass along both the id and the value (in case you need to retain the id for some additional processing) through your option would be this:
echo "<option value='". $row['id'] . '|'. $row['province'] ."'>" .$row['province'] ."</option>" ;
Then after the submit, you'd simply split the two by the '|' delimiter.
if (isset($_POST['submit'])) {
$id = $_POST['dist_id'];
$province = explode('|', $_POST['province_id']);
$province_id = $province[0]
$province_name = $province[1]
I am new to php and can not turn a row into a checkbox field that passes onto a separate page.
if ($result = $db->query($sql) ) {
//*start of original
if ($result->num_rows > 0) {
?>
<!-- <form action="Confirmation.php" method="post"> -->
<form action="Payment.php" method="post">
<label for="PatientID">Choose the Patient you wish to register for:</label>
<select name="PatientID" id="">
<option value="" disabled>Please select a patient to enroll:</option>
<!-- <option value="All">All Patients</option> -->
<?php
while($result = fetch_assoc($result)) {
echo "<input type='checkbox' value='{$row['PatientID']}'>" . $row['PatientName'] . '</br>';
}
//
//while ($row=$result->fetch_assoc()) {
?>
//<option value="<?=$row['PatientID'];?>"> <?=$row['PatientName']?></option>
//*/ <?php
//}
echo '</select><input type= "submit" class="myButton" name="submit" value="Register Now" /></form>';
} else {
echo "There are no patients currently available for registration." ;
$rosterListFail = true;
}
} else {
$message = $db->error;
// echo $message;
}
//*/
//}
?>
I'm facing an issue most probably due to my lack of experience.
I'm getting the data successfully from the MYSQL DATABASE and it is successfully populating the Second DROPDOWN menu.
The problem is... when I click in "Validate" to submit and record the data in the variable it cleans the previous option selected. I'm being unable to record the selected options in variables.
<?php
$location = "";
$locationf = "";
$system = "";
$systemf = "";
$conn = mysqli_connect('localhost', 'root', 'test', 'SRSBASE')
or die('Cannot connect to db');
$result = $conn->query("select distinct SUB_ACCOUNT from SRSLOAD");
if (isset($_POST["selec"]["account"])) {
$location = $_POST["selec"]["account"];
$locationf = $location;
$locationf = sprintf('%s', $locationf);
}
echo "Location: $locationf";
$conn2 = mysqli_connect('localhost', 'root', 'test', 'SRSBASE')
or die('Cannot connect to db');
$result2 = $conn2->query("select distinct SYSTEMNAME from SRSLOAD where SUB_ACCOUNT='$locationf'");
if (isset($_POST["selec"]["system"])) {
$system = $_POST["selec"]["system"];
$systemf = $system;
}
echo "System: $systemf";
$post_at = "";
$post_at_to_date = "";
$post_at_todate = "";
$queryCondition = "";
if (!empty($_POST["search"]["post_at"])) {
$post_at = $_POST["search"]["post_at"];
list($fiy, $fim, $fid) = explode("-", $post_at);
$post_at_todate = date('YY-mm-dd');
if (!empty($_POST["search"]["post_at_to_date"])) {
$post_at_to_date = $_POST["search"]["post_at_to_date"];
list($tiy, $tim, $tid) = explode("-", $_POST["search"]["post_at_to_date"]);
$post_at_todate = "$tiy-$tim-$tid";
//TESTING SELECTED TARGETS
//echo $post_at;
//echo "/";
//echo $post_at_todate;
}
//$queryCondition .= "WHERE RDATE BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'";
$queryCondition .= "WHERE RDATE BETWEEN '$post_at' AND '" . $post_at_todate . "'";
}
//$sql = "SELECT * from SRSLOAD " . $queryCondition . " ORDER BY post_at desc";
//$sql = "select * from SRSLOAD where rdate between '$post_at' AND $post_at_todate;"
$sql = sprintf("SELECT * FROM SRSLOAD WHERE RDATE BETWEEN '%s' AND '%s' AND SYSTEMNAME='%s' AND SUB_ACCOUNT='%s'", $post_at, $post_at_todate, $systemf, $locationf);
$result3 = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Storage Report System - Search</title>
<script src="jquery-1.9.1.js"></script>
<link rel="stylesheet" href="jquery-ui-1.11.4.css">
<style>
.table-content{border-top:#CCCCCC 4px solid; width:50%;}
.table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;}
.table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px solid;vertical-align:top;}
</style>
</head>
<body>
<h2 style='font-family:arial'>Storage Report System - Search</h2>
<form name='sname' id='sname' action='' method='POST'>
<select id='select' name="selec[account]" value="<?php echo $location; ?>" >
<option value='-1'>--Select the Location--</option>
<?php
while ($row = $result->fetch_assoc()) {
unset($sub_acc);
$sub_acc = $row['SUB_ACCOUNT'];
echo '<option value="' . $sub_acc . '">' . $sub_acc . '</option>';
}
?>
</select>
<input type='submit' value='Validate' />
</form>
<form name='sname' id='sname' action='' method='POST' >
<select id='system' name="selec[system]" value="<?php echo $system; ?>" >
<option value='-1'>--Select the System--</option>
<?php
while ($row2 = $result2->fetch_assoc()) {
unset($syst);
$syst = $row2['SYSTEMNAME'];
echo '<option value="' . $syst . '">' . $syst . '</option>';
}
?>
</select>
<input type='submit' value='Validate' />
</form>
<div class="demo-content">
<form name="frmSearch" method="post" action="">
<p class="search_input">
<input type="text" placeholder="From Date" id="post_at" name="search[post_at]" value="<?php echo $post_at; ?>" class="input-control" />
<input type="text" placeholder="To Date" id="post_at_to_date" name="search[post_at_to_date]" style="margin-left:10px" value="<?php echo $post_at_to_date; ?>" class="input-control" />
<input type="submit" name="go" value="Search" >
</p>
<?php if (!empty($result3)) { ?>
<table class="table-content">
<thead>
<tr>
<th width="30%"><span>SYSTEM NAME</span></th>
<th width="50%"><span>DATE</span></th>
<th width="20%"><span>HSM</span></th>
</tr>
</thead>
<tbody>
<?php
while ($row3 = mysqli_fetch_array($result3)) {
?>
<tr>
<td><?php echo $row["SYSTEMNAME"]; ?></td>
<td><?php echo $row["RDATE"]; ?></td>
<td><?php echo $row["HSM_MCDS"]; ?></td>
</tr>
<?php
}
?>
<tbody>
</table>
<?php } ?>
</form>
</div>
<script src="jquery-ui-1.10.3.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'yy-mm-dd'
});
$(function () {
$("#post_at").datepicker();
$("#post_at_to_date").datepicker();
});
</script>
</body>
</html>
It is because you have two forms there. Every form has its own select and submit. When you click submit, only appropriate form with select is sent.
When you want to have data from both selects, you have to have both selects in one form with one submit.
Something like this code:
<form name='sname' id='sname' action='' method='POST'>
<select id='select' name="selec[account]" value="<?php echo $location; ?>" >
<option value='-1'>--Select the Location--</option>
<?php
while ($row = $result->fetch_assoc()) {
unset($sub_acc);
$sub_acc = $row['SUB_ACCOUNT'];
echo '<option value="' . $sub_acc . '">' . $sub_acc . '</option>';
}
?>
</select>
<select id='system' name="selec[system]" value="<?php echo $system; ?>" >
<option value='-1'>--Select the System--</option>
<?php
while ($row2 = $result2->fetch_assoc()) {
unset($syst);
$syst = $row2['SYSTEMNAME'];
echo '<option value="' . $syst . '">' . $syst . '</option>';
}
?>
</select>
<input type='submit' value='Validate' />
</form>
I have select box in PHP and want text selected item of that:
<form action="" method="get" target="" dir="rtl" class="w3-container">
<?php
echo " <select id='p1' name='p1' >";
echo "<option value='*'>Choose person</option>";
sql code
$result2 = mysql_query($query2,$con);
while($row2 = mysql_fetch_array($result2))
{
$row2['name'];
echo "<option value='".$row2['id']."'>".$row2['name']."</option>";
}
echo " </select>";
mysql_close($con);
?>
<input class="w3-btn w3-hover-yellow" name="" type="submit" value="جستجو">
</form>
<?php
if(isset($_GET['p1']) and $_GET['p1'] != "*")
{
if ($name_insert=trim($_GET["p1"]))
{
if ($strWhere == ""){
$strWhere .= "name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
else{
$strWhere .= " and name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
}
}
?>
I want code that get text of select box in $name_insert. This code gets value of that.
You can use javascript and save selected option text in a hidden field.
Example
<form action="" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
PHP
$getOptionText = $_POST['test_text'];
Replace your code with this code
<form action="" method="get" target="" dir="rtl" class="w3-container">
<select id='p1' name='p1' onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value='*'>Choose person</option>
<?php
$result2 = mysql_query($query2,$con);
while($row2 = mysql_fetch_array($result2))
{
echo "<option value='".$row2['id']."'>".$row2['name']."</option>";
}
mysql_close($con);
?>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
<input class="w3-btn w3-hover-yellow" name="" type="submit" value="جستجو">
</form>
<?php
if(isset($_GET['p1']) and $_GET['p1'] != "*")
{
$getValueOfText = $_GET['test_text'];
echo $getValueOfText;
if ($name_insert=trim($_GET["p1"]))
{
if ($strWhere == ""){
$strWhere .= "name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
else{
$strWhere .= " and name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
}
}
?>
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
When I submit this form, I stay on the same page, but I want a the same time to keep the option selected previously option visible.. How can I do that?
I tried:
document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>";
But it did not work..
here:
foreach($allemp as $row){
echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>";
}
Try this :
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
added a line above echo options ($selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';)
Added '.$selected.' in side options.
Your code becomes : Copy paste below code instead of yours
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
} ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>