How can I echo out the users dropdown selection? - php

I'm trying to echo to the screen what a user selects from a dropdown menu. So whenever the user selects a number, it updates the text below it in to display what they selected. Here's my code below:
<html>
<head>
</head>
<body>
<label for="numbers"></label>
<form action="drop.php">
<select name="numbers">
<option value="0">select number</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br><br>
<input type="submit">
</form>
<?php
if(isset($_POST['numbers']))
{
$numbers = $_POST['numbers'];
$error = "?";
echo "<p>" . $numbers . "</p>";
}
?>
</body>
</html>
UPDATED MAIN CODE

Try method="post" on form declaration. or use $_GET instead of $_POST ON drop.php
if(isset($_POST['numbers']))

I suggest reading PHP's Dealing with Forms tutorial and also researching the change event.
For your code example, I've added a form element along with an "onchange" attribute to the select element which will automatically submit the form when a number is chosen.
<html>
<head>
</head>
<body>
<form action="<?php echo basename(__FILE__); ?>" method="post">
<label for="numbers">Numbers:</label>
<select name="numbers" onchange="document.forms[0].submit();">
<option value="0">select number</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit">
</form>
<?php
if (array_key_exists('numbers', $_POST)) {
$numbers = $_POST['numbers'];
echo '<p>' . $numbers . '</p>';
}
?>
</body>
</html>

Add method="post" to the form
<form action="drop.php" method="post">

Related

Dropdown value not populating in php on change

I want to pass Drop Down value to php, i am new to PHP please help
<html>
<head>
<script>
function displayVals() {
var singleValues = $("#account").val();
$("p").html("<b>Single:</b> " + singleValues);
}
</script>
<select id="account" name="account">
<option value="1">Tes1</option>
<option value="2">Tes2</option>
</select>
</head>
<body>
<?php
$account = $_get['account'];
echo $account;
?>
</body>
</html>
Hi, I want to pass Drop Down value to php, i am new to PHP please help
<?php
if (isset($_POST['submit'])) {
echo $_POST['account'];
}
?>
<form action="" method="POST">
<select id="account" name="account">
<option value="1">Tes1</option>
<option value="2">Tes2</option>
</select>
<input type="submit" name="submit"/>
</form>
You need to make a form
Here is the solution for you,
<?php
if (isset($_REQUEST['save'])) {
$sql = "SELECT Business_Unit, COUNT(Joining_Month) AS 'A' FROM jl WHERE Business_Unit = '".$_REQUEST['opt']."' GROUP BY Business_Unit";
// Here is your sql query
echo $sql;exit();
}
?>
Yor HTML code:
<form method="post" action="">
<select name="opt">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<input type="submit" name="save" />
</form>
Given simple working demo to fulfill your requirement. Hope it will help you.

how to connect drop down list with button and php file with if statement

I'm new in php, and I need help. I have created drop down list, than I should hit submit button and it should run one of my php files that are in drop down list. (using if statement) I tried in this way:
<p>
What Genre you want?
<select name="Ganre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<?php if (Genre == "FPS"): ?>
<FORM METHOD="LINK" ACTION="FPS.php">
<INPUT TYPE="submit" VALUE="Submit">
</FORM>;
</p>
Use your code like this:
<?php if (Genre == "FPS") { ?>
<form method="get" action="FPS.php">
<select name="Genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
</form>
<?php } ?>
You can get values in PHP by using PHP superglobal (POST/GET):
<?php
if(isset($_GET['Ganre'])){
echo $_GET['Ganre'];
// use your stuff that you want in IF condition.
}
?>
And if you need values in URL as sub string than you need to replace form method with $_GET:
Change:
<FORM METHOD="LINK" ACTION="FPS.php">
With:
<form method="get" action="yourfilename.php">

PHP: Can't get the selected value of a combobox

