This question already has answers here:
PHP: on select change, post form to self
(3 answers)
Closed 9 years ago.
I have a working drop down box but im not sure how to make it so that if they chose "volksvagen" it would display
"Your Favourite Car is Volksvagen!"
and so on for each of the options, here is the code for the drop down box.
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars">';
foreach($array1 as $cars){
echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
</div>
javascript not used..try it..!!
<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar='';
}
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
echo 'your favourite car is : '; echo $mycar;
?>
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="display_message(this.value);">';
foreach($array1 as $cars){
echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
<span id="message"></span>
</div>
<script>
function display_message($selected_value)
{
document.getElementById('message').innerHTML = 'Your Favourite Car is'+$selected_value+'!';
}
</script>
<body onload="showCar()">
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" id="cars" onchange="showCar()">';
foreach($array1 as $cars){
echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
<div id="demo"> </div>
<script>
function showCar() {
var car = document.getElementById('cars').value;
document.getElementById("demo").innerHTML="Your Favourite Car is "+car+"!";
}
</script>
</div>
</body>
Try This
Related
So I have 2 drop down lists, I need to chose a year from them. Then I need to show the chosen year and the number of population for that year.
For example:
For year 1800: the population is : 3,929,214
For year 1900: the population is : 76,212,168
Population increased by: 72,282,954.
So here is my code until now:
<html>
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<h1>Population Change Calculator</h1>
<?php
$population=[3929214,5236631,7239881,9638453,12866020,17069453,23191876,31443321];
$year=(range(1790, 1860, 10));
$array= array_combine($year, $population);
?>
<form method="post">
<p><label for="year1">Year 1:</label>
<select name="year1">
<option value=""></option>
<?php
foreach ($array as $year1=>$population){ ?>
<option value="<?php echo $year1; ?>"><?php echo $year1; ?></option>
<?php }
?>
</select>
</p>
<p>
<label for="year2">Year 2:</label>
<select name="year2">
<option value=""></option>
<?php
foreach ($array as $year2=>$population){ ?>
<option value="<?php echo $year2; ?>"><?php echo $year2; ?></option>
<?php }
?>
</select>
</p>
<input type="submit" name="submit" value="Submit">
<br>
</form>
<?php
$ini_set = ini_set('display_errors', 1);
error_reporting(E_ALL);
$empty=true;
if(isset($_POST['submit'])){
if(empty($_POST['year1'])){
$empty=FALSE;
print "<p>Please choose Year 1.</p>";
}
if(empty($_POST['year2'])){
$empty=false;
print "<p>Please choose Year 2.</p>";
}
}
I cant figure out how to print the chosen year and the population for that year.
I am new to PHP, thank you in advance.
It seems weird that you are setting the value of your 'year' options to the population. Perhaps this is a mistake..?
You should change them to use the year:
<option value="<?php echo $year1; ?>"><?php echo $year1; ?></option>
Once you do that, you can print your desired values by doing something like this:
if (isset($_POST['submit'])) {
if (empty($_POST['year1'])) {
$empty = FALSE;
print "<p>Please choose Year 1.</p>";
} else {
print "<h1>Year 1: {$_POST['year1']}</h1>";
print "<p>Population: {$array[$_POST['year1']]}</p>";
}
if (empty($_POST['year2'])) {
$empty = false;
print "<p>Please choose Year 2.</p>";
} else {
print "<h1>Year 2: {$_POST['year2']}</h1>";
print "<p>Population: {$array[$_POST['year2']]}</p>";
}
}
I wanted to select a table from a specific database and then the list of tables will appear in an option tag under select tag.
Thanks a lot.
Current Code:
<?php
include 'database/connectdatabase.php';
if(isset($_POST['select_db']))
{
$select_db = $_POST['select_db'];
$selectdb_query = 'SHOW TABLES FROM $select_db';
$query_select = mysql_query($selectdb_query,$connectDatabase);
if(!$query_select)
{
echo 'NO table selected!';
}
?>
<form method="POST" action="selecttable.php" autocomplete="off">
<select name="select_db">
<option selected="selected">Select Database</option>
<option>section_masterfile</option>
</select>
</form>
<form method="POST" action="#" autocomplete="off">
<?php while ($row = mysql_fetch_row($query_select)) {
$num_row = mysql_num_rows($row);?>
<select name="select_table">
<option selected="selected">Select Table</option>
<?php for($i=0;$i>=$num_row;i++){?>
<option><?php echo $row[0];?></option>
<?php}?>
</select>
<?php}?>
</form>
The problem is that you're already fetching rows yet you haven't even submitted the form yet.
I suggest restructure you logic this way:
$con = new mysqli('localhost', 'username', 'password', 'database');
$tables = array();
if(isset($_POST['select_db'])) { // if its submitted
$select_db = $con->real_escape_string($_POST['select_db']); // escape the string
$query = $con->query("SHOW TABLES FROM $select_db");
while($row = $query->fetch_assoc()) {
$tables[] = $row['Tables_in_' . $select_db]; // use associative instead
}
}
?>
<form method="POST" autocomplete="off">
<select name="select_db" onchange="this.form.submit();">
<option disabled selected>Select Database</option>
<option>test</option>
</select>
<br/><br/>
<select name="select_table">
<?php foreach($tables as $table): ?>
<option value="<?php echo $table; ?>"><?php echo $table; ?></option>
<?php endforeach; ?>
</select>
</form>
Sidenote: If you have turned on the error reporting, this should have given some red light to what you are doing wrong. Kindly turn it on.
error_reporting(E_ALL);
ini_set('display_errors', '1');
<select name="select_table">
<option selected="selected">Select Table</option>
<?php while ($row = mysql_fetch_row($query_select)) { ?>
<option value = "<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php } ?>
</select>
Try This
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select))
{
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
Consider this code:
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select)) {
?>
<option><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
I currently have a drop down box than when one of the options is selected it will echo-
"Your Favourite Car Is (option)
What I need to do now is change this so its a text box but the user can only type in one of the options within the array and if another one was chosen it would say you cannot have this as one of the choices and it would also be able to type in more than one so theoretically it could echo-
"Your Favourite Car is Mazda, Nissan, Renault!"
Here is the code i have now for the drop box that i have working.
<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar="";
}
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars)
{ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "
"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
echo "Your favourite car is $mycar";
?>
</div>
EDIT: I have attempted this and what i currently have always echo's "this car isnt among the selection" and nothing else and nothing i enter into the text box seems to effect this
here is the code i have
<?php
$cars = array("Volkswagen","Renault","Land Rover");
?>
<form action="array.php" method="post">
<center> <input type="text" name="cars" id="cars" />
<input type="submit" /> </center>
<?php
if (in_array($_POST, $cars)) {
echo "Your Favourite Car is $_POST";
}
else {
echo "This car is not among the selection";
}
?>
</form>
You can easily control the user input by checking it before:
<form method="post">
<div id="dropdown">
<?php
// Car types
$carTypes = array('Volkswagen' , 'Renault' , 'Land Rover');
$wrongCarChoosen = false ;
if(isset($_POST['cars'])) {
$mycar = $_POST['cars'];
if (!in_array($mycar, $carTypes)) {
$wrongCarChoosen = true ;
}
}
else {
$mycar = "";
}
echo' <select name="cars" onchange="this.form.submit()">';
foreach($carTypes as $carName){ ?>
<option value="<?php echo $cars; ?>"
<?php
if($mycar == $carName) echo "
"selected='selected'"; ?> >
<?php echo $carName;
?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
if ($wrongCarChoosen) {
echo "Your choice ".$mycar." is not contained in ".implode($carTypes, ',') ;
}
else {
echo "Your favourite car is $mycar";
}
?>
</div>
You must edit the form to accept multiple values. And you can check the user values before you echo the text.
<form method="post">
<div id="dropdown">
<?php
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
$error = false;
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
foreach ($mycar as $car)
{
if (!in_array($car, $array1))
{
$error = $car;
}
}
}
else
{
$mycar=Array();
}
echo' <select name="cars[]" multiple="multiple">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if(in_array($cars, $mycar)) echo "\"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
<input type="submit" name="submit" value="submit" />
</div></form>';
?>
<div id="result">
<?php
if ($error===false) echo "Your favourite car is ".implode(', ', $mycar);
else echo $error . ' is not contained by ' . implode(', ', $array1);
?>
</div>
I am trying to print the dropdown selected item. I have well displayed the dropdwon list menu. but when i select an option it doesn't print the option. i have tried in many ways. But not yet got! Please help me, this is my following code.
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
You have issues in your code, try this one instead :
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
$_GET['category']
should be
$_POST['category']
Example for javascript:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('cat');
eSelect.onchange = function() {
document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
document.getElementById("displaytext").style.display= 'block';
}
}
</script>
</head>
<body>
<select id="cat" name="cat">
<option value="x">X</option>
<option value="y">Y</option>
<option value="other">Other</option>
</select>
<div id="displaytext" style="display: none;" ></div>
</body>
</html>
I have my textboxe showing the data that is in the correct field from the database, but not when it is a drop down (select)
I have tried this code
<?php
$rs_settings = mysql_query("SELECT * from thesis WHERE user_id = $user_id;");
?>
<form action="academic.php" method="post" name="regForm" id="regForm" >
<?php
$num_rows = mysql_num_rows($rs_settings);
if($num_rows > 0) { ?>
<?php while ($row_settings = mysql_fetch_array($rs_settings)) {?>
1a.question <span
class="required">*</span></td>
<select name="Applied_Elsewhere" id="Applied_Elsewhere" >
<option <? if ($row_settings[Applied_Elsewhere]=='Y') { ?> selected <? } ?>
value="Y">Yes </option>
option <? if ($row_settings[Applied_Elsewhere]=='N') { ?> selected <? } ?>
value="N">No </option>
</select>
Try changing <? to <?php and $row_settings[Applied_Elsewhere] to $row_settings['Applied_Elsewhere']
<select name="Applied_Elsewhere" id="Applied_Elsewhere" >
<option <?php if($row_settings['Applied_Elsewhere']=='Y') { echo "selected='selected'"; } ?> value="Y">Yes </option>
<option <?php if($row_settings['Applied_Elsewhere']=='N') { echo "selected='selected'"; } ?> value="N">No </option>
</select>
Please try this code:
<?php
$rs_settings = mysql_query("SELECT * from thesis WHERE user_id = $user_id;");
?>
<form action="academic.php" method="post" name="regForm" id="regForm" >
<?php
$num_rows = mysql_num_rows($rs_settings);
if($num_rows > 0) {
while ($row_settings = mysql_fetch_array($rs_settings)) {
1a.question <span
class="required">*</span></td>
print'<select name="Applied_Elsewhere" id="Applied_Elsewhere" >'
if ($row_settings['Applied_Elsewhere']=='Y')
print '<option value="Y" selected>Yes </option>';
else
if ($row_settings['Applied_Elsewhere']=='N')
print '<option value="N" selected>No</option>';
?>