How to change content depending on a <select>? - php

I need help on 2 things since I'm not savy in PHP:
Content that changes depending on the:
<select>
<option>Option 1</option>
<option>Option 2</option>
</select>
I think this can be done using PHP, by giving names to the options, having PHP get the names, creating variables depending on those names and putting an "include" that shows the changing content.
Here's the model for the script:
READ Select
IF User selects "variable1, varialbe2,etc"
THEN Display "page1,page2,page3"
*The page changes within the page so that if the user wants to change it again they dont have to go back...Maybe this can be done by using:
<?php include 'page1.php'; ?>
That content will include a button, and depending on that button, I want a text box to be filled with certain text.
<input type="button" value="">
Then Depending on the value of that button the PHP will put text into a textbox.
From what I've read this will be using $request and $post ...
(This of course is separate PHP)

There may be a couple of different ways to accomplish your goal. If we stick to using PHP as you've requested then something like this is probably best. I've used a case/switch here for security purposes and a POST instead of a GET to prevent URL manipulation although with the case/switch you have addressed a lot of URL manipulation anyway so it becomes a matter of preference for you.
<?php
$page = null;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
switch($page){
case 'page3': include_once('/path/to/page3content.php'); break;
case 'page2': include_once('/path/to/page2content.php'); break;
case 'page1': include_once('/path/to/page1content.php'); break;
default: include_once('/path/to/defaultcontent.php'); break;
}
?>
<form name="myform" action="" method="post">
<select name="page" onchange="this.form.submit()">
<option value="page1"<?php if($page == "page1"){ echo " selected"; }?>>Page 1</option>
<option value="page2"<?php if($page == "page2"){ echo " selected"; }?>>Page 2</option>
<option value="page3"<?php if($page == "page3"){ echo " selected"; }?>>Page 3</option>
</select>
</form>
However, depending on your file structure, you can accomplish something similar even easer with just a little basic Javascript and HTML.
<select name="page" onchange="window.location=this.value">
<option value="/path/to/page1.php">Page 1</option>
<option value="/path/to/page2.php">Page 2</option>
<option value="/path/to/page3.php">Page 3</option>
</select>

You need to have a name field in your select tag, and value fields in your option tags:
<form name='myform' action='myform.php' method='POST'>
<select name='myselect'>
<option value='1'>Option 1</option>
<option value='2'>Option 2</option>
<option value='3'>Option 3</option>
</select>
<input type='submit'>
</form>
Then the PHP page will receive the selection via $_POST['myselect'].

You can submit the form onChange event of the select option.
Then you can run a $_GET and display the appropriate content for that particular page by comparing the value of $_GET['page']
<form action="page.php" method="get">
<select name="page" onchange="this.form.submit()">
<option value="page1">Page 1</option>
<option value="page2">page 2</option>
</select>
</form>
<?php
if(isset($_GET['page'])){
if($_GET['page']=="page1"{
//content for page 1
}elseif($_GET['page']=="page2"{
//content for page 2
}
}
?>
In this way you wont be needing the submit button as well. You can also redirect it to certain page by comparing the values of $_GET['page'] however you will need to include this form in every page with appropriate method to get your work done.

You could also bypass the submit button and use the following:
<form action="index.php" name="myForm" method="post">
<select onchange="window.location.href = this.value" name="select">
<option value="index.php?page=yourpage">yourpage</option>
<option value="index.php?page=yourpage2">yourpage2</option>
</select>
</form>
Then where your content would go you could:
<?php
if (isset($_GET['page'])) {
if ($_GET['page'] == "yourpage") {include("pages/page1.php");}
else ($_GET['page'] == "yourpage2") {include("pages/page2.php");}
} else {include("pages/default.php");}
?>

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>

select onchange add get variable to url

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!

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>

how to pass value to php thorough POST(Can i do this just with html and php)

<div id="navigation">
<ul>
<li><a href="?id=0")><em>Home</em></a></li>
<li><a href="?id=2")><em>Login</em></a></li>
<!-- <form method="POST" onclick="form.submit();"> -->
<select name="state">
<option value="Genre">Genre</option>
<option value="Crime and Drama">Crime and Drama</option>
<option value="Drama and Romance">Drama and Romance</option>
<option value="Sci-Fi">Sci-Fi</option>
<option value="Action">Action</option>
<option value="Adventure">Adventure</option>
<option value="Adventure/Fantasy">Adventure/Fantasy</option>
<option value="Action/Adventure/Fantasy">Action/Adventure/Fantasy</option>
<option value="Action/Sci-fi/Thriller">Action/Sci-fi/Thriller</option>
</select>
<!--<input type="submit" value="Submit" name="listsubmit" id="sub"> -->
<li></li>
<li><a href="?id=1")><em>Contact us<em></a></li>
What i am trying to do here is take selection from dropdown menu and pass that value through $_POSt to my sql select statement and include that movies.php file in div content.
but unfortunateky i am not finding a way to pass var to php file .doesn't know y my $post is not working .Please guide me how to submit my form what should i menntion in action field since it is my nav.php div navigation. i select one option and pass that to movies.php which takes selected option and include that php in div content based on id value for example: this is my content.php
<?php
$id= isset($_GET['id'])? $_GET['id'] :0;
if($id=="1"){
include 'Store.php' ;
}
else if($id=="2"){
include 'Login.php';
}
else if($id=="3"){
include 'movies.php';
}
Please advice!
Modify your select and form like this.
<form method="POST" action="?id=3">
<select name="state" onchange="this.form.submit();">
<option value="Genre">Genre</option>
<option value="Crime and Drama">Crime and Drama</option>
<option value="Drama and Romance">Drama and Romance</option>
<option value="Sci-Fi">Sci-Fi</option>
<option value="Action">Action</option>
<option value="Adventure">Adventure</option>
<option value="Adventure/Fantasy">Adventure/Fantasy</option>
<option value="Action/Adventure/Fantasy">Action/Adventure/Fantasy</option>
<option value="Action/Sci-fi/Thriller">Action/Sci-fi/Thriller</option>
</select>
</form>
Form action needs for sending GET variable id. Inside movies.php now you can use $_POST['state']
Use jQuery AJAX request. You can pass values return content and show it.

How can I use PHP to validate a drop down list

I want to validate a form using PHP. Below is the sample of my form:
<form>
<select name="select_box">
<option value="0">Please Select</option>
<option value="1">OPT 1</option>
<option value="2">OPT 2</option>
</select>
<input type="submit" value="Go!" />
</form>
<?php
if(isset($_REQUEST['select_box']) && $_REQUEST['select_box'] == '0') {
echo 'Please select a country.';
}
?>
In your php script where are you are submitting your form (in this case it's the same script that echoes out the form, since you aren't specifying action and method attribute to your <form>), you can get values of inputs by doing $_GET['name_of_input'], which in your case:
if(isset($_GET['select_box'])) { // do something with value of drop down
<?php
if ($_GET['select_box'] != '0') //form validation
{
//your code here
}
?>

Categories