I'm trying to get the selected value of a combobox in php but the only result I get is "1" no matter what value is selected.
Here is my html code:
<form name="form" method="POST" action="">
<select class="dropdown-select" name="dayscombo" id="dayscombo">
<option value="1">7</option>
<option value="2">30</option>
<option value="3">90</option>
<option value="4">365</option>
</select>
</form>
I'm trying to echo the selected value using:
$mydays=$_POST['dayscombo'];
echo "$mydays";
Thanks!
set your value equal to your text like <option value="7">7</option>
Are you using jQuery? Where is the send button? Try it:
//send.php
<form name="form" method="POST" action="posted.php">
<p>
<select class="dropdown-select" name="dayscombo" id="dayscombo">
<option value="1">7</option>
<option value="2">30</option>
<option value="3">90</option>
<option value="4">365</option>
</select>
</p>
<p>
<button type="submit">POST</button>
</p>
</form>
//posted.php
<?php
$mydays = $_POST['dayscombo'];
echo "$mydays";
?>
Running link: http://missoesrestauradoras.com.br/send.php
Best Regards!

unable to pass multiple listbox items to php [duplicate]

This question already has answers here:
How to get multiple selected values of select box in php?
(11 answers)
Closed 8 years ago.
i need to store multiple elements of listbox item into the database , but i am unable to pass the elements from the html form to php. Please help
<html>
<head>
</head>
<body>
<form method="post" action="Save.php">
<select name="country[]" multiple="multiple">
<option value="Belgium">Belgium</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Holland">Holland</option>
<option value="Greece">Greece</option>
</select>
<input type="Submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$con = $_POST['country'];
foreach($con as $selected)
{
echo 'selected'.$selected;
}
?>
This code shall solve your issue
<?php
if(isset($_POST['submit']))
{
if (isset($_POST['country'])){
$con = $_POST['country'];
foreach($con as $selected)
echo 'selected'.$selected;
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="Save.php">
<select name="country[]" multiple="multiple">
<option value="Belgium">Belgium</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Holland">Holland</option>
<option value="Greece">Greece</option>
</select>
<input type="Submit" value="Submit" name="submit" />
</form>
</body>
</html>
You don't have a POST index of submit hence your isset() is failing. You are also missing a closing brace for your if statement in your code example. I've modified your example to use the $_POST['country'] for the isset(). This bit worked for me as far as returning selected values goes:
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="country[]" multiple="multiple">
<option value="Belgium">Belgium</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Holland">Holland</option>
<option value="Greece">Greece</option>
</select>
<input type="Submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['country']))
{
$con = $_POST['country'];
foreach($con as $selected)
{
echo 'selected'.$selected;
}
}
?>
//the post request takes the Tag NAMES only
<input type="Submit" value="Submit" name="submit"/>
see this phpfiddle it is working fine only

PHP Submit on Select

I am trying to not have a submit button, is there any way that the form can submit when the user selects a value?
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
<table class="form">
<select name="category" class="formfield" id="category">
<option value="-1"> Category </option>
<?php
$sql_contry = "SELECT * FROM category";
$rs_c = mysql_query($sql_contry);
while ($row_c = mysql_fetch_array($rs_c)) {
echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>';
}
?>
</select>
</table>
</form>
Here an example
<form>
<select name='myfield' onchange='this.form.submit()'>
<option selected>Milk</option>
<option>Coffee</option>
<option>Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Using noscript tag it allows browsers that are not JavaScript-enabled to still function.
Yes - use javascript:
Html:
<form id="frm">
<select onchange="onSelectChange();">
<option>1</option>
<option>1</option>
<select>
</form>
js:
function onSelectChange(){
document.getElementById('frm').submit();
}
<form action="product.php" method="POST">
<select onchange="this.form.submit();" name="prod">
<option value="">Select product</option>
<option value="1">abc</option>
<option value="2">def</option>
<option value="3">ghi</option>
<option value="4">jkl</option>
<option value="5">mno</option>
</select>
Just change code as below, I haven't check code so there may be some error like capital/small letter I am not sure like, it's submit() or Submit() and so on..
<script language="javascript">
function submitForm(){
var val = document.myform.category.value;
if(val!=-1){
document.myform.submit();
}
}
</script>
<form method="post" name="myform" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
<table class="form">
<select name="category" class="formfield" id="category" onchange="submitForm();">
<option value="-1"> Category </option>
<?php
$sql_contry = "SELECT * FROM category";
$rs_c = mysql_query($sql_contry);
while ($row_c = mysql_fetch_array($rs_c)) {
echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>';
}
?>
</select>
</table>
</form>

Categories