Jump to content

Build a Form with 2 dropdown options that load a specific PDF


Mindjakk

Recommended Posts

I understand that is might be something that is already answered and I apologize if it is, I could not find it.

What I need to do is build a simple form that has two options, they will be dropdown options. Dropdown A and Dropdown B then a Submit button. This part I understand in HTML, although it may be easier in php or javascript.

Then I need it to take the two options and create a "if/then" statement that loads a specific pdf that matches the two options selected.

Example.

If someone selects Option 1 from Dropdown A and Option 2 From Dropdown B then it loads 12.pdf
If someone selects Option 5 from Dropdown A and Option 3 From Dropdown B then it loads 53.pdf
If someone selects Option 2 from Dropdown A and Option 1 From Dropdown B then it loads 21.pdf

and so on...

It does not have to be the exact thing just some way to take both inputs and have it equal a specific pdf.

Here is the form I built but I don't know what to put in the form_action.php file in order to make it work

<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<center>
<h1> Get Directions</h1>
<form action="form_action.php" method="get" name="directions" target="_new">

<select name="startpoint" size="1">
<option value="north">North Tower Entrance</option>
  <option value="south">South Tower Entrance</option>
  <option value="moba">MOB A Entrance</option></select>
  
  ----->

<select name="endpoint" size="1">
<option value="onco">Oncology</option>
  <option value="radio">Radiology</option>
  <option value="pulm">Pulmanary</option></select>
<br /><br />
<input type="submit" value="Submit" />
</form>
</center>

</body>
</html>

Any help is appreciated, thanks.

in form_action.php, you'd do:

//grab start/end times, and force them to integers
$start = (int)$_GET['startpoint'];
$end = (int)$_GET['endpoint'];

//build the filename
$filepath = '/path/to/file/';
$filename = $start . $end . '.pdf';

//check to see that it actually exists first
if (file_exists($filepath . $filename))
{
  //send file to browser
  header('Content-type: application/pdf');
  header('Content-Disposition: attachment; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: ' . filesize($filepath . $filename));
  header('Accept-Ranges: bytes');

  readfile($filepath . $filename);
}

Sorry about the additional thread. I have this portion working now. But it still won't let me know load the pdf, It wont' allow the headers to modified in order to load it, thanks.

 

You can see the working script here...

http://movidstudios.com/wayfinder/directions.html

 

Thanks again for your help.

Post the code you're using. The html on the website doesn't show us anything. We need to see the PHP you're using.  But if you have a "headers already sent" error, it's most likely because you are sending output to the browser before it tries to send the pdf file...perhaps it's the echo'ing you're doing. No output can be sent before the headers.

This is the HTML

<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<center>
<h1> Get Directions</h1>
<form action="form_action.php" method="get" name="directions" target="_new">

<select name="startpoint" size="1">
<option value="north">North Tower Entrance</option>
  <option value="south">South Tower Entrance</option>
  <option value="moba">MOB A Entrance</option></select>
  
  ----->

<select name="endpoint" size="1">
<option value="onco">Oncology</option>
  <option value="radio">Radiology</option>
  <option value="pulm">Pulmanary</option></select>
<br /><br />
<input type="submit" value="Submit" />
</form>
</center>



</body>
</html>

This is the PHP

<?php
$a = $_GET["startpoint"];
$b = $_GET["endpoint"];

//build the filename
$filename = $a . $b . '.pdf';

echo "Your Startpoint is:". $a. "<br />";
echo "Your Endpoint is: ". $b. "<br />";
echo  $a,$b,".pdf". "<br />";
echo  $filename;

//check to see that it actually exists first
if (file_exists($filename))
{
  //send file to browser
  header('Content-type: application/pdf');
  header('Content-Disposition: attachment; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: ' . filesize($filename));
  header('Accept-Ranges: bytes');

  readfile($filename);
}
?>

I'm new to PHP coding so I might have messed it up, thanks.

 

Also I am only echoing to be sure I am getting the proper results, I can remove those, in the end it should just launch a PDF with corresponding file name. THanks.

Get rid of the echo statements. You can't have those before the header()'s. That's what I meant about "no output before the headers".

 

The way you are coding this also makes it so that the pdf files must be in the same directory as this form_action.php script since you didn't provide a path to the pdf's.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.