How to select only id instead of print name in select options? - php

<select id="rightsid" name="rightsid">
<?php
while($row=mysql_fetch_array($list)) {
$admno=$row["adm_no"];
$name=$row["name"];
?>
<option id="<?php echo $admno;?>" name="<?php echo $admno;?>"><?php echo $admno;?> <?php echo $name;?></option>
<?php
}
?>
<input type="submit" value="Show Rights">
</select>
This is my code. When I take the value of select to next page, it is taking both name and admno but I want to take only admno but here in dropdown list. I want to display both but want to carry only one. How to do this?

Within option value, write $admo. Try this:
<?php
while ($row=mysql_fetch_array($list)) {
$admno = $row["adm_no"];
$name = $row["name"];
echo '<option id = "'.$admno.'" name = "'.$admno.'" value = "'.$admno.'" >' . $admno . $name . '</option>';
}
?>

Use value in option as below
<select id="rightsid" name="rightsid">
<?php
while($row=mysql_fetch_array($list)) {
$admno=$row["adm_no"];
$name=$row["name"];
?>
<option value="<?php echo $admno;?>" id="<?php echo $admno;?>" name="<?php echo $admno;?>"><?php echo $admno;?> <?php echo $name;?></option>
<?php
}
?>
</select>

<option id="<?php echo $admno;?>" value="<?php echo $admno;?>"><?php echo $name;?></option>

Related

How to get the second value in the echo in PHP?

I have some options to select and would like to set some textinputs to visible or hidden depending on what user selects. I have the functionality already but in this case I need to take the second value of the echo and check if its equal to something, I can currently only check the first value of the echo:
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
So, I need to get ONLY the value of $row['serial_or_imei'] and compare onchange="if (this.value=='') , how can I do that?
EDIT: further explanation
I need to read the value of $row['serial_or_imei'] which could be empty, 'serial' OR 'imei', so, one of these three.
P.S: I must keep the $row['id'] in the value attribute because I am passing that value later to server so dont suggest removing that.
You can add one attribute to option element and test with it. something like
<select id="type_id" name="device_type_id" onchange="if (this.getAttribute('data')==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option data="<?php echo $row['serial_or_imei'] ?>" value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You can compare the value of an array in the onchange by assign the value to a variable which will be a quite neat format as
<?php $value = echo $row['serial_or_imei']; ?>
<select id="type_id" name="device_type_id" onchange="if (this.value=='<?php empty($value)?>'){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You add a new custom attribute serial and supply the value of serial_or_imei to it and then compare in javascript with onchange="if (this.value=='')
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-serial="<?php echo $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
EDIT: Attribute “serial” is not allowed on element “option”. Changed this to data-seial.
You say you need the values back to server because they change dynamically. A clean way would be to have a dedicated hidden field:
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
Here's a full self-contained script to illustrate it:
<?php
$random_serial_or_imei = range(1000, 9999);
shuffle($random_serial_or_imei);
$result = [];
for ($i = 0; $i < 5; $i++) {
$result[] = [
'id' => $i,
'serial_or_imei' => $random_serial_or_imei[$i],
'name' => "Name #$i",
];
}
?>
<select name="device_type_id">
<option value="">Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
This prints something like:
<select name="device_type_id">
<option value="">Select</option>
<option value="0">Name #0</option>
<option value="1">Name #1</option>
<option value="2">Name #2</option>
<option value="3">Name #3</option>
<option value="4">Name #4</option>
</select>
<input type="hidden" name="map[0]" value="9513">
<input type="hidden" name="map[1]" value="3931">
<input type="hidden" name="map[2]" value="3971">
<input type="hidden" name="map[3]" value="4476">
<input type="hidden" name="map[4]" value="6429">
Selected type can be found at $_POST['device_type_id'] and the corresponding IMEI at $_POST['map'][ $_POST['device_type_id'] ].
Based on the information from the comments, I think this solution is what you are looking for. Note I move the onchange to a function for clarity.
You can see the "three" option hides the text input as it doesn't have a imei.
Also notice how I use the data- attribute inside your option so you can retrieve that information as well, independent of the option id. Also note the this.form['device_imei_textinput'] didn't worked. The elements reference is required.
function check_imei(obj, form) {
var imei = obj.options[obj.selectedIndex].getAttribute('data-imei');
if (imei == '') {
form.elements.device_imei_textinput.style.visibility='hidden';
} else {
form.elements.device_imei_textinput.style.visibility='visible';
}
}
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<option value="1" data-imei="foo">One</option>
<option value="2" data-imei="bar">Two</option>
<option value="3" data-imei="">Three</option>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Mirror of example : https://jsfiddle.net/asp1hewx/
EDIT : To address the PHP side of things, the form above could be generated with :
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-imei="<?php $row['serial_or_imei'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>

