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

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!

Related

How I can remove an Item for a select option html pressing a button with php

Im tryng to create a simple application that when you click on the send button it removes the
option that is selected. I only can use PHP and no JS.
<form action="prueba.php" method="POST">
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="Eliminar">
<form>
You can do this with some sort of data storage. In this case I'm using sessions, but you could use a database.
<?php
session_start();
if(!isset($_SESSION['removed_options'] )){
$_SESSION['removed_options'] = [];
}
if(isset($_POST['Eliminar'])){
$_SESSION['removed_options'][] = $_POST['cars'];
}
$options = ["volvo", "saab", "opel", "audi"];
?>
<form action="prueba.php" method="POST">
<select name="cars" id="cars">
<?php foreach(array_diff($options, $_SESSION['removed_options']) as $option): ?>
<option value="<?= $option ?>"><?= ucfirst($option) ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Eliminar" name="Eliminar">
<form>
You could also possibly store the options in a hidden field on the frontend now that I think about it.

How to get value from select option and not the contents - PHP

I am pretty new to PHP and have looked for a solution for what I'd have thought would be a common question but I can't find the answer. Any help would be greatly appreciated.
So I have a form with a selector and various options:
<form class="form-horizontal" id="main-home-form" action="trip_search_process.php" method="post">
<div class="form-group">
<label>Select location:</label>
<select class="form-control" id="location_selector" name="location">
<option value="default">I Don't Mind...</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="aus_nz">Australia & New Zealand</option>
<option value="europe">Europe</option>
<option value="north_america">North America</option>
<option value="south_america">South America</option>
</select>
When this form is submitted, I have this PHP code which extracts the location selected:
$location_passed = $_POST["location"];
However, what is passed is the contents of the 'option'. What I would like is the value. I.e. for this 'option':
<option value="aus_nz">Australia & New Zealand</option>
I currently get 'Austraia & New Zealand', whereas I want aus_nz
Is this possible? Or am I going about this the wrong way?
<?php
if(isset($_POST['re'])){
if($_POST['location'] == 'default'){
// error
echo '<p>make a selection</p>';
}else{
$val = $_POST['location'];
echo '<p> selected value; '.$val.'</p>';
}
}
?>
<form class="form-horizontal" id="main-home-form" action="" method="post">
<div class="form-group">
<label>Select location:</label>
<select class="form-control" id="location_selector" name="location">
<option value="default">I Dont Mind...</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="aus_nz">Australia & New Zealand</option>
<option value="europe">Europe</option>
<option value="north_america">North America</option>
<option value="south_america">South America</option>
</select><br>
<input type="submit" name="re" id="re" value="value.">
</div>
</form>
the above code will give the value of the selected option.
Put the php code to your action page trip_search_process.php
<?php
if(isset($_POST['submit'])){
echo $value = $_POST["make"];
echo $text = $_POST["make_text"];
}
?>
<form method=post>
<select name='make' onchange="setTextField(this)">
<option value = '' > Please Select Value </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<br/>
<input type=submit name=submit>
<input id="make_text" type = "hidden" name = "make_text" />
<script type="text/javascript">
function setTextField(ddl) {
document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
</form>

How do i get the select tag value with codeigniter?

how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');

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">

html select list value not passing on to PHP

I have a form in which values get inserted and passed to PHP, everything was working till I thought about inserting a select value, and this one always gets passed on to the database as 0. This is the form:
<form name="myForm" id="register" action="register.php" method="post" accept-charset="utf-8">
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
This is how I retrieve all my variables, but this select one doesn't seem to work.
PHP:
$value7 = mysql_real_escape_string($_POST['Year']);
Thank you!
Change this
<option value="year1">1</option>
to this
<option value="1">year1</option>
PHP Code
<?php
if(isset($_POST))
echo mysql_real_escape_string($_POST['Year']);
?>
HTML code
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
<input type='submit' >
</form>
change this
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
to
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>

Categories