Dropdown data not being sent to $_POST php - php

I'm new to coding, and have encountered some difficulty with the drop-down list. Would appreciate any help given!
I have this:
<html>
<select name="Subject">
<option value="One">One</option>
<option value="Two">Two</option>
</select>
</html>
<?php
if (isset($_POST['submit'])) {
echo $_POST['Subject'];
}
echo '
<form method="post"><input type="submit" name="submit" value="Submit Option!"></form>';
?>
This returns me with an unidentified index error for 'Subject' whenever I hit the Submit Option button.
I did a print_r($_POST) and realized that my selected options for the drop-down list "Subject" did not pass through. (i.e. The $_POST array that was printed did not show anything selected options from the drop-down list)

To submit your selected value to PHP you need to put <select> inside <form> code like below:-
<html>
<form method="post">
<select name="Subject">
<option value="One">One</option>
<option value="Two">Two</option>
</select>
<input type="submit" name="submit" value="Submit Option!"></form>
</html>
<?php
if (isset($_POST['Subject'])){
echo $_POST['Subject'];
}
?>

One of the first things to know about HTML forms is that, when a form is submitted, the information contained within it gets submitted. To submit a value for Subject, that field needs to be contained within the <form> element.
<html>
<?php
if (isset($_POST['submit'])) {
echo $_POST['Subject'];
}
?>
<form method="post">
<select name="Subject">
<option value="One">One</option>
<option value="Two">Two</option>
</select>
<input type="submit" name="submit" value="Submit Option!">
</form>
</html>

Related

Select form doesn't send POST

This would be a part of a search menu, but I can't get the POST from it. I searched for it, but cant make it work.
Notice: Undefined index: Make in C:\xampp\htdocs\carsale\carsale_dropdown.php on line 3
<form name="mainForm" action="carsale_dropdown.php" method="POST">
<select class="form-control" name="Make" >
<option value="Nissan">Nissan</option>
<option value="Toyota">Toyota</option>
</select>
</form>
<?php
if (isset($_POST['search'])){
$selectedMake = $_POST['Make'];
$sql="SELECT * FROM vehicles WHERE make=selectedMake";
}
?>
In order to send something via $_POST or $_GET you need to submit it. Therefore you need something like
<form name="mainForm" action="carsale_dropdown.php" method="POST">
<select class="form-control" name="Make" >
<option value="Nissan">Nissan</option>
<option value="Toyota">Toyota</option>
</select>
<button type="submit">Click</button>
</form>
Every form has to be submited.

get the form values in php in the same page of the form

i want to take the < select > value to the php code (above) when i press the submit button
I tried to send the form values to the same page (with action) but it didnt work.
what should I do?
<form name="open_file">
<select name="file" style="width:40%; font-size:20px;">
<option value="A1" selected="selected" >A1</option>
<option value="A2" >A2</option>
<option value="A3" >A3</option>
</select>
<button type="submit" name="submit">UPLOAD</button>
</form>
<?php
//the <select> value should be in $file.
$file= ...?
?>
thank you !
Without defining the action attribute, form element WILL NOT send the request to any page via any method.
In addition, using button, probably would the server not send the request. Use input element instead.
If you really wish to send the data to the same page, use:
<form name="open_file" action="" method="GET">
<!--your link to this page in action attribute-->
<select name="file" style="width:40%; font-size:20px;">
<option value="A1" selected="selected" >A1</option>
<option value="A2" >A2</option>
<option value="A3" >A3</option>
</select>
<input type="submit" name="submit" value="UPLOAD" />
</form>
Plus, it is not a good practice to process form data after outputting HTML. Put the PHP code at the beginning of the document.
First, you have to define your form action and method like :
<form name="open_file" action="" method='GET'>
<select name="file" style="width:40%; font-size:20px;">
<option value="A1" selected="selected" >A1</option>
<option value="A2" >A2</option>
<option value="A3" >A3</option>
</select>
<button type="submit" name="submit">UPLOAD</button>
</form>
<!-- GET YOUR SELECT VALUES USING PHP -->
<?php
if(isset($_GET['file'])){
$file = $_GET['file'];
// Now you can use $file variable according to your code requirements
}
?>
For more detail : PHP Form Handling

Passing two variables via GET to PHP using HTML form

I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.
Obviously this method doesn't work:
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.
You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:
<input type="text" id="sString" name="sString">
and then in PHP
$search = $_GET['sString'];
You need to use attribute name instead of value.
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString" name="sString"/>
<select name="sField">
<option name="Name">Name</option>
<option name="HostID">Host ID</option>
<option name="OrderNumber">Order Number</option>
<option name="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
Try:
<form action="search.php" method="get">
<input type="text" name="sString" id="sString" value="">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);

Access values of a group of select boxes in php from post

I'm new to PHP, and I can't figure this out.
I'm trying to figure out how to access the data of a group of select boxes I have defined in my HTML code. I tried grouping them as a class, but that doesn't seem to work... maybe I was doing it wrong.
This is the following HTML code.
<form action="" method="post">
<select class="foo">
<option> 1.....100</option>
</select>
<select class="foo">
<option> 1.... 500></option>
</select>
<input type="submit" value="Submit" name="submit"/>
</form>
I essentially want to group all my select boxes and access all the values in my PHP code.
Thanks
Use the name attribute in your select tags. Then you can access those fields by name in the $_POST variable.
<form action="" method="post">
<select name="number_one">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
<select name="number_two">
<option value='a'>1</option>
<option value='b'>2</option>
<option value='c'>3</option>
</select>
<input type="text" name="something"/>
<input type="hidden" name="hidden_value" value="hidden"/>
<input type="submit" value="Submit" name="submit"/>
</form>
In your PHP you can access these like so:
$number_one = $_POST['number_one'];
$number_two = $_POST['number_two'];
$something = $_POST['something'];
$hidden_value = $_POST['hidden_value'];

PHP - Pass Drop Down List option through query string

I have a drop down list that has options that need to be passed through a query string. How would I go about doing this? Also, would anyone be able to list a way to do this both using a button and without using a button? Thank you!
<form method="get">
<select multiple="multiple" name="things[]">
...
</select>
<input type="submit" value="submit"/>
</form>
<?php
if(isset($_GET['things'])) {
foreach($_GET['things'] as $thing) {
echo $thing . '<br />';
}
}
?>
Based on Jani's response, are you wanting to have the form submit without a button but still have a backup button if the user doesn't have javascript? You can use noscript to cover that:
<form action="script.php" method="get">
<div>
<select name="options" onchange="this.form.submit()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<noscript>
<input type="submit" value="Go!" />
</noscript>
</div>
</form>
Without a button:
<form method="get">
<select multiple="multiple" name="things[]" onchange="this.form.submit()">
...
</select>
</form>

Categories