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.
Related
While editing the record from database, i display already selected value and option to select other values too. But i want to avoid already selected value to display twice in dropdown. Not getting how to do it
here is my code
<label class="control-label">Sales Area</label>
<?php
$sql5 = "SELECT * FROM sales_area ORDER BY name";
$query5 = mysqli_query($con, $sql5);
?>
<select name="area" class="form-control" required>
<option value="<?php echo $row['sales_area']; ?>"><?php echo $row['areaname']; ?></option>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>"><?php echo $rs5["name"]; ?></option>
<?php } ?>
</select>
in $row['sales_area'], data already present in database, this should not display again.
haven't tested it, but should be something like this:
<select name="area" class="form-control" required>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>" <?php if($rs5["id"] == $row['sales_area'] ) { echo "selected"; } ?> ><?php echo $rs5["name"]; ?>
</option>
<?php } ?>
</select>
I want a first radio button to be checked by default. When I click get schedule I want to fetch the schedule of that radio selected train number using a WHERE clause.
<?php
while ($res = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type = radio />" . $res['Train_no'] . "</td>";
?>
<form action="schedule.php" method="POST" class="form-inline">
<input type="submit" value="Get Schedule"/>
</form>
<?php
}
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('traindb', $con);
} else {
die('Could not connect: ' . mysql_error());
}
$selected_val = $_POST['Train_no']; // Storing Selected Value In Variable
echo "You have selected :" . $selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE Train_No='$selected_val'");
mysql_close($con);
?>
You can try this:
if(isset($_POST['Submit'])){
$selected_val = $_POST['Train_no']; // Storing Selected Value In
echo "You have selected :" .$selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE
Train_No='$selected_val'");
}
Here is one example of "select":
<form action="" method="post">
<select type="age" class="form-control" id="age" name="age">
<!-- <option value="disable" selected="">Please Select</option> -->
<option value="">Please select</option>
<option value="Under 35">Under 35</option>
<option value=">35 - 44">35 - 44</option>
<option value=">45 - 54">45 - 54</option>
<option value=">55 - 59">55 - 59</option>
<option value=">60 - 64">60 - 64</option>
<option value=">65 - 69">65 - 69</option>
<option value=">70 - 74">70 - 74</option>
<option value=">75 - 79">75 - 79</option>
<option value="80 +">80 +</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
$age = $_POST['age'];
if (isset($_POST['age']) && $_POST['age'] == "")
echo "You did not choose any options. Pls try again.";
else {
echo $age;
}
?>
If you want embed PHP in select options, do it like this:
<option value="<?php echo $res['Train_no'] ?>"><?php echo $res['some_other'] ?></option>
For radio button it is like this:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<input type="radio" name="gender" value="<?php echo $res['Train_no'] ?>" checked><?php echo $res['some_other'] ?><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br><br>
<input type="submit">
</form>
</body>
</html>
I have create a select option form but when edit i can get current value from database.
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($nm_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?php echo $selected;?>>
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
You should not use value for select tag. Check the condition in inside the option tag and make it is selected like following
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){?>
<option value="<?php echo $value->kd_proyek; ?>" <?php if($nm_proyek==$value->kd_proyek) { ?> selected="selected" <?php }?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>
<?php
$conn = new mysqli('SERVER NAME', 'DB USER NAEM', 'DB USER PASSWORD', 'DB NAME');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//set the value of which option you want to selected
$nm_proyek = '';
?>
<!-- for combobox you should use 'multiple' -->
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." multiple />
<option value=""></option>
<?php
$query = $conn->query("SELECT * FROM `t_proyek`");
while($value = $query->fetch_object()) {?>
<option value="<?php echo $value->kd_proyek; ?>" <?php $nm_proyek == $value->kd_proyek ? "selected='selected'" : "" ?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>
I have created a form which allows users to edit existing data within a database, I pull information from one page to the next to populate text boxes and select boxes. I have managed to populate the select box with the correct value but when the update statement goes through it deletes or doesn't recognize the pre-existing value. Can anyone help?
if (isset($_POST['submit'])) {
// Process the form
if (empty($errors)) {
$id = $brand["brandId"];
$brandName = mysql_prep($_POST["brandName"]);
$brandCategory = mysql_prep($_POST["brandCategory"]);
$brandKeyword = mysql_prep($_POST["brandKeyword"]);
$addedBy = mysql_prep($_SESSION['username']);
$query = "UPDATE brands SET ";
$query .= "brandName = '{$brandName}', ";
$query .= "brandCategory = '{$brandCategory}', ";
$query .= "brandKeyword = '{$brandKeyword}', ";
$query .= "addedBy = '{$addedBy}', ";
$query .= "dateTime = CURRENT_TIMESTAMP ";
$query .= "WHERE brandId = '{$id}' ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) == 1) {
// Success
$_SESSION["message"] = "Brand updated.";
redirect_to("search.php");
} else {
// Failure
$_SESSION["message"] = "Brand update failed.";
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
<?php $layout_context = "user"; ?>
<?php include("../includes/layouts/header.php"); ?>
<?php include("../includes/layouts/navigation.php"); ?>
<div class="section">
<div id="message">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
</div>
<form id="edit_brands" action="edit_brands.php?id=<?php echo urlencode($brand["brandId"]); ?>" method="post">
<h2>Edit Brand Information: <?php echo htmlentities($brand["brandName"]);?></h2>
<p>
<label for="bname">Brand Name:</label>
<input class="textbox" id="bname" type="text" name="brandName" value="<?php echo htmlentities($brand["brandName"]); ?>" autofocus/>
</p>
<p>
<label for="bcategory">Brand Category:</label>
<select class="textbox" id="bcategory" type="text" name="brandCategory">
<option value=""><?php echo htmlentities($brand["brandCategory"]); ?></option>
<option value="Animation">Animation</option>
<option value="Automotive">Automotive</option>
<option value="Beauty and Fashion">Beauty & Fashion</option>
<option value="Comedy">Comedy</option>
<option value="Cooking and Health">Cooking & Health</option>
<option value="DIY">DIY</option>
<option value="Fashion">Fashion</option>
<option value="Film and Entertainment">Film & Entertainment</option>
<option value="Food and Drink">Food & Drink</option>
<option value="Gaming">Gaming</option>
<option value="Lifestyle">Lifestyle</option>
<option value="Music">Music</option>
<option value="News and Politics">News & Politics</option>
<option value="Science&Education">Science & Education</option>
<option value="Sports">Sports</option>
<option value="Technology">Technology</option>
<option value="Television">Television</option>
</select>
</p>
<p>
<label for="bkeyword">Brand Keyword:</label>
<textarea class="FormElement" id="bkeyword" name="brandKeyword" id="brandKeyword" placeholder=""><?php echo htmlentities($brand["brandKeyword"]); ?></textarea>
</p>
<p>
<input type="submit" class="button" name="submit" value="Edit Brand" onclick="return confirm('Do you wish to edit brand?');"/>
</p>
<p>
Cancel
</p>
</form>
</div>
</div>
The best way is to build the select from an array.
For instance:
<?php
$array = array('Animation', 'Automotive', 'Beauty and Fashion ', ...);
echo '<select class="textbox" id="bcategory" type="text" name="brandCategory">';
foreach ($array as $value){
if($value == htmlentities($brand["brandCategory"]){
echo '<option value='.$value.' selected>'.$value.'</option>';
}else{
echo '<option value='.$value.'>'.$value.'</option>';
}
}
echo '</select>;
This way you can check if the value in the array is the same that the one recieved by post and then add the selected attribute to the option tag.
I'm developing a webpage with a select list that contains images.
I already have this:
When I select an image name in the list the image will be displayed in the div below.
<?php
// Create connection
$con=mysqli_connect("******","***","***","charts");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<form method="post" action="index.php" id="nano" name="nano">
<p>
<select name="SelectBox" id="SelectBox" onchange="this.form.submit()">
<?php if($_POST['submitted'] == true){ ?>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano WHERE IMAGE_NAME ='". $_POST['SelectBox']."'");
while($row = mysqli_fetch_array($result))
{ ?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php } ?>
<?php echo $_POST['SelectBox']; ?></option>
<?php } else{ ?>
<?php
$result = mysqli_query($con,"SELECT TOP * FROM Nano");
while($row = mysqli_fetch_array($result))
{
?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php
$var1 = $row['IMAGE_NAME']; ?>
<?php
}
?>
<?php } ?>
<option value="" disabled="disabled"> -------- </option>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano");
while($row = mysqli_fetch_array($result))
{ $values[] = $row['IMAGE_NAME'];
?>
<option value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php }?>
</select>
<input type="hidden" name="submitted" id="submitted" value="true" />
</p>
<?php if($_POST['submitted'] == true){ ?>
<p><img src="Images\Nano\<?php echo $_POST['SelectBox']?>" width="953" height="600" /></p>
<?php }else { ?>
<p><img src="Images\Nano\<?php print_r($values[0]) ?>" width="953" height="600" /></p>
<?php } mysqli_close($con);?>
</form>
</div>
I want when I move down in the select list the picture will change and not when I click on it in the select list.
In your case, you have to bind hover event to option, but there is no way to do what you want using native select control. The native one only answers when you click a different option from previous.
However, you can simulate a select control using html&css&js, that way when your cursor move down the simulated option(which might be a div or something), you can bind event handlers to it and display the name.