So I'm trying to get the value of my select element. I've tried testing to see if my php file receives my value, but it doesn't seem to be working.
Here is my php code:
<?php
if (isset($_POST['okButton'])){
$value = $_POST['allCategories1'];
}
?>
And here is my HTML-code:
<form action="index.html" method="post">
<select id="allCategories1">
<option name ="All_categories" value="All_categories">All categories</option>
<option value="No_category">No category</option>
<option value="Shooter_games">Shooter games</option>
<option value="Family_games">Family games</option>
<option value="Action_games">Action games</option>
<option value="Sport _games">Sport games</option>
</select>
<button type="submit" name="okButton" id="okButton">Ok</button>
</form>
I don't know why, but it's not giving me the value of all categories.
Anyone know what I did wrong?
1.Try to put name attribute to select element like below:-
<select id="allCategories1" name="allCategories1">
Now you will get the values.thanks.
2.<form action="index.html" method="post"> action must have some php file name otherwise your php code on that page will not work.
"name" attributes should come in tags like input, select. But you've added "name" attribute at "option". Thats the mistake you've done.
Make your html dropdown box like this one:
<select id="allCategories1" name="allCategories1">
Form actions should have a file with .php extensions.
But you've kept in .html file. You've to change file also to get this work.
just try like:
You have missed the index name i.e thats why you got the error like this.
Notice: Undefined index: allCategories1 .
Just put the name="allCategories1" in .
and here two options you have.
1.remove .html extention and put it .php file as index file.
2.just wrote php code and html code in one file and save entire file as a .php file.
Code:-
<?php
if (isset($_POST['okButton'])){
$value = $_POST['allCategories1'];
echo $value;
}
?>
<form action="" method="post">
<select id="allCategories1" name="allCategories1">
<option name ="All_categories" value="All_categories">All categories</option>
<option value="No_category">No category</option>
<option value="Shooter_games">Shooter games</option>
<option value="Family_games">Family games</option>
<option value="Action_games">Action games</option>
<option value="Sport _games">Sport games</option>
</select>
<button type="submit" name="okButton" id="okButton">Ok</button>
</form>
Output:-
For output just Click Here
Your form action in incorrect. It should be to a .php file.
<form action="my_file_name.php" method="post">
If you are posting to the same file, save it as .php (index.php in your case ) and then form action should be changed to
<form action= "" method="post">
Also change the statement
<select id="allCategories1"> to <select name="allCategories1">
`
Related
I have a simple simple question, And is so strange for me.
In my HTML file I have a button and a form tag in which used select tag with some options as :
<form action="" method="post" name="general"> Wireless mode
<select name="wirelessmode" id="wirelessmode">
<option value="ap">AP</option>
<option value="client">Client</option>
<option value="clientbrdige">Client Bridge(Routed)</option>
<option value="adhoc">Adhoc</option>
<option value="wdsstation">WDS Station</option>
<option value="wdsap">WDS AP</option>
</select> <br>
</form>
<form action="" method="post" name="apply">
<input type="submit" name="apply" value="Apply"/>
</form>
Include that HTML file in my Php file, create a class and a function to display the selected option of the HTML as below :
<?php
include('./view/config.html');
class SSHCommand{
public function display(){
if (isset($_POST['apply'])) // press button {
$category = $_POST['wirelessmode']; // get the select option
echo $category;
}
}
}
$sshCommand=new SSHCommand();
$sshCommand->display();
?>
When i try, it gives me nothing !
But when i try :
echo 'david';
instead of
echo $category;
It prints david after press apply button.
Where I am doing wrong?
you can get a result using single FORM, modified your HTML code as per bellow.
<form action="" method="post" name="general" id="general_form"> Wireless mode
<select name="wirelessmode" id="wirelessmode">
<option value="ap">AP</option>
<option value="client">Client</option>
<option value="clientbrdige">Client Bridge(Routed)</option>
<option value="adhoc">Adhoc</option>
<option value="wdsstation">WDS Station</option>
<option value="wdsap">WDS AP</option>
</select>
<input type="submit" name="apply" value="Apply"/>
</form>
You would have to include your config.html as html with a php extension .. config.php for the html include to work.. that would be a key thing.
You can include HTML or text in a PHP include file. Anything that can go in an standard HTML file can go in a PHP include.
Your entire website should be subsequently saved using .php extensions however, whether they include php or not, that's the downside, eg. index.php rather than index.html, so it could be time-consuming. Some servers don't require this, so test your configuration first.
Best of luck
Your select is in one form block and the submit button is in another. The default nature of the submit button will submit only the html elements in that form and not all forms of the page. So to get this to work, you need to keep one form with the required elements as
<form action="" method="post" name="general"> Wireless mode
<select name="wirelessmode" id="wirelessmode">
<option value="ap">AP</option>
<option value="client">Client</option>
<option value="clientbrdige">Client Bridge(Routed)</option>
<option value="adhoc">Adhoc</option>
<option value="wdsstation">WDS Station</option>
<option value="wdsap">WDS AP</option>
</select> <br>
<input type="submit" name="apply" value="Apply"/>
</form>
I am new to php and want to make a dropdown menu where you can select a site that you want, then when you click a button you will be redirected to that site. The button will take you to for example "google.com" if that is what you select, if you select "stackoverflow.com" the same button will take you there. I currently have no working php code as I am not sure where to start. I will include the html code below.
<form method="post" action="testttt.php">
<select id="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
The form ends a bit further down, so please don't complain about there being no :P ANY HELP IS APPRECIATED :)
Try the below code , i have done the example which you want and its work perfectly . You can do it this in single file as i have done .
<?php
if(isset($_POST['btnsubmit']))
{
$options = $_POST["testing"];
header('Location: '.$options.'');
echo "Your option value".$options;
}
?>
<form method="post" action="#">
<select id="mySelect" name="testing" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="http://www.google.com"> Itslearning
<option value="http://www.stackoverflow.com"> NDLA
</select>
<input type="submit" name="btnsubmit" value="GO"/>
</form>
testttt.php will receive values in the $_POST array, so things like 'echo $_POST["somefield"];' will get you data...
...BUT you have to name your form elements. Not '<select id="somefield"...' but '<select name="somefield"...'. you can still have id attributes if you want, but pass to $_POST happens by what you put in the name= attribute.
Once you know the URL you're redirecting to, header("Location: $url");
Hope that gives you the push you need :-)
can you not just use the myfunction function like this:
function myfunction(event){
return window.open(event.target.options[event.target.options.selectedIndex].value,'');
}
If you want to do it in php, your select need a name attribute :
<form method="post" action="testttt.php">
<select id="mySelect" name="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
</form>
You can do that either in PHP :
if(isset($_POST['mySelect']) && !empty($_POST['mySelect'])){
header('Location: ' . $_POST['mySelect']);
}
Or in Javascript using the onchange attribute you already set up :
myFunction(){
var target = event.target;
document.location.href(target.options[target.options.selectedIndex].value);
}
so I'm having problems with forms sending data to my php script.
My html is:
<form method="get" action="form.php">
<select name="course">
<option value"COMM1">COMM1111</option>
<option value"COMM2">COMM2222</option>
<option value"COMM3">COMM3333</option>
<option value"COMM4">COMM4444</option>
</select>
</form>
What's sent to form.php when I do
$_GET['course']
is the inner text of the chosen option.
So choosing COMM1111 gives me COMM1111 instead of COMM1
In your option tag you forgot your equal sign for value
<option value = "COMM1">COMM1111</option>
You are missing the = sign in your HTML for the value of the options.
Consider the following code which produces the values you are looking for ::
HTML:
<form method="get">
<select name="course">
<option value="COMM1">COMM1111</option>
<option value="COMM2">COMM2222</option>
<option value="COMM3">COMM3333</option>
<option value="COMM4">COMM4444</option>
</select>
<input type='submit'>
</form>
PHP:
<?php
if(!empty($_GET)){
print_r($_GET);
}
?>
I am using multiple select elements on a page. This page's url is something like this: "index.php?s=foo".
Now I want that changing my select boxes will alter my URL and refresh page, this just works partly :(
Here is the example:
<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">
<select name="this" onchange="this.form.submit()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>
So if I change my selection the url will swap to "index.php?this=1", but I would like to hold the previous _GET Parameters, so change would occur to "index.php?s=foo&this=1".
Any chance I would get that?
Thanks very much for your help.
Best Regards
Using REQUEST_URI does not include your current GET parameters. You will need the function in this gist to transform all parameters into input fields: https://gist.github.com/eric1234/5802030 .
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="get">
<?php array_to_input($_GET); ?>
<select name="this" onchange="this.form.submit()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>
CAUTION: array_to_input can introduce XSS vulnerabilities. Please escape contents of the $_GET variable so this cannot happen.
You could add each query string item as a hidden field which will be added to the query string when the form is submitted. Be sure to do some sanitizing on these things to make them safe.
if(!empty($_GET))
{
foreach($_GET AS $k => $v)
{
echo '<input type="hidden" name="'.$k.'" value="'.$v.'">';
}
}
The form action can then be hard coded to something like the file name or $_SERVER['SCRIPT_NAME']. Not $_SERVER['PHP_SELF'] - as pointed out by comment below.
edit : posted answer before I was finished. :(
The simple answer is instead of this:
<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">
Do the action like this:
<form action="<?php echo $_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING']; ?>" method="GET">
Which will include the current $_GET values in your form's action/url and by extension be present when the form is submitted. Basically that is the way forms are designed in general to handle submission to a page with query parameters, ie. the default way it works.
Just a note but I think the way you have it now you might get this for your action/url
"http://'.$_SERVER['REQUEST_URI'].'"
It's just a guess, but it looks like the form is html, and the php will just print and not be executed as code ( without php tags ). Probably just an over-site in the example code. But, I would expect this with the format you have:
echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">';
Cheers!
I have a select tag with different values as shown below.
I want to send the selected value to a PHP page as a POST variable or any other way.
Is this possible?
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
Put your select inside a form. Give the form an action that is a php file. give it the method of post and include a <input type="submit" value="Submit"> inside the form as well.
Then, in your php file you will access the $_POST array
echo $_POST['city'];
If you'd like to submit it without the button, you'll need to use javascipt's onChange event.
Yes, and it's very simple. You have to use the from tag : http://www.w3schools.com/html/html_forms.asp and learn a little of PHP and HTML basic interaction
<form action = 'myPage.php' method = 'post'>
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
<input type = 'submit' name = 'send' value = 'send'/>
</form>
(I also added a button son send data)
in myPage.php :
<?php
if(isset($_POST['city'])){
echo isset($_POST['city'];
}
?>