PHP getting value from a dynamic select list

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

How to retain dropdown selected option after submit / page refresh

I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.
How to retain selected value after submit/refresh with this code?
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
Solved this way:
Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
Because I use explode on $_POST['test_email']
I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.
Perhaps this could be simpler but it works like a charm.
#roberto06: Thank you for your guidance.
You need to add the selected="selected" attribute on the option matching your $_POST value.
You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.
Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
</form>

Database driven select box being empty on submit

I am trying to submit a form value in a database with php. In form a select box value comes from database.
<?php include_once 'header.php';
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
//form validion
if(isset($_POST['submit']))
{
$eid =$_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
$miatm =trim($_POST["miatm"]);
if(empty($miatm) || !preg_match("/^[a-zA-Z0-9 ]*$/",$miatm)) {
$flag=1;
$miErr="Please Enter Valid Id";
}
.............like this
if($flag==0)
{
$sqll="insert into **********";
}
//my form is
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
Its seems Ok.but when i tried to submit the form if some of one field remain empty then its show blank value in select box.
how can i remain the same selected value in select box even if textbox remain empty.
You need to retain the value of drop down after form submit.
User selected attribute of select option.
<?php
if (isset($_POST['submit'])) {
$eid =$_POST["eid"];
if ($eid=="blank") {
$flag=1;
$idErr="please Select E-MITRA";
}
}
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result)) {
$selected = (isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected="selected"' : '';
?>
<option value="<?php echo $row['uid']; ?>" <?php echo $selected;?>><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
You need to use selected="" or selected="selected" after submission in your select tag as a attribute as:
<?
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
$selected = ((isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected=""' : '');
?>
<option <?=$selected?> value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
if(isset($_POST['submit']))
{
$eid = $_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
?>
</select>
Side Note:
In your question ist two lines are not inside the php, i hope this is type error.

HTML Option tag with selected value from PHP

Can you please help me out as to what I am doing wrong with this code:
<option value="">------------ Select ------------</option>
<?php while (($row = mysql_fetch_array($tech))) {
?>
<option value= <?php echo $row['id'] ?>
<?php echo $selectedtechname ?>
<?php echo "selected" ?>
>
<?php echo $row['technician_name'] ?></option>
<?php } ?>
All variables are working fine since I used Var_dump and they echo correctly so no errors is SQL etc. What I need to do is pass a php statement in the option tag of HTML to simply select the value of $selectedtechname from the list. The value to post is $row['id'] but the selected value to show should be $selectedtechname
Thanks
I see a similar post
populate a select box with php mysql
while($row = mysql_fetch_assoc($result)) {
if ($row['technician_name'] == $selectedtechname) {
echo '<option value=\"'.$row['id'].'" selected>'.$row['id'].'</option>';
} else {
echo '<option value=\"'.$row['id'].'">'.$row['id'].'</option>';
}
}
You can try the following:
$selected='';
<option value="">Select a value</option>
<?php
while ($row = mysql_fetch_array($tech)) {
?>
<?php
if($selectedtechname == $row['selectedtechname'])
{
$selected='selected';
}
else
{
$selected='';
}
?>
<option value="<?php echo $row['id']?>" <?php echo $selected;?> ><?php echo $row['value_to_display'];?></option>
<?php
}
?>

Categories