Jump to content

Modify query with form pull down menu


NoDoze

Recommended Posts

please be more detailed because I could not understand what do you want.

 

You mean multiple query using pull down menu?

 

example, user select pull down menu A, then the value is passed and used for the second pull down menu B. Do you mean like this?

 

if yes, it is possible. :)

Link to comment
Share on other sites

<select name="sortBy">
<option value="name">Name</option>
<option value="date">Date</option>
</select>

 

Submit that form and on the resulting page, use the following form:

 

$query = "SELECT * FROM table WHERE field1 = 'value'";
if(isset($_POST['sortBy'])) {
     $query .= " ORDER BY " . $_POST['sortBy'];
}
$result = mysql_query($query);

 

 

Link to comment
Share on other sites

Hmmm...looks straight forward, but for some reason I can't get it to work...???

 

Here is my code:

 

<tr class="table-header">
<td>
<div align="right">Sort by: </div>
</td>
<td colspan="2" class="sortby">
<form action="viewall2-sort.php" method="post">
<div align="left">
<select name="sortBy" class="txtbox">
<option value="date">Initiation Date</option>
<option value="project_number">Project Number</option>
<option value="responsible_individual">Responsible Individual</option>
<option value="off_project_name">Official Project Name</option>
<option value="inv_project_des">Invoice Job Description</option>
<option value="serv_project_name">Project Folder Name</option>
<option value="region">Region</option>
<option value="clientend">End Client</option>
<option value="solesource">Sole Source</option>
</select>
</div>
</form>
</td>
<td align="left"><input name="Submit" type="submit"></td>
<td>
<?php
$query = "SELECT * FROM pil WHERE field1 = 'value'";
if(isset($_POST['sortBy'])) {
     $query .= " ORDER BY " . $_POST['sortBy'];
}
$result = mysql_query($query);
?>
</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>

Link to comment
Share on other sites

  • 5 months later...

<?php
include 'config.php';
include 'opendb.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="../css/index2.css" rel="stylesheet" type="text/css" />
<link href="../css/viewall.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.body-base {
text-decoration: none;
position: relative;
top: 140px;
left: 25px;
}
.sort {
font-family: Arial;
font-size: 10pt;
text-decoration: none;
color: #FFFFFF;
position: absolute;
left: 0px;
top: -25px;
}
.sort-pull {
text-decoration: none;
background-color: #0554A3;
margin-top: 19px;
}
-->
</style>
</head>
<body>
<div class="body-base">
<table border="0">
<tr>
<td>
<div align="right">Sort by: </div>
</td>
<td colspan="2" class="sortby">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="left">
<select name="sortBy" class="txtbox">
<option value="date">Initiation Date</option>
<option value="project_number">Project Number</option>
<option value="responsible_individual">Responsible Individual</option>
<option value="off_project_name">Official Project Name</option>
<option value="inv_project_des">Invoice Job Description</option>
<option value="serv_project_name">Project Folder Name</option>
<option value="region">Region</option>
<option value="clientend">End Client</option>
<option value="solesource">Sole Source</option>
</select>
</div>
</form>
</td>
<td align="left"><input name="Submit" type="submit"></td>
<td>
<?php
$query = "SELECT * FROM projects WHERE field1 = 'value'";
if(isset($_POST['sortBy'])) {
     $query .= " ORDER BY " . $_POST['sortBy'];
}
$result = mysql_query($query);
?>
</td>
</tr>
</table>
</div>
</body>
</html>

 

This is the complete code.... Nothing shows up when submit is clicked.

Link to comment
Share on other sites

Because you're not doing anything with the result.

You can't just copy and paste without understanding what you're doing ;) You didn't change the code given to make it work with your code. Does your table actually have a field1 that will equal 'value'? Probably not...

There are tons of tutorials on PHP/Mysql so I think you need to read one to find out how to use the $result you got and make it display info.

Link to comment
Share on other sites

Ok this was a bit of a rush job, so the code is probably ot it's best....

 

<?php
include 'config.php';
include 'opendb.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="../css/index2.css" rel="stylesheet" type="text/css" />
<link href="../css/viewall.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.body-base {
text-decoration: none;
position: relative;
top: 140px;
left: 25px;
}
.sort {
font-family: Arial;
font-size: 10pt;
text-decoration: none;
color: #FFFFFF;
position: absolute;
left: 0px;
top: -25px;
}
.sort-pull {
text-decoration: none;
background-color: #0554A3;
margin-top: 19px;
}
-->
</style>
</head>
<body>
<div class="body-base">
<table border="0">
<tr>
<td>
<div align="right">Sort by: </div>
</td>
<td colspan="2" class="sortby">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="left">
<select name="sortBy" class="txtbox">
<option value="date">Initiation Date</option>
<option value="project_number">Project Number</option>
</select>
</div>
</form>
</td>
<td align="left"><input name="Submit" type="submit"></td>
<td>
<?php
$query = "SELECT * FROM projects WHERE field1 = 'value'";
if(isset($_POST['sortBy'])) {
     $query .= " ORDER BY " . $_POST['sortBy'];
}
$result = mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "<br><br><br><b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$date=mysql_result($result,$i,"date");
$project_number=mysql_result($result,$i,"project_number");

echo "<b>$date</b><br>$project_number<hr><br>";

$i++;
}

?>
</td>
</tr>
</table>
</div>
</body>
</html>

 

Still get nothing....

Link to comment
Share on other sites

Why have you ended the form before the submit button? ???

And what is the txtbox attribute for? (Or the sortby attribute on the table for that matter?)

<select name="sortBy">
<option value="date">Initiation Date</option>
<option value="project_number">Project Number</option>
</select>
<input name="Submit" type="submit">
</form>

 

(removed the formatting tags cause they aren't relevant, you should be able to figure out where they go)

And Jesirose is right. You need to actually change the mysql statement to fit your database.

Link to comment
Share on other sites

  • 3 weeks later...

Yes, those are css....

 

Well, I made the changes, but still get nothing....

 

<?php
include 'config.php';
include 'opendb.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="../css/index2.css" rel="stylesheet" type="text/css" />
<link href="../css/viewall.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.body-base {
text-decoration: none;
position: relative;
top: 140px;
left: 25px;
}
.sort {
font-family: Arial;
font-size: 10pt;
text-decoration: none;
color: #FFFFFF;
position: absolute;
left: 0px;
top: -25px;
}
.sort-pull {
text-decoration: none;
background-color: #0554A3;
margin-top: 19px;
}
-->
</style>
</head>
<body>
<div class="body-base">
<table border="0">
<tr>
<td>
<div align="right">Sort by: </div>
</td>
<td colspan="2" class="sortby">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="left">
<select name="sortBy" class="txtbox">
<option value="date">Initiation Date</option>
<option value="project_number">Project Number</option>
</select>
</div>
</form>
</td>
<td align="left"><input name="Submit" type="submit"></td>
<td>
<?php
$query = "SELECT * FROM projects WHERE field1 = 'value'" or die(mysql_error());
if(isset($_POST['sortBy'])) {
     $query .= " ORDER BY " . $_POST['sortBy'];
}
$result = mysql_query($query);
$num=mysql_numrows($result);



echo "<br><br><br><b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$date=mysql_result($result,$i,"date");
$project_number=mysql_result($result,$i,"project_number");

echo "<b>$date</b><br>$project_number<hr><br>";

$i++;
}

mysql_close();
?>
</td>
</tr>
</table>
</div>
</body>
</html>

 

Please help...

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.