drop down menu php with condition - php

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.

Related

How can I prevent the ID from showing in the datalist element?

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.

PHP get selected value from select box?

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

Display result in dropdown and select to perform query in php mysql

I have a database named Data which has a table in which their are different names of products their id and prices, i want to make a web page using php so that i can edit,add and save the items from the web page to the DB and search the names accordingly.
<html>
<head>
<title>Products store</title>
</head>
<body>
<p style="font-size:20px" align="center"> <b>Product Database Editor</b> </p>
<p>
<form method="post">
Enter Product Name: <input type="text" name="pname" id="pname" size="70">
<input type="submit">
</p>
<form method="post">
<select id="opt" name="opt">
<?php
$pname = $_REQUEST['pname'];
// $pname= mysql_real_escape_string($_POST['pname']);
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Dataentry", $con);
$result = mysql_query("SELECT * FROM products where name like '%$pname%'");
$result_rows = mysql_num_rows($result);
if($pname==NULL)
{
echo "Please enter a product name!";
}
else if($result_rows==0)
{
echo "Product Name does not exist!";
}
else
{
while($row = mysql_fetch_array($result))
{
$name = $row['name'];
echo "<option value='$name_selected'>$name</option>";
//echo ("<option value = '" . $row['name'] . "'>" . $row['id'] . "</option>");
echo $name_selected;
echo "<br />";
}
}
mysql_close($con);
?>
</select>
</form>
</body>
</html>
when i run this code i get the names list in the dropdown but after i select any name, nothing happens, how should i modify my code so that i can select any name from the dropdown and then be able to fetch the price of that particular name to edit it.
please help, coding will be much helpful.
Suppose you have a question like this in your dropdown menu.
Q - How many colors does the US flag has?
Now, from what I understand you want your choice from the drop down menu to appear instantly..
Well, here is a simple select form.
<form method="post">
<select id="opt" name="opt">
<option value="four">four</option>
<option value="five">five</option>
<option value="two">two</option>
<option value="million">million</option>
</select>​
And, the JS code:
$(document).ready(function() {
$("#opt").change(function() {
alert($(this).val());
});
});​
Now, click her a DEMO with jsFiddle to show you, how it works.
You can copy/paste the codes and include them in your site, this is a simple code, but you if you small knowledge of Javascript you can manipulate the data the way you need it to appear. .
To get a value of an input (this case, from a dropdown) on the fly, you need to use client-side scripting language javascript (or jquery) and use ajax to sent it to server-side, where the code is in PHP.

sql retrieve data from table and make them links

I am generating web pages from database. Now my question is:
I have 1000 records(names) in my database(MySql).
I have made a search box in a page and when i enter any name or a part of name that is in my DB all the name's should come up.
Eg-
SELECT * FROM table where name like '%$find%'
Now i want to show the selected names(fetched through the query) on the new page so that when i click on any of the name a new page should open up and all the data related to that selected name (present in the table belonging to the database)to be shown on that page with navigation buttons, what query should i use to perform it.
In short i want to make my page like Google search page.
My first page is like this
<html>
<body >
<h2>Search</h2>
<form name="search" method="post" action="second.php">
Search Name: <input type="text" name="find" id="find" />
<input type="submit" name="search" value="search" />
</form>
</body>
</html>
Second page is somewhat like this
<html>
<head>
<script>
function favBrowser()
{
var mylist=document.getElementById("opt");
document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text;
}
</script>
</head>
<body>
<form method="get">
<?php
$find = $_REQUEST['find'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data", $con);
$result = mysql_query("SELECT * FROM table where name like '%$find%'");
$result_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
// $names[] = $row['name'];
// echo $names[0];
// echo "$row[name]. $row[id] <a href='data.php?edit=$row[name]'>edit</a><br />";
$_name = $row['name'];
echo "Name : <input type='text' name='name' value='$_name' size='30'>";
echo "<br />";
}
}
mysql_close($con);
?>
<!--</select>
<input type ="submit" value="submit">
<p>Your selected name is: <input type="hidden" name="fun" id="favorite" size="30">
</p>
-->
</body>
</html>
Well, simplified, on the first page you'll have something like:
while($row = mysql_fetch_array($result))
{
$_name = $row['name'];
echo '<a href="second_page.php?name='.strip_tags($_name)'" target="_BLANK"'.'</a>';
}
and on the second page you have name, passed as URL parameter, on which you then do another database look up to get the contacts details and populate the various fields:
$_name = $GET['name'];
Please remember to add the required escapes or rather use PDO / mysqli
But the question is how will you make all the names as links and then fetch their result on next page .. right ?

how can i view the data resulting from one of the choices in the dropdown list?

im new to php so im having some problems creating what i want
i'll explain first what i need .. there conferences, each conference has a list of reviewers and authors.
i have create a dropdown list where the user chooses which conference ... i want to show a list of the reviewers and the authors that are in this conference after clicking submit.
that is my code
<?php
$con = mysql_connect("localhost:3306","root","");
mysql_select_db("messaging_dd", $con);
$sql_drop = "SELECT conference_ID,conference_name FROM Conferences";
$drop_result = mysql_query($sql_drop,$con) or die(mysql_error());
$num_rows = mysql_num_rows($drop_result) or die(mysql_error());
mysql_close($con);
?>
<form name="choose" action="savedata.php" method="POST">
<br />
Conference: <select name="conference">
<?php
for($i=0 ; $i<$num_rows ; $i++)
{
$idofconference = mysql_result($drop_result,$i,0);
$nameofconference = mysql_result($drop_result,$i,1);
echo '<option value=" '.$idofconference.' ">'.$nameofconference.'</option>';
}
?>
</select>
<br />
<input type="submit" value="submit" name="submit" />
</form>
Try this,
$conf_id = $_POST['conference'];
$con = mysql_connect("localhost:3306","root","");
mysql_select_db("messaging_dd", $con);
$sql = "SELECT review, author FROM Reviews WHERE conf_id = ".$conf_id;
$review_list = mysql_query($sql,$con) or die(mysql_error());
mysql_close($con);
Or you can go for Ajax. Updating your search result, without reloading the whole page. Reference for Ajax: http://www.w3schools.com/php/php_ajax_database.asp
All the data being submitted gets stored in the $_POST variable as an array. Your conference ID will be in $_POST['conference'] as the name of your select element is conference.
An other approach is to load the desired data (reviewers and authors) through an AJAX request so that the viewer of your website won't leave the webpage.
it's similar to what you have done, just add conference id details like this:
$sql = "SELECT reviewer, author FROM Conferences where conference_ID = " . $_POST['conference'];
In your file savedata.php you can put
$whatever = $_POST['conference']
$_POST is one of several arrays in php that is reserved for system data, for example you can make calls to $_server to find out details about the server(eg the time on the server)
you could also change the method='POST' to method='GET' and it would be in the GET array
$whatever = $_GET['conference']
this is a bit less secure, but if that's not a priority its worth considering
I think you should Try this.
<form name="choose" action="savedata.php" method="POST">
<br />
Conference: <select name="conference">
<?php
while($row=mysql_fetch_array($drop_result)
{
echo '<option value=" '.$idofconference.' ">'.$nameofconference.'</option>';
}
?>
</select>

Categories