Is there a way in PHP to get the index of a currently selected HTML <option>? - php

I'm trying to use PHP to get the current index of my dropdown box.
<select name="iface" id="iface">
<option>vlan10</option>
<option>br0</option>
<option>br1</option>
<option>eth3</option>
</select>
The options are set from a loop that grabs interfaces from a server. I need to get the currently selected index if that is possible.

<form method="post">
<select name="iface" id="iface">
<option>vlan10</option>
<option>br0</option>
<option>br1</option>
<option>eth3</option>
</select>
<input type="submit" value="Print selected option"/>
</form>
<?php
if (isset($_POST['iface']))
{
echo $_POST['iface'];
}
?>

For example, the selected interface is "eth3".
So, just render with php:
<option selected="selected">eth3</option>
for indexing you render your list like this:
<select name="iface" id="iface">
<option value="0">vlan10</option>
<option value="1">br0</option>
<option value="2">br1</option>
<option value="3" selected="selected">eth3</option>
</select>
Now iface.value (or $_POST['iface']) = 3

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>

How can I make the output of this form input an array?

I'm trying to create a form in which users can select multiple options from a drop down menu. This form looks something like the following:
<html>
<form method='post'>
<select name='tag' multiple>
<option value='opt1'>Option 1</option>
<option value='opt2'>Option 2</option>
<option value='opt3'>Option 3</option>
<option value='opt4'>Option 4</option>
<option value='opt5'>Option 5</option>
</select>
<input type='submit' Value='Submit'>
</form>
<? include('select.php'); ?>
</html>
Where the php file contains the following simple code:
<?php
if($_POST){
$tag = $_POST['tag'];
echo $tag;
}
?>
The end result of this code is a drop down menu from which you may select multiple options. However, when you click submit, it only echos one of the options that the user selected.
How can I make an array of all of the options selected by the user?
Try Changes the to <select name='tag' multiple> to
<select name='tag[]' multiple>
For PHP side :
foreach ($_POST['tag'] as $selectedOption){
echo $selectedOption."\n";
}
<select name='tag[]' multiple>
You can change your select tag from this to
this:
<select name='tag[]' multiple>
In PHP:
foreach ($_POST['tag'] as $option_selected){
echo $option_selected;
}

GET the value of the selected option using php then show it with echo

I'm Beginner on PHP, Javascript, I would like to get selected value from select/option and stock it into a php variable ( $varpeoplesnames ), to use it in a future SQL request and show it with echo
my code:
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<?php (....) echo $varpeoplesnames;
How can I get the text of the selected option to show it with echo in php?
Wrap it in a form with action being the link to your php file and a submit button
HTML:
<form action='yourPhpFile.php' method='GET'>
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<input type='submit'></input>
</form>
PHP
<?php
$varpeoplesnames = $_GET['peoplesnames'];
echo $varpeoplesnames;
?>

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 do I Pass Values from One Form to Another?

I have this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form

Categories