php if statement from html option value error - php

I am trying to replicate the following:
php if statement html option value
and am following the directions as suggested:
Give name to your select.
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
In php
Example:
switch($_POST['selectedValue']) {
case 'Newest':
// do Something for Newest
break;
case 'Best Sellers':
// do Something for Best seller
break;
case 'Alphabetical':
// do Something for Alphabetical
break;
default:
// Something went wrong or form has been tampered.
}
However, I am getting the following error:
Notice: Undefined index: selectedValue in C:\xampp\trial.php on line 7
Please help

if you are sending form data then you have to set method post in your form tag or if you are using ajax send it by type post and in your php file put code inside if condition after form has been posted like isset($_POST['selectedValue'])
<form method="post" action="url to your php file">
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
<button type="submit">Submit</button>
</form>

That's happens because you don't always have the key selectedValue in your POST array.
if(isset($_POST['selectedValue'])){
// your code
}

My problem was solved after adding a submit button within the form tag of HTML
<form action="#" method="post">
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option
</select>
<input type="submit" style="float:left; margin-left: 2%;" name="submit" value="Search"/>
</form>

Related

Get selected option from html via php

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>

Sending value of a select tag to a PHP page as a POST variable

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'];
}
?>

How to return a URL and submit the form using onchange

Firstly, there may be a similar question answered amoungst the site but after carefully considering what solutions and problems I'm still stumped as to what to do.
I'm looking to return a URL and post my users to a page of the chosen URL from a select list.
Currently it works using the normal submit button using the following code;
<form id="formurl" name="formurl" method="post" action="" onSubmit="return getURL(this.url.value)">
<!-- begin postcode list -->
<select size="1" name="url" id="posctode_srch" onchange="this.form.submit()">
<option value="e1-office-space/">E1</option>
<option value="e7-office-space/">E7</option>
<option value="e10-office-space/">E10</option>
<option value="e11-office-space/">E11</option>
<option value="e14-office-space/">E14</option>
<option value="e15-office-space/">E15</option>
<option value="e16-office-space/">E16</option>
<option value="e17-office-space/">E17</option>
<option value="ec1-office-space/">EC1</option>
<option value="ec2-office-space/">EC2</option>
<option value="ec3-office-space/">EC3</option>
<option value="ec4-office-space/">EC4</option>
</select>
<input type="image" src="images/graphics/new/btn_go.png" class="go_btn" width="119" border="0" value="submit" onclick="_gaq.push(['_trackEvent', 'Contact', 'AreaSearchForm',,, false]);" alt="Area search submit" />
</form>
I'm looking to use post but I am testing it with get to see what result is created when choosing an option. when the form is set to get i'm seeing ?url=e7-office-space%2F so am I safe to assume it's working to some degree but there is something missing for the output to just take the value of the option and nothing more?
Being new to this kind of stuff i'm guessing it's a php / html problem as the functionality is there it's just a misused element somewhere so if anyone can help me see where i'm going wrong i'd really appreciate it.
Thanks
You have three things -
setting the url
setting the google stuff
submitting the form if not in the submit event
In the code below you can omit the image if you can rely on javascript only
DEMO
window.onload=function() {
document.getElementById("formurl").onsubmit=function() {
var val = this.url.value;
if (val=="") {
alert("Please select a value");
return false;
}
this.action="http://bing.com/search?q="+val;
_gaq.push(['_trackEvent', 'Contact', 'AreaSearchForm',,, false]);
}
document.getElementById("posctode_srch").onchange=function() {
var val = this.value;
if (val=="") return;
this.form.onsubmit(); // not 100% sure but this should not trigger twice
this.form.submit(); // since this is not triggering onsubmit
}
}
using
<form id="formurl" name="formurl" method="post" action="">
<!-- begin postcode list -->
<select size="1" name="url" id="posctode_srch">
<option value="">Please select</option>
<option value="e1-office-space/">E1</option>
<option value="e7-office-space/">E7</option>
<option value="e10-office-space/">E10</option>
<option value="e11-office-space/">E11</option>
<option value="e14-office-space/">E14</option>
<option value="e15-office-space/">E15</option>
<option value="e16-office-space/">E16</option>
<option value="e17-office-space/">E17</option>
<option value="ec1-office-space/">EC1</option>
<option value="ec2-office-space/">EC2</option>
<option value="ec3-office-space/">EC3</option>
<option value="ec4-office-space/">EC4</option>
</select>
<input type="image" src="images/graphics/new/btn_go.png"
class="go_btn" width="119" border="0" value="submit"
alt="Area search submit" />
</form>

Simple PHP select option programming

I have a simple problem in php select option.
The page has 5 select option & submit button. When I select a option then it should goes on specific web page. For Example: http://onlinetools.org/tricks/using_multiple_select.php
Then I select a option & press Send then It show Which option I select. But I need go specific webpage.
I have tried with this code but I have failed...
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
echo "
<script type=\"text/javascript\">
window.location = \"$test\"
</script>
";
?>
Anybody Can help me?
This should be a simple select not a multiple one since you want to redirect to only one site. here is the code :
<?php
$test=$_POST['test'];
if(isset($test)){
header("Location: $test");
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
As you've declared:
<select name="test[]" multiple="multiple">
test is an array, thus you have to take the first object of the array or remove the unused []. By the way, you can do an HTTP redirection, which is faster and works more often than javascript ones.

HTML form submit to PHP script

I am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

Categories