hi i have some data in mysql db and i need to create a page where i have a dropdown down box with different categories. when a user select a category from a the drop down it will show data form the mysql table from that category.

so how can i do that but i need as soon as they select the drop down the result is loaded and doesnt have to load result on another page.

thanks

Dani AI

Generated

A quick recap: the dropdown filter and returned rows were handled on the page, and correctly identified that the missing path was the reason the PDF name appeared as plain text (confirmed by ). and offered preview/iframe approaches, but the thread clarified the requirement: show a PDF icon and let the user download the file (not preview it in-browser).

To make a click trigger a download reliably, serve files through a small download handler instead of linking the raw upload folder. The handler looks up the stored filename (and the original display name) from the database, verifies the file exists, and sends safe headers so browsers download the file:

<?php
// download.php?id=123  (map id -> storedName / originalName from DB)
$file = '/var/www/uploads/' . $storedName;
if (!is_file($file)) { http_response_code(404); exit; }
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($originalName).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>

Security and robustness checklist (gaps seen in the thread):

  • Store only a safe server filename (or a DB id) and keep the original filename separately for display. Rename uploads to unique names (uniqid/random) to avoid collisions and injection.
  • Validate uploads (server-side): check MIME with finfo, enforce allowed extensions and size limits, and confirm move_uploaded_file returned true.
  • Protect storage: keep uploads outside webroot when possible, or deny direct access and force downloads via the handler. Consider server accel modules (X-Sendfile / X-Accel-Redirect) to offload file serving for large files.
  • Output safely: use htmlspecialchars() for displayed names, and move from deprecated mysql_* calls to prepared statements (mysqli or PDO) to prevent SQL injection.
  • UX: for immediate filtering without a full page reload, use the dropdown onchange to submit via AJAX and replace the results table; links in the returned HTML can point to the download handler so clicking the icon starts the download.

Recommended Answers

All 13 Replies

ok i managed to do it but the only problem i geteing now is display a pdf that i stored its file name in mysql and the file on a folder on the server.

i want the pdf file to show allong with my other result and if the click on it they can download it. can anyone help me with that?

Member Avatar for Member #120589

Perhaps if you showed your code?

// check if form has been submitted:
if(isset($_GET['submit'])) {

    // establish a DB connection (assuming mysql)
    ...

    // check if the appropriate category has been selected
    // and escape it
    $category = mysql_real_escape_string($_GET['category'])

    // construct the query
    $q = "SELECT * FROM theTable WHERE category='$category'";

    // get the result
    ...

    // do whatever else is neccessary (handle errors, redirect?)
    ...
}

// put the html here including the form code
// decide what the action is (GET in this example)

To make form autosubmit google for form autosubmit. This is one link.

OK, while typing the answer to your post, your question changed, so never mind my post above.

maybe and <iframe> such as:

<html>
  <head>
    <script language="JavaScript" type="text/javascript">
    <!--
      function changeFrame(newPage){
        document.getElementById('myframe').src = "results.php?category=" + newPage;
      }
    //-->
    </script>
  </head>
  <body>
    <div align="center">
      <select id="menu">
        <option id="category1" onClick="changeFrame(this.id)">Home</option>
        <option id="category2" onClick="changeFrame(this.id)">About</option>
        <option id="category3" onClick="changeFrame(this.id)">Contact</option>
      </span>
      <br><br>
      <iframe src="default.html" id="myframe"></iframe>
    </div>
  </body>
</html>

ok so here is my code to display the data.where its displaying the file i want it to display the acctual pdf file not just the name

<html>
<header>
<link rel="stylesheet" type="text/css" href="style.css">
</header>

<body>

<form name="filter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

<select name="catselect">
<option value="engineering">Engineering</option> 
<option value="law">Law</option> 
<option value="audit">Audit</option> 
<option value="marine time">Marine Time</option> 
</select>

<input type="Submit" value="search" name="search">
</form>


<?php
mysql_connect("localhost","xxxxx","xxxxxx");//database connection
mysql_select_db("xxxxxxxx");


if (isset($_POST["catselect"])) { 
$cat = mysql_real_escape_string ($_POST["catselect"]); 
$Query = "SELECT * FROM data WHERE apptype='$cat'"; 





//RUN THE QUERY
$result = mysql_query($Query) or die (mysql_error());
echo "<table cellpadding=10 border=1>"; 
while ($row = mysql_fetch_array($result)) {
echo "<tr>"; 
 echo "<td>" . $row['name']."</td>"; 
 echo "<td >" . $row['email']."</td>"; 
 echo "<td >" . $row['apptype']."</td>"; 
 echo "<td >" . $row['description']."</td>"; 
 echo "<td >" . $row['file']."</td>"; 
echo "</tr>"; 

}
echo "</table>";
}

?>

<body>

</html>

and here is my code i use to insert the data into the data base

<?

//the example of inserting data with variable from HTML form
//input.php
mysql_connect("localhost","xxxxx","xxxxxxx");//database connection
mysql_select_db("xxxxxxx");

 //This is the directory where images will be saved 

 $target = "upload/".basename($_FILES['pdffile']['name']) ;


$name=$_POST['name'];
$email=$_POST['email'];
$apptype=$_POST['apptype'];
$description=$_POST['description'];
$pdf=($_FILES['pdffile']['name']); 


//inserting data order
$apply = "INSERT INTO data
            (name, email, apptype, description, file)
            VALUES
            ('$name','$email','$apptype','$description','$pdf')";

            //Writes the pdf to the server 
 if(move_uploaded_file($_FILES['pdffile']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 


?>
Member Avatar for Member #120589

You can use Google pdf viewer.

$url = "";

echo "<iframe src='http://docs.google.com/gview?url=$url&embedded=true' style='width:718px; height:700px;' frameborder='0'></iframe>";

pdf must be visible to public - can't be behind a logged in area etc.

i dont want the user to view the pdf.i want that when the form return the result it has the pdf name displayed with the pdf icon.if the click on it the i gets download on thier pc.

all im getting now is just the name of the pdf showing as text its not linking to the file on the server.

i dont know if i should have store the pdf path in mysql insted of the pdf name

You construct links to files when displaying the table.

// path to pdf files (including backslash/slash)
$path = 'whatever/';

// the icon
$icon = '<img src="pathtotheicon" alt="PDF File">';

while ($row = mysql_fetch_array($result)) {

    $file = $row['file'];

    echo "<tr>";
    echo "<td>" . $row['name']."</td>";
    echo "<td >" . $row['email']."</td>";
    echo "<td >" . $row['apptype']."</td>";
    echo "<td >" . $row['description']."</td>";
    echo "<td >$icon <a href=\"$path{$file}\">" . $file . "</a></td>";
    echo "</tr>";
}

By clicking a link the user will be able to download (or open) the file.

Member Avatar for Member #120589

i dont want the user to view the pdf.i want that when the form return the result it has the pdf name displayed with the pdf icon.if the click on it the i gets download on thier pc

Please explain yourself better, as you stated:

i want it to display the acctual pdf file not just the name

broj1 you are the best it work like a charm man. that was my problem coundt figure out how to inclued the path to it.

thanks a million

diafol im sorry i didnt explain my self clearly. but thank all for the great help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.