This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";
Related
I have a code which has a form that inputs surface area. db_connect.php connects the database. I am trying to populate a drop down list with a condition that all values that have surface area greater than the value typed into the text field will be displayed in the text field. But when I try to run the code, i'm getting all the values. How can I solve this? Thank you in advance!
<html>
<head>
<title>hi</title>
</head>
<body>
<form>
<p> surface area : <input name = "sa" type = "text"> </p>
<br>
</form>
<select name="areas">
<?php
$sa = $_POST['sa'];
include "db_connect.php";
$displayArea = "SELECT area FROM details where area > '".$sa."'" ;
$sql = mysqli_query($link, $displayArea);
echo "<option> Select </option>";
while ($row = mysqli_fetch_assoc($sql))
{
echo "<option value=\"areas\">" . $row['area'] . "</option>";
}
?>
</select>
</body>
</html>
first you need a submit button into the form.
<input type="submit" value="Submit">
Then if you are using POST you have to specify it as a Form method:
<form method="post">
Then add:
$sa = $_POST['sa'];
echo("[".$sa."]");
to see if "sa" is populated.
If you add a value and click on "Submit" you will see the result.
I am trying to utilize the datalist element. Everything is working with 1 little hitch. The selectable list is showing 2 columns, both the street_id and street columns. I need the street_id that will be submitted but dont want the street_id to show in the datalist.
<?php
require 'connect_mysqli.php';
$sql = "SELECT * FROM streets";
$result = mysqli_query($con, $sql) or die ("Error " . mysqli_error($con));
?>
<form action="test.php" name="test" method = "post">
<datalist id="street" name="streets">
<?php while($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['street_id']; ?>"><?php echo $row['street']; ?></option>
<?php
}
?>
</datalist>
<input type="text" name="street_val" id="test" autocomplete="off" list="street">
<input type="submit" value="Submit">
</form>
<?php
mysqli_close($con);
//test the output value
echo $_POST['street_val'];//
?>
You have coded a select list - which has separate values for display and returned values. In the datalist, you only need value="" for options and then it will only return that value. Also better to keep the server code and display code separate: i.e. populate or build the array in the PHP with your query, then in the HTML only display it.
I have displayed the data in a form edit.php and again paas the truk_id in url on update page to update.php the data of the fields but it's not working. Can anyone check this
PHP Code
<?php
$truck_id=$_GET['truck_id'];
print_r($truck_id);
include("assets/database_con.php");
if (isset($_POST['submit']))
{
$truck_id=$_POST['truck_id'];
$truck_number= mysql_real_escape_string(htmlspecialchars($_POST['truck_number']));
$truck_model= mysql_real_escape_string(htmlspecialchars($_POST['truck_model']));
$truck_make= mysql_real_escape_string(htmlspecialchars($_POST['truck_make']));
$truck_type= mysql_real_escape_string(htmlspecialchars($_POST['truck_type']));
$truck_tierweight= mysql_real_escape_string(htmlspecialchars($_POST['truck_tierweight']));
$truck_gvm= mysql_real_escape_string(htmlspecialchars($_POST['truck_gvm']));
$truck_regodate= mysql_real_escape_string(htmlspecialchars($_POST['truck_regodate']));
$truck_inspectaiondate= mysql_real_escape_string(htmlspecialchars($_POST['truck_inspectaiondate']));
$sql1 ="UPDATE add_truck SET truck_number='$truck_number', truck_model='$truck_model', truck_make='$truck_make', truck_type='$truck_type', truck_tierweight='$truck_tierweight', truck_gvm='$truck_gvm', truck_regodate='$truck_regodate', truck_inspectaiondate='$truck_inspectaiondate' where truck_id='$truck_id'";
echo $sql1;
$results=$conn->query($sql);
if($results)
{
print 'Success! record updated';
}else{
print 'no! record updated';
}
}
?>
You are assigning values to fields in single quotes.
The main string is enclosed within single quotes.
Variables inside single quotes are not parsed (value is not calculated).
This is called variable interpolation.
Use double quotes for whole string and single quotes for values.
Corrected SQL:
$sql = "UPDATE add_truck
SET truck_number='$truck_number', truck_model='$truck_model',
truck_make='$truck_make', truck_type='$truck_type',
truck_tierweight='$truck_tierweight',
truck_gvm='$truck_gvm', truck_regodate='$truck_regodate',
truck_inspectaiondate='$truck_inspectaiondate' where truck_id='$truck_id'";
you are using GET for the truck id and POST for all other variables - is this intentional?
$truck_id=$_GET['truck_id'];
should it be
$truck_id=$_POST['truck_id'];
or should all the other variables be from the GET array?
$truck_id=$_GET['truck_id'];
Here is your problem part you can't get values from get method when you submit. So you need to add some hidden field into your form like this
<input type="hidden" name="truck_id" value="<?php echo $_GET['truck_id']; ?>">
Change your code like this
if (isset($_POST['submit']))
{
$truck_id=$_POST['truck_id'];
}
I changed your code please look at this
//Your code starts now
<?php
include("header.php");
include("assets/database_con.php");
$truck_id= $_GET['truck_id'];
//print ($truck_id);
?>
<?php
//submit part in top that is better
if (isset($_POST['submit']))
{
$truck_id=$_POST['truck_id'];
$truck_number= mysql_real_escape_string(htmlspecialchars($_POST['truck_number']));
$truck_model= mysql_real_escape_string(htmlspecialchars($_POST['truck_model']));
$truck_make= mysql_real_escape_string(htmlspecialchars($_POST['truck_make']));
$truck_type= mysql_real_escape_string(htmlspecialchars($_POST['truck_type']));
$truck_tierweight= mysql_real_escape_string(htmlspecialchars($_POST['truck_tierweight']));
$truck_gvm= mysql_real_escape_string(htmlspecialchars($_POST['truck_gvm']));
$truck_regodate= mysql_real_escape_string(htmlspecialchars($_POST['truck_regodate']));
$truck_inspectaiondate= mysql_real_escape_string(htmlspecialchars($_POST['truck_inspectaiondate']));
$sql1 ="UPDATE add_truck SET truck_number='$truck_number', truck_model='$truck_model', truck_make='$truck_make', truck_type='$truck_type', truck_tierweight='$truck_tierweight', truck_gvm='$truck_gvm', truck_regodate='$truck_regodate', truck_inspectaiondate='$truck_inspectaiondate' where truck_id='$truck_id'";
echo $sql1;
die();
$results=$conn->query($sql);
if($results)
{
print 'Success! record updated';
}else{
print 'no! record updated';
}
}
?>
And changed some thing in here(added action="")
<form role="form" method="post" action="">
And changed some thing in here also(added name="submit")
<div class="form-group">
<input type="hidden" name="truck_id" value="<?php echo $_GET['truck_id']; ?>">
<input type="submit" value="submit" name="submit" class="btn_full" id="submit-booking">
</div>
No need for another div for hidden because it is 'hidden'
Used a new code as solution :
<?php
if (isset($_POST['submit']))
{
$truck_id=$_POST['truck_id'];
...
}
and one hidden value with the submit name of attribute :
<input type="hidden" name="truck_id" value="<?php echo $_GET['truck_id']; ?>">
<input type="submit" value="submit" name="submit" class="btn_full" id="submit-booking">
I am using a get method to select options with years and months. The URL after submitting this selection looks as follows:
www.mywebsite.com/transactions/?yr=2013&mo=6
I can reload this page and the selection is still there. However, when i submit a form to add a record, the page reloads as follows:
www.mywebsite.com/transactions/?#
And the selection is gone. How can I keep the seletion and reload the exact same url after submitting the form?
I am using the following code:
<form action="?" method="post">
<SELECT options with different inputs>
<input type="submit" value="Add">
</form>
In my PHP it looks the following:
header('Location: ?yr='. $GLOBALS['yearselect'] .'&mo=' . $GLOBALS['monthselect']);
It creates the right URL after submit, only the global variables are not updated. I defined those as follows:
$GLOBALS['monthselect'] = date('m');
$GLOBALS['yearselect'] = date('Y');
And they are changed when I select an option.
You could traverse through submitted variables and add them to the HTML code.
<form action="target.php?var1=<?= $_GET["value2"]; ?>&var2=value2" method="get">
Or just use $_SERVER['REQUEST_URI'] inside action="".
Your form element probably has an attribute action="#".
You could replace this with the original request uri (assuming you use the POST method, if you use GET the query parameters in the form's action attribute will be overridden in most browsers.
<form method="POST" action="<?= $_SERVER['REQUEST_URI'] ?>">your form</form>
In your PHP code, after you get the values by GET method, use the header() function as :
header("Location: www.mywebsite.com/transactions/?yr=2013&mo=6");
It will redirect to the page with the form.
EDITED:
This is a sample code for your need:
<?php
$years=array(1999,2000,2001,2002,2003,2004);
if(isset($_POST['year']))
{
/*
Do your coding here
*/
echo <<<EOT
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<select name='year'>
EOT;
for($i=0;$i<6;$i++)
{
if($years[$i]==$_POST['year'])
echo "<option value='{$years[$i]}' selected='selected' >{$years[$i]}</option>";
else
echo "<option value='{$years[$i]}'>{$years[$i]}</option>";
}
echo <<<EOT
</select>
<input type='submit' value='Add'>
</form>
EOT;
}
else
{
echo <<<EOT
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<select name='year'>
EOT;
for($i=0;$i<6;$i++)
{
echo "<option value='{$years[$i]}'>{$years[$i]}</option>";
}
echo <<<EOT
</select>
<input type='submit' value='Add'>
</form>
EOT;
}
?>
It will print the first value of the drop-down list on the first time, and after that it'll save the values.
Inside form tag action attribute should b set to the page which handles your request,i think u have set action='#' .
Please specify more detail.so that i can help u
I've created a php form to insert values into a database.
One of my form options is a dynamic list populated with fields from another table.
I first created the form without the dynamic option, and all data inserted just fine (and still does).
Now I'm attempting to include the code below, and while it displays the option values properly, the value fails to insert. Any advice?
<?php
/*
* LIST ALL CATEGORIES
****************************************/
include('../dbconnection.php');
$query = 'SELECT category_id, category_name FROM ingredient_categories';
$result = mysql_query($query);
echo '<select>';
while ($ingredientCategoryOption = mysql_fetch_array($result)) {
echo '<option value="'.$ingredientCategoryOption[category_id].'">'.$ingredientCategoryOption[category_name].'</option>';
}
echo '</select>';
?>
I had created something similar yesterday. The $polls array is passed to the view in CodeIgniter in the $this->load->view('poll.php', $data['polls']), while you do it in the page itself. However, you can have the general idea.
<FORM id="formPoll" class="question" name="createpoll" action="<?php echo base_url()?>index.php/poll/selectOption/" method="POST">
<select name="poll_list">
<?php
foreach($polls as $poll){
echo "<option name='poll_table'>$poll->name</option>";
}
?>
</select>
<div id="input">
Poll name: <input type="text" name="name"></input>
Title: <input type="text" name="title"></input>
</div>
<div id="options">
Option: <input type="text" name="option"></input>
</div>
<input type="submit"></input>
</FORM>
Some ideas:
Check if $results is not empty before using it
Give your <select> form a name, as I showed above
Check if $ingredientCategoryOption is not null or is something returned.
Check your database connection