
Originally Posted by
miradoro
<form name="frmSearch" method="post" action="body_dwm_products.php">
[...]
AS YOU CAN SEE IT GOES TO ANOTHER PHP PAGE...I WOULD LIKE TO KNOW IF I CAN AVOID THAT...
I'm a little confused as to what you're after. The topic is regarding avoiding POST requests, but now you're writing about avoiding submitting to another page. Do you mind clearing that up?
Using a GET request is probably preferable as this just seems to be a database query (of sorts). If an operation is idempotent and there is no private information that needs to be propagated, GET requests are preferable.
If you're after just one script that handles both the search form and its results, I'd say that was a good idea, too.
PHP Code:
<form action="" method="get">
<div>
<label for="part-num">Part number:
<input id="part-num" name="part_num" type="text"
value="<?php echo $_GET['part_num']; ?>"></label>
<input type="submit" name="action" value="Search">
<p class="note">For this demonstration, product IDs are
30201029 and sb0589.</p>
</div>
</form>
<?php
if(!empty($_GET['action']) && !empty($_GET['part_num'])
&& ($_GET['action'] == 'Search'))
{
$dataFile = 'testdb.txt';
$part = $_GET['part_num'];
$dataset = file($dataFile);
for($i = 0; $i < count($dataset); $i++) {
$dataset[$i] = trim($dataset[$i]);
$record_key = strtok($dataset[$i], " ");
$record_key = trim($record_key);
if(0 == strcmp($record_key, $prt)) {
$stringArray = explode(" ", $dataset[$i], 2);
$pdfPath = trim($stringArray[1]);
?>
<p>Documentation for <a href="<?php echo $pdfPath; ?>"><?php echo $part; ?></a>.</p>
<?php
break;
}
}
}
if(!$pdfPath) {
?>
<p class="warning">No part was found for part number <?php echo $part; ?>.</p>
<?php
}
?>
I haven't bothered to test this as I don't know your file format. I'll leave any debugging to you.
You may notice a few things:
- The client-side script has been removed. With even IE6 now supplying pop-up blockers, trying to open an unrequested pop-up is almost certainly doomed to failure.
- The font element has been removed and a paragraph with the class, 'warning', has been added in its place. You can use a style sheet to style it however you like. For instance,
HTML Code:
<style type="text/css">
p.warning {
background-color: inherit;
color: red;
}
</style>
in the head element. - The hidden input element has been removed. Instead, the submit button will send the action value (though with a capital 'S' in 'search').
Mike
Bookmarks