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'];
?>
Related
I have a select field and i am using my option value as a variable for some further work but i would like to create a second variable for the name of my option to use elsewhere.
This is my form
<form action="second.php" method="post" class="form">
Client:<select class="default" id="client" name="client" required>
<option value="" selected>Select Client</option>
<option value="clienta" name"Client A">Client A</option>
<option value="clientb" name "Client B">Client B</option>
</select>
</form>
In my second.php file i am getting the value for client like this:
$client = $_POST["client"];
Im not sure how to get the name for client instead of value.
i have tried:
$client_name = $_POST["client.name"];
$client_name = $_POST["client[name]"];
$client_name = $_POST["client(name)"];
But none of these work, they all come back blank when i want to return the name of the option e.g.
"Client A" instead of "clienta"
Does anyone know how i could get to the name field instead of the value from my select?
If you Need Both value and Name. the best way to archive is by combining them in value
<?php
if (isset($_POST['client']))
{
$pair = explode("/", $_POST["client"]);
$name = $pair[0];
$value = $pair[1];
print "Key: $name<br />";
print "Value: $value<br />";
}
?>
<form method="post" action="" name="form">
<select name="client">
<option value="clienta/Client A">Client A</option>
<option value="clientb/Client B">Client B</option>
</select>
<input name="submit" type="submit">
</form>
You don't need add attribute name <option> element:
<form action="second.php" method="post" class="form">
Client:<select multiple="multiple" class="default" id="client" name="client[]" required>
<option value="" selected>Select Client</option>
<option value="ClientA">Client A</option>
<option value="ClientB">Client B</option>
</select>
</form>
In php:
$clients = $_POST['client']; // will be array with selected options
Use Like Below Code
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<form class="form-horizontal" action="#">
<div class="form-group">
<label class="control-label col-sm-2" for="client">Client:</label>
<div class="col-sm-6">
<select class="form-control" id="client" name="client" required>
<option value="" selected>Select Client</option>
<option value="clienta" name="Client A">Client A</option>
<option value="clientb" name="Client B">Client B</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="button" class="btn btn-default" id="submit_btn" name="submit_btn" value="Submit">
</div>
</div>
</form>
</body>
</html>
<script>
$('#submit_btn').on('click', function(){
var option_value = $('#client').val();
var option_name = $('#client').find('option:selected').attr('name');
$.ajax({
type:"post",
url:"second.php",
data:"option_value="+option_value+"&option_name="+option_name,
success: function(data){
console.log(data);
}
});
});
</script>
second.php
<?php
echo $option_value = $_POST['option_value'];
echo $option_name = $_POST['option_name'];
?>
<form action="second.php" method="post" class="form">
Client:<select class="default" id="client" data-name="client" required>
<option value="" selected>Select Client</option>
<option value="clienta" data-name="Client A">Client A</option>
<option value="clientb" data-name="Client B">Client B</option>
<input type="button" id ="button" value="send">
$(document).ready(function(){
$('#button').click(function(){
$.ajax({
type: 'POST',
url: 'second.php',
data: {'clinet_val':$('#client').val(),'clinet_name':$('#client').find(':selected').data('name')},
success:function(response)
{
console.log(response);
}
});
});
});
I have a 'this year' dropdown with a form attached. I then select one of the years, like say 2016. When I click the submit button I want the page to be reloaded while the value in the dropdown stays at 2016 (the one that was previously selected).
How can I do that? Here's my form:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){?>
<option value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>
You should use the value from $_POST to set the selected as default. Below is the updated code:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){
$isSelected = (isset($_POST['year']) && ($_POST['year'] == $rows->years)) ? 'selected' : '';
?>
<option <?php echo $isSelected; ?> value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>
I have the following form but would like to add a condition so that only managers can see 'clear' option.
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<option value='clear'>Remove the ticket</option>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
Something like: if(havePriv('grp_mgr'))
<?php if(havePriv('grp_mgr')) : ?>
<option value='clear'>Remove the ticket</option>
<?php endif; ?>
Does the trick
You can do this:
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<?php if(havePriv('grp_mgr')){ ?>
<option value='clear'>Remove the ticket</option>
<?php } ?>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
So "Remove the ticket" will only displayed if the condition is true.
You can say :
if(havePriv('grp_mgr'))
{
echo "<option value='clear'>Remove the ticket</option>";
}
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 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>