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

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;
?>

Related

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;
}

HTML Forms: The value attribute doesn't change what's sent to php

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);
}
?>

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

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

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

Categories