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>
Related
I am trying to make a dropdown list and take three parameters from the user which will be stored in a php file for later use. My current code is :
<body>
<header>
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First">
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Second</label>
<select name="Second">
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Third</label>
<select name="Third">
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
</form>
</header>
</body>
Here, the problem is, I don't want submit button for each parameter, instead there should be only one button for all the parameters. I have tried several things but none is working.
My php file
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
Where am I going wrong?
EDIT : Corrected the php code
Just wrap all your selects under one form instead of 3 different forms.
Make sure that the input submit is not inside the select tag.
Also, note that your php code is trying to echo parameters that don't exist in the html you shared. The name of your select will be the parameter name.
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
you can add all there in one form
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
<input type="submit" name="submit" value="submit">
</form>
in your code you have missed button "name".
parameter.php code should be this.
if(isset($_POST['submit'])){
echo $First = $_POST['First'];
echo $Second = $_POST['Second'];
echo $Third = $_POST['Third'];
}
<form action="parameter.php" method="post" id="frm">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
JQuery AJAX
<script>
$('#frm').on('submit', function(e){
e.preventDefault();
$.ajax({
url : $(this).attr('action'),
type: "POST",
dataType: "json",
data : $(this).serialize(),
success: function(data)
{
console.log(data)
}
});
});
</script>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
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.
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">
I thinks this is pretty easy but yet can't do it for the sake of my life. Is this too much to ask for?
<select name="cars">
<?php
if (isset($_POST['Submit1'])) {
$car = $_POST['cars'];
}
?>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="">
<Input Type = "text" Value ="<?php echo $car; ?>" Name ="word">
<Input Type = "Submit" Name = "Submit1" Value = "Submit">
</FORM>
The <select> tag is an HTML form element and in order to be submitted that tag would have to be inside the <form> tag.
Your new code should look like this:
<body>
<form name="form1" method="POST" action="">
<select name="cars">
<?php
if (isset($_POST['Submit1'])) {
$car = $_POST['cars'];
}
?>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="text" value="<?php echo $car; ?>" name="word" />
<input type="Submit" name="Submit1" value="Submit" />
</form>
</body>
Let me know if you have any other questions.
I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.
If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …
Then you can acces the array in your PHP script
<?php
header("Content-Type: text/plain");
foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";
$_GET may be substituted by $_POST depending on the <form method="…" value.
Change:
<select name="select2" ...
To:
<select name="select2[]" ...
You can use this code to retrieve values from multiple select combo box
HTML:
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
PHP:
<?php
$values = $_POST['ary'];
foreach ($values as $a){
echo $a;
}
?>
Use the following program for select the multiple values from select box.
multi.php
<?php
print <<<_HTML_
<html>
<body>
<form method="post" action="value.php">
<select name="flower[ ]" multiple>
<option value="flower">FLOWER</option>
<option value="rose">ROSE</option>
<option value="lilly">LILLY</option>
<option value="jasmine">JASMINE</option>
<option value="lotus">LOTUS</option>
<option value="tulips">TULIPS</option>
</select>
<input type="submit" name="submit" value=Submit>
</form>
</body>
</html>
_HTML_
?>
value.php
<?php
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
You can iterate it directly like this
foreach ($_GET['select2'] as $value)
echo $value."\n";
or you can do it like this
$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";
This will display the selected values:
<?php
if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}
?>
// CHANGE name="select2" TO name="select2[]" THEN
<?php
$mySelection = $_GET['select2'];
$nSelection = count($MySelection);
for($i=0; $i < $nSelection; $i++)
{
$numberVal = $MySelection[$i];
if ($numberVal == "11"){
echo("Eleven");
}
else if ($numberVal == "12"){
echo("Twelve");
}
...
...
}
?>
You could do like this too. It worked out for me.
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHP code below. It print the selected multiple values accordingly.
$shift=$_POST['selectDuration'];
print_r($shift);
I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:
for(i=0; i < form.select.options.length; i++)
if (form.select.options[i].selected)
form.hidden.value += form.select.options[i].value;
Next, i get by post that field and get all the string ;-)
I hope it'll be work for somebody more. Thanks to all.
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
form submit with enctype="multipart/form-data"