I have this select button:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
In the same page, few lines bellow, I have:
<div id="challenge">
<?php
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
?>
</div>
But when I change the option, it didn't change the post value.
$Ch = $_POST['challengeX'];
won't get value until you submit the form. Add a submit button to your form.
<form method="post" action="">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
<input type="submit" value="Submit">
</form>
And access the form values in the same page only if the form is submitted.
<div id="challenge">
<?php
if(isset($_POST)) // checks whether any value is posted
{
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
}
?>
</div>
you need to submit form on change of dropdown.
Change html like this:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;" onchange="this.form.submit()">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
for your php execution you can try like this:
<div id="challenge">
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
function showSomething($Ch,1,1){
//do your stuff
}
}
?>
</div>
PHP runs on the server and delivers HTML (or other data) to the browser.
If you want to process the form data with PHP then you must include it in a new HTTP request (typically by submitting the form) and have PHP generate a new page using that data.
Alternatively, if you want to process the data on the client then you will need to use a programming language that is supported in the browser. Unless you want to start using plugins (or browser specific technologies), that limits you to JavaScript. You can bind a change event listener to the select element and then manipulate the DOM with the results.
In the way you can use Javascript. If you dont want to use any server like PHP or something
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<input type="submit" /> /* add this line */
</label>
</form>
By Javascript
<select id="label" name="challengeX" style="margin-top:150px;" onChange="getSelect(this)">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<div id="resultSelect"></div>
<script>
function getSelect(thisValue){
document.getElementById('resultSelect').innerHTML=thisValue.options[thisValue.selectedIndex].text;
}
</script>
Related
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');
I have a form with two select field. The select use onChange="form1.submit()" to submit form. And how I know what select field in the form is selected by user? I have tried to do this with two Submit Button and I can know what button have submitted easily.
<?php
if(isset($_POST["select"])) {
echo "submit";
}
if(isset($_POST["select2"])) {
echo "submit2";
}
?>
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="1">Select a</option>
<option value="1">Select b</option>
</select>
<select name="select2" id="select" onChange="this.form.submit()">
<option value="2">Select2 a</option>
<option value="2">Select2 b</option>
</select>
</form>
Both the <select> values are sent to the server. What you need to check is for their emptiness. So kindly change your code to:
<?php
if(!empty($_POST["select"])) {
echo "submit";
}
if(!empty($_POST["select2"])) {
echo "submit2";
}
?>
I do believe you should change the value of the select too, here:
Select a
Select b
Select2 a
Select2 b
Generally, to check what's happening / getting posted to the code, you can do it in two ways:
PHP Way
Use var_dump() to check what has been submitted. The best one would be:
var_dump($_REQUEST);
var_dump($_POST);
var_dump($_GET);
Network Tab
If you are using Chrome or Firebug, you can use the Developer Tools' Network tab to check the FORM Data.
Firebug
Chrome
Unrelated Note: You have <select> tags with same id, which is totally wrong, but that doesn't affect it. Checking using the PHP way, will work for you.
You can use a default value, such as an empty string:
<?php
if(!empty($_POST["select"])) {
echo "submit";
} elseif(!empty($_POST["select2"])) {
echo "submit2";
}
?>
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="">Choose</option>
<option value="1a">Select a</option>
<option value="1b">Select b</option>
</select>
<select name="select2" id="select2" onChange="this.form.submit()">
<option value="">Choose</option>
<option value="2a">Select2 a</option>
<option value="2b">Select2 b</option>
</select>
</form>
Try This:
Your HTML (Adding one more option with 0 value):
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="0">Select</option>
<option value="1">Select a</option>
<option value="1">Select b</option>
</select>
<select name="select2" id="select" onChange="this.form.submit()">
<option value="0">Select</option>
<option value="2">Select2 a</option>
<option value="2">Select2 b</option>
</select>
</form>
Your PHP Code:
if($_POST['select'] > 0){
echo "submit";
}
if($_POST['select2'] > 0){
echo "submit2";
}
i am trying to add a text field using prestashop CMS option, i can easily save drop downs that i have created, but when i try to save the text field it gives me this error
The content field is invalid..
Following is my HTML Block.
<h1>Sell your item</h1>
<p>Select Category:</p>
<select class="selectBox selectBox-dropdown selectProductSort" name="catgeory_dropdown">
<option value="">-- Select Category</option>
<option value="category1">Category 1</option>
<option value="category2">Category 2</option>
<option value="category3">category 3</option>
</select>
<p>
<input id="item_name" class="search_query ac_input" autocomplete="off" name="item_name" type="text" /></p>
<p>Select Category:</p>
<select class="selectBox selectBox-dropdown selectProductSort" name="catgeory_dropdown">
<option value="">-- Select Category</option>
<option value="category1">Category 1</option>
<option value="category2">Category 2</option>
<option value="category3">category 3</option>
</select>
It happened to me the same error message when I tried to put input fields in contents.
i have a simple drop down select menu.
<div id="select">
<select class="select">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
How do i take the value that the user selects and store it into a php variable?
in somephpfile.php
$selected = $_POST['somename'];
html
<form action="somephpfile.php" method="post">
<div id="select">
<select class="select">
<select name="somename"> <!-- you missed this -->
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</form>
you missed the "name" attribut in the select and the form tag. Try this:
<HTML><BODY>
<?PHP
$sel_year= $_POST['select'];
echo $sel_year
?>
<FORM method="post" action="...your-php-file-name-here...">
<div id="select">
<select name="select">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</FORM>
</BODY></HTML>
<form action="" method="post">
<div id="select">
<select class="select" name="selectoptionname">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</form>
when form submits you can get the value of selected option using $_POST['selectoptionname']
put it in a form , then use $_POST['select']
I run a simple form collecting 4 values, three of the values are listed in a dropdown. I'm wondering if I can write the values selected by a user to my mysql table prior to submitting the form.
The form looks like this:
<form id="search" action="" method="POST">
<input id="field1" type="text" name="to" value="">
<select name="day">
<option value="">Day</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
</select>
<select name="cym">
<option selected="" value="">Month</option>
<option value="2012-12">December 2012</option>
<option value="2013-01">January 2013</option>
<option value="2013-02">February 2013</option>
</select>
<select name="nights">
<option value="">Nights</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
</select>
<button onclick="document.getElementById('search').submit();">Search!</button>`
So, once the user selects the 'day', I want to write the value to the mysql, once the user select the 'cym' I want to write the value to the mysql and so on.
Can this be realized and if so how?
If you're using jQuery you can do something like this:
$("#cym").change(function () {
$.ajax({
url: 'serverUrl.php',
success: function(data) {
// nothing to be done here
}
});
});
The <select name="cym"> would become <select id="cym"> for the selector $("#cym") to work. This, of course, can be achieved by using raw Javascript and if that's your preferred approach you can have a look at http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first .
You can bind some onChange events to the select elements. You can easily use jQuery for that. Look at http://api.jquery.com/change/