Jump to content
Mindjakk

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

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.

Share this post


Link to post
Share on other sites

Wouldn't the easiest thing to do is to assign the values of the dropdowns to be numeric? Then take the combination of them to create the filename?

Share this post


Link to post
Share on other sites

That looks like it might work but what do I put in the form action section of the form? Do I move the javascript into a file or can I put it into the header? Thanks.

Share this post


Link to post
Share on other sites

What you did was great, my issue is getting it to actually launch the pdf through the php action script, thanks.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

The javascript was just showing how to build the filename from the inputs. You don't need it. You didn't describe what you wanted to do beyond just building a filename from the 2 selects.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

YOU WERE RIGHT!!

 

After removing the Echos, I was able to get it to launch the downloader for the pdf

 

I guess my next question is can we launch ti in the browser? Thanks.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Change

header('Content-Disposition: attachment; filename="' . $filename . '"');

to

header('Content-Disposition: inline; filename="' . $filename . '"');

Share this post


Link to post
Share on other sites

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.