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.

Link to comment
Share on other sites

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);
}
Edited by CroNiX
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Edited by Mindjakk
Link to comment
Share on other sites

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.

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.