How do I $_REQUEST an option made by a database? - php

The type of sport-options are selected from a database where I have all the type of sports.
Type_sport contains the name of the sport. Everything works great and all the sports in the database are shown in an option list. But how do I $_REQUEST what type of sport the user have choosen when the options are comming from a database?
The code in the form looks like this:
<p>Choose sport: </p>
<form action="page2.php" method="post">
<select name="sports">
<option>Choose sport</option>
<?php
$db=mysqli_connect("xxxxxxxx","xxxxx","","xxxxx");
$sql="Select* from sports";
$result=mysqli_query($db,$sql);
$numberOfRows=mysqli_affected_rows($db);
for($i=0;$i<$numberOfRows;$i++){
$row=mysqli_fetch_array($result);
echo "<option> ".$row["type_sport"]."</option>";
}
?>
</select>
</form>
I have tried: $sport=$_REQUEST["sports"]; but it does not work.
`

You need to give the option tag a value attribute that's the only way you'll get the value from the request superglobal echo "<option value='".$row["type_sport"]."'> ".$row["type_sport"]."</option>";

Related

PHP variable in header function in one drop list with two values [duplicate]

I'd like to post two values in one drop down option and I'm not sure about the best way to approach it.
The drop down list pulls data in from an external service. This data is 'id' and 'name'.
When I select an option in the drop down and then press submit I want the 'id' and the 'name' to be posted.
My code looks like this:
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
I've tried putting in a hidden input field, but that then doesn't render out the drop down (instead it just creates a list).
I am using both the id and name elsewhere, so I don't want to have to post just the id and then have to get the name again, hence I want to post both of them at the same time.
Any recommendations?
You cannot post two values unless both of them appear in the value attribute. You can place both in, separated by a comma or hyphen and explode() them apart in PHP:
// Place both values into the value attribute, separated by "-"
<option value="<?php echo $name['id'] . "-" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
Receiving them in PHP
// split the contents of $_POST['data'] on a hyphen, returning at most two items
list($data_id, $data_name) = explode("-", $_POST['data'], 2);
echo "id: $data_id, name: $data_name";
You may add a hidden field with the name "id" and then bind an onchange event listener to the <select>. inside the onchange function, get the value of the <select> and assign it to the "id" field.
<form>
<select name="name" onchange="document.getElementById('id').value=this.value">
<!--
...
options
...
-->
</select>
<input type="hidden" name="id" id="id" />
<input type="submit" />
</form>
You could output something like:
<option value="<?php echo $name['id']."_".$name['name'];?>">
Then use preg_splitor explode to separate the two once the form data is processed.
The only way to do this is to include both pieces of information in the value for the single option. Just use code like the following, and then when you get the value back, split the string at the first underscore to separate out the values.
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id'] . "_" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
Also, you could just leave the ID alone in your form, and then look up the id again when the user submits the form.
You could make as many hidden inputs as there are options, each with the name of one option value. The input's values are the contents of the options. In the second script, you can look for the dropdown value, and then take the value of the hidden input with that name.
We can pass it with data attribute, and can access in js,
<option value="<?php echo $name['id']);?>" data-name="<?php echo $name['name']);?>">One</OPTION>
var name = $(this).find(':selected').data('name');

using a variable in $_POST in php?

i have a page in which i am using a dropdown menu. i have passed a variable in the name field of the select tag whose value comes from an array.
$query1="Select SSAP from project where projname='$project'";
$result=mysql_query($query1);
while($row=mysql_fetch_assoc($result))
{
$ssap = $row['SSAP'];
$query2="Select * from student where SSAP='$ssap'";
$res=mysql_query($query2);
$row1=mysql_fetch_assoc($res);
$name=$row1['name'];
echo $name; ?> <select name="<?php echo $name;?>">
<option value="A"> Exceptional </option>
<option value="B"> Highly Effective </option>
<option value="C"> Effective </option>
<option value="D"> Good </option>
<option value="E"> Not Satisfactory </option>
</select> <br> <?php
}
and i need to retrive the value of each select tag created in another variable on the action page.
$grade=$_POST[$name];
echo $grade;
the first code snippet works fine but i am unable to fetch the value in the second snippet.
When the form containing the <select> with the dynamic name is submitted, it is a different request, so $name hasn't been set.
When your form is submitted, the <select> names are getting evaluated like <select name="Raj">, <select name="Kiran">, ..........<select name="Sameer"> etc depending on the values of $name=$row1['name']; from your query on 'student' table. Also you should pay consideration to the fact that your query $query2="Select * from student where SSAP='$ssap'"; might return more than one rows, it seems you are assuming only one row will be returned.
Also note $_POST is an associative array that accept parameters as
KEY=>VALUE pairs from submission of HTML. In $_POST key is name of
element in input form value is value of that element in input form
So you can not use a variable as the KEY in your SUPER GLOBAL $POST like this $_POST[$name]; when $name is undefined on your action script. I would suggest you use an named array in your multiple <Select>. Do a var_dump($_POST) on your action script. That may reveal most of the stuffs for you.

Complicated situation. Getting input name from ajax select dropdown for mysql data

I have a locations dynamic dropdown (country,region,city) done with ajax. Works great.
My goal is to retrieve data (posts) from mysql table, based on the location selections. I already have the posts in mysql table that correspond to the locations.
Now the issue I am having is that the query won't retrieve data based on the region and city. It will only work with the $_GETcountry. It comes to my understanding the reason is because region and city <select> queries are in a separate PHP files and are called through js code in the head section, so I am unable to use $_GET method to get their inputs.
Here is my form setup:
<form action="" method="get" enctype="multipart/form-data >
<div id="countrydiv">
<select id="country" name="country" onChange="showRegion(this.value);">
<option value="0">--Select Country--</option>
<?php
$getCountry = DB::getInstance()->query("SELECT * FROM countries");
if(!$getCountry->count()) {
echo 'No Country found!';
} else {
foreach($getCountry->results() as $row) {
$country_id = escape($row->countryId);
$country_name = escape($row->countryName);
?><option value="<?php echo $country_id; ?>" ><?php echo $country_name; ?></option><?php
}
}
?>
</select>
</div>
<div id="regiondiv"></div>
<div id="citydiv"></div>
<input type="submit" value="Search">
</form>
I can get the country input name from above with this $_GET['country'], but I also need it for the region and city. How can I do that?

select field in form is not sending data using php

so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine?
<form method='POST' action='add.php'>
<select>
<option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option>
<?php
$dis=show_groups_select_list($_SESSION['user_id']);
foreach($dis as $key=>$list){
echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>';
}
?>
</select>
</form>
You need to put the name attribute on the select tag, not the option tag.
<select name="user_page_id">
<?php foreach ($dis as $key => $list): ?>
<option value="...">...</option>
<?php endforeach; ?>
</select>
I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:
<select name="any_name" class="form-control">
<option value="this is what would be sent if selected"> sth </option>
</select>
for more information take a glance at this discussion too!

Get the Selected value from the Drop down box in PHP

I am populating a Drop Down Box using the following code.
<select id="select_catalog">
<?php
$array_all_catalogs = $db->get_all_catalogs();
foreach($array_all_catalogs as $array_catalog){?>
<option value="<?= $array_catalog['catalog_key'] ?>"><?= array_catalog['catalog_name'] ?></option>
Now how can I get the selected option value using PHP (I have the code to get the selected item using Javascript and jQuery) because I want the selected value to perform a query in database.
Any help will be much appreciated. Thank you very much...
You need to set a name on the <select> tag like so:
<select name="select_catalog" id="select_catalog">
You can get it in php with this:
$_POST['select_catalog'];
Couldn't you just pass the a name attribute and wrap it in a form?
<form id="form" action="do_stuff.php" method="post">
<select id="select_catalog" name="select_catalog_query">
<?php <<<INSERT THE SELECT OPTION LOOP>>> ?>
</select>
</form>
And then look for $_POST['select_catalog_query'] ?
You have to give a name attribute on your <select /> element, and then use it from the $_POST or $_GET (depending on how you transmit data) arrays in PHP. Be sure to sanitize user input, though.
Posting it from my project.
<select name="parent" id="parent"><option value="0">None</option>
<?php
$select="select=selected";
$allparent=mysql_query("select * from tbl_page_content where parent='0'");
while($parent=mysql_fetch_array($allparent))
{?>
<option value="<?= $parent['id']; ?>" <?php if( $pageDetail['parent']==$parent['id'] ) { echo($select); }?>><?= $parent['name']; ?></option>
<?php
}
?></select>

Categories