Append form data to URL and send user to URL

时间:2016-04-04 17:37:32

标签: javascript php jquery forms

I'm a front end programmer so I work mostly with HTML and CSS. I have a client that wants to be able to type in a part number and be redirected to the corresponding part's sell sheet, which is a PDF. The code for the form is below. Basically I would like the person to type in "1234567" and then be redirected to http://www.website.com/PDFs/1234567.pdf.

What is the best way to achieve this? I'm not very well versed in scripting languages. Thank you in advance for taking the time to help me.

<form action="search.php" method="post">
     <input type="text" class="searchField" name="search" placeholder="Enter part number here">
     <input type="submit" class="searchBtn" value="Search"> 
</form>

2 个答案:

答案 0 :(得分:1)

I'm not sure what your search.php page is doing, but if you can be sure they type in a valid part number or handle the 404 page, then using a javascript function to redirect to the new url with that path will work.

<form method="post" onsubmit="get_sell_sheet(); return false;">
     <input id="part_number" type="text" class="searchField" name="search" placeholder="Enter part number here">
     <input type="submit" class="searchBtn" value="Search"> 
</form>

<script>
function get_sell_sheet(){
  var part_number = document.getElementById("part_number").value;
  var url = "http://www.website.com/PDFs/" + part_number + ".pdf";
  // open in same window
  // window.location.href = url;
  // open in new window
  var win = window.open(url, '_blank');
}
</script>

答案 1 :(得分:1)

To add on the above answer...

In PHP:

<?php
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
    header('Location: http://www.website.com/PDFs/' . $_POST['search'] . '.pdf');
    exit(0);
}

Add your own error handling where needed.