retrieve value from drop down bar and display result - php

on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?

This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">

You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks

Related

Send multiple values from dropdown view to the controller action

I have a dropdown box where the data is coming from my database. I want to send the selected drop-down item to the controller action method. And then I will send these values to the model to do the further works. Here, the problem is I have two database values in a single item of drop-down box. And, I am not figuring out how to send those two the method. Here is my code given below,
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option>Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
From this dropdown items, I want to send the sourcestationid and destinationstationid separately as 2 parameters to my controller action method. Here is my controller code though this is not correct way I think,
function getdata(){
$iotdata['test'] = $this->input->post('select2');
//rest of the code according to the source and destination id item
}
Thanks in advance.
You haven't kept the variable inside option's value tag.
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
Also here at PHP end, you can explode the string into an array and store it further into db.
<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
Thanks #BitsPlease for our suggestion. Your idea is working. But I need to do a small change to get it instantly. My select tag needs to be under form tag to get them without reloading the page again. Here is my view,
<form method="post" accept-charset="utf-8" action="<?php echo site_url("controller/action"); ?>">
<select name='select' onchange="this.form.submit()">
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationidr']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationidr']; ?></option>
<?php endforeach; ?>
</select>
</form>
Here is the php code,
$select = explode(',', $this->input->post('select'));
print_r($select)

Displaying default value through select tag

I am having trouble passing & displaying default value for select tag. So, I have tried different variants of select and option tag but I am not able to get this right.
I want to fetch different options in form of a drop down menu. I want to display a default value in it. ($categorytemp[$i] in code). On displaying the form
A user can update this choice; or
A user can select the same choice again; or
A user can not change it at all.
Expected result for the above activity should be update, update, default value for $category
Below is the snapshot of my code
<select name="category" value="<?php echo $categorytemp[$i] ?>">
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>
I tried using the option tag with selected attribute but that is making things more complicated. For example, if I use
<select name="category" value="<?php echo $categorytemp[$i] ?>">
<option selected="selected"><?php echo $categorytemp[$i]; ?> </option>
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>
It displays the form as desired but on making no selection, it passes null value in the category. I have gone through questions of similar type asked earlier, but they do not answer my query to my satisfaction
You do it with a "selected" attribute of a given <option> and not in the <select> tag with the value attribute.
Check these two:
http://www.w3schools.com/tags/tag_select.asp
http://www.w3schools.com/tags/tag_option.asp
Have you consider using Javascript on the form submit to achieve it?
Another option would be a hidden field with default value before the select and an hidden option of the select field:
<html>
<head>
<body>
<form method="POST" action="">
<input type="hidden" name="dropdown" value="default" />
<select name="dropdown">
<option selected disabled hidden style="display: none" value="default"></option>
<option>1</option>
<option>2</option>
<input type="submit" value="send" />
</form>
</body>
</html>
Is $categoryTemp[$i] an ID or a name? If it is the ID and you are wanting to check the result against it to determine the default option to display then something along these lines would select the option that is equal to the value of $categoryTemp[$i].
<option <?php echo ($categorytemp[$i] == $education_category ? 'selected' : ''); ?>
So if $categorytemp[$i] = 1 and $education_category = 1 then that option would be marked selected and be displayed when the page loaded
This did the trick. When no value is selected, pass the default value as value in option
<select name="category" >
<option value='<?php echo $categorytemp[$i]?>'><?php echo $categorytemp[$i]; ?> </option>
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>

Get the value from a listbox in php in the same page

I searched and searched the internet for help but I didn't get it so I'm posting here. I've got a select option thing in my code and I want to get the value of it so I can use it in a query, but I don't know how to get the value from the select option in the same page. So how can I get the value from this select? I'll give you the code to my select.
<select name="tamanho">
<?php
$sqltamanhos="SELECT * FROM detalhes_produtos WHERE cod_produto ='$codigo'";
$resultadoo=odbc_exec($ligaBD,$sqltamanhos);
while (odbc_fetch_row($resultadoo))
{
$tamanho = odbc_result($resultadoo,2);
?>
<option value ="<?php echo $tamanho; ?>"> <?php echo $tamanho; ?> </option>
<?php } ?>
you can get the value of select box via posting that value in the form(get/post method).
<form method="get" action="">
<select name="tamanho">
<?php
$sqltamanhos="SELECT * FROM detalhes_produtos WHERE cod_produto ='$codigo'";
$resultadoo=odbc_exec($ligaBD,$sqltamanhos);
while (odbc_fetch_row($resultadoo)){
$tamanho = odbc_result($resultadoo,2);?>
<option value ="<?php echo $tamanho; ?>"><?php echo $tamanho; ?> </option>
<?php }?>
</form>
and get the value via
$value=$_GET['tamanho'];
you can also check this get the value of <select> without submitting on the same page using php

Change textbox value using dropdown selected in php and mysql

I have texbox and dropdown list which is populated from mysql database.I want to change textbox value using dropdown list, without refreshing the page.
Here is my code and Thanks in Advance.
<select name="select" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php
require("dbcon.php");
$getallcompanies = mysql_query("SELECT * FROM ifcandetails6");
while($viewallcompanies = mysql_fetch_array($getallcompanies)){
?>
<option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
<?php
}
?>
</select>
Here is my Input field code:
<input type="text" id="field1" value="<?php echo $viewallcompanies['tcname']?>" disabled/>
Use following
$(document).ready(function(){
$("#dropdownlist1").change(function(){
$("#field1").val($(this).val());
});
});
As you can do it on front side itself, you dont need to change in your PHP code. Add the following code on DOM ready.

how to get selected username from dropdown list to be the var for URL

How do i get the selected variable to show up in the URL from the
<select> ...
my code is:
print "<select name=\"assignedby\" multiple size=\"10\">";
while ($data = dbResult($qh)) {
print "<option name=\"$data[name]\"";
print ">$data[name]</option>\n";
}
print "</select>";
print "<br><a href='".$_SERVER['PHP_SELF']."?action=add'>Add</a> || <a href='".$_SERVER['PHP_SELF']."?origname=$data[name]'>Edit</a>\n";
When someone clicks on "EDIT" link its showing up as: http://www.site.com?origname=
i want it to show up with the actual selected origname from the drop down list...
like:
http://www.site.com?origname=$selecteduser-fromdroplist
please help!
Why don't you just make the form method = get?
<form id="select_name" action="" method="get">
<select name="origname">
<?php while ($data = dbResult($qh)): ?>
<option value="<?php echo $data[name]; ?>"><?php echo $data[name]; ?></option>
<?php endwhile; ?>
</select>
Add || <input type="submit" name="submit" value="Edit">
</form>
But as for why your code doesn't work, you're calling the $data[name'] item outside the while loop.

Categories