How to receive the value in the form submit php - php

<select name="gender" id="Gender">
<option selected="selected" value="woman">女士</option>
<option value="man">先生</option>
</select>
the above code is in a form, when submit the form to test.php. how to get the selected value?女士 or 先生.
i know this can get.
if($_POST['gender']=='woman'){
$_POST['gender']="女士";
}else{
$_POST['gender']="先生";
}
is there a way to get the text. thank you

Use the option text as option value. Like this,
<option selected="selected" value="女士">女士</option>
<option value="先生">先生</option>
But keep in mind any one can change the gender and become something else.
<option value="transexual">transexual</option>
To prevent this its better you also maintain a white list in the server side code.
$gender == isset($_POST['gender'])? $_POST['gender']: "";
if(!in_array(array("女士", "先生"), $gender)){
$gender = "先生"; // set the default gender.
}

Related

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

change browser href in combobox option

I try to make a list of data from a table in a database. I do this with an select option, because the user is only allowed to choose one.
I do it on this way:
<select name="test">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php" onClick="window.location.href='other.php'">
Other
</option>
</select>
This works in IE and Firefox, but not in Google Chrome.
It's not an option to put the onClick in the <select>-tag, because they don't have a href and I need their values to get the primary key and store something in the table.
I hope you can help me.
UPDATE: this is the solution for my case, solved by Harry
<select name="test" onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
Try the below code and check if it solves your problem. You needn't have the href values in the select tag for this to work. The href can be in your option's value tag.
UPDATE: The below code would do the redirect only if the value of the selected option tag has .php in it. For the first two options, no action will be taken.
<select name="test"
onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
this isn't valid HTML. it should at least be:
<option value="other.php" onClick="window.location.href='other.php'">
try this
<select name="test"
onchange="var val = this.value;
if (val.indexOf('other.php')!=-1) window.location=loc">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>

jQuery change form action based on selected value

ive run into a quite tricky problem while using a method to set the form action when value changes. I have got the following code :
var actions = new Array();
actions[0] = "some-site.html";
actions[1] = "some-other-site.html";
actions[2] = "another-site.html";
etc....
$("select#feld-urlaubsart").change(function () {
if ($("select#fieldname").val() !== "") {
$("form#searchform").attr("action","http://www.mysite.com/" +
actions[$("select#fieldname").val()]);
}
});
On the other side, my select looks like this :
<select id="fieldname" name="fieldname">
<option value="0" selected="selected">All</option>
<optgroup label="Golfurlaub mit Greenfees">
<option value="1">Alle Greenfee-Angebote</option>
<option value="2">Top-Angebote</option>
<option value="3">Golfurlaub mit 4 Tage Greenfee</option>
</optgroup>
Actually it does work but i am not happy with it as i need to rely on the value="1" value="2" etc.
Now i need to use the actual description as value to be compatible with another script that i pass the value to for usage in PHP/JSON. I would need the data like this
<option value="All">All</option>
<option value="Option 1">Option 1</option>
etc
How can i change the action of the form based on the selected VALUE while not needing to use array keys like "1" "2" etc?
Thanks for any advice.
Use a json object:
actions = {"All":"some-site.html","Option1":"some-other-site.html"}
Then
actions["All"]
is
"some-site.html"

How to disable drop down list

I have this drop down list and my idea is when i click on videos that i disable other drop down list.This is not working.Do i need something else?
<select name="Type" id="type">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
<?php
$value=$_POST['image'];
?>
</select>
<select id="text-one" <?php if($value=="V"){ ?> disabled="disabled"<?php }?> >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select
You can do this with PHP, but that's going to require submitting form the data so you can then access $_POST, and then loading a new page, that's basically the same as the original, just with text-one disabled.
Remember, PHP runs server side, and once it's rendered in the browser, nothing else can be done via PHP. For example, you've got a line in your sample code that show's you tring to assign $_POST['image'] to $value - until you submit a form, $_POST will be empty.
Most likely, you want to do this client side and without a reload, and this can be done using javascript.
As a basic overview:
monitor the onChange event handler for your type select element
check the value of the type select element, and set the disabled attribute on the text-one as needed
Another option (maybe simpler?):
attach an onclick attribute to the video input, that will run a javascript function that disables text-one
jQuery will make some of this easier, but you could write all of the above in plain javascript, without any libraries.
try this code :
<script>
function disable(val)
{
if(val=="V")
document.getElementById("text-one").disabled=true;
else
document.getElementById("text-one").disabled=false;
}
</script>
<select name="Type" id="type" onchange="disable(this.value)">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
</select>
<select id="text-one" >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select>

How can I retain a option from a select field?

I have a <select>:
<select id="countries">
<option value="1">USA</option>
<option value="2">Spain</option>
</select>
The user selects an option, then he press a send button to make a query, thru PHP. The result of the query appears in the same page, so the page reloads.
How do i retain the selected option? when the page reloads??
I mean, if they select spain, how can I see spain again when the page reloads?
You need to first give your select dropdown a name.
For example:
<select id="countries" name="countries">
Then you will have access to the value in your PHP when the form is submitted. The value can be retrieved like this in PHP (after submit):
$countries = $_POST['countries'];
Then you can do something like #JohnConde did by setting the selected attribute with PHP.
Here's a very basic way to do it:
<option value="1"<?php if (1 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>USA</option>
<option value="2"<?php if (2 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>Spain</option>

Categories