PDA

View Full Version : Avoiding 'post' Method



miradoro
04-26-2005, 03:05 PM
hI

MY ORIGINAL FORM IS :

<form name="frmSearch" method="post" action="body_dwm_products.php">
Enter Part Number to Search For. For this demo, product IDs are 30201029 and sb0589: <br>
<input name="part_num" type="text" id="part_num">
<input type="hidden" name="action" value="search">
<input type="submit" name="Submit" value="Search">
<?php require_once('form_error.php'); ?>
</form>


AS YOU CAN SEE IT GOES TO ANOTHER PHP PAGE...I WOULD LIKE TO KNOW IF I CAN AVOID THAT...

body_dwm_products.php IS AS FOLLOW:

<form name="frmSearch" method="post" action="body_dwm_products.php">
Enter Part Number to Search For. For this demo, product IDs are 30201029 and sb0589: <br>
<input name="part_num" type="text" id="part_num">
<input type="hidden" name="action" value="search">
<input type="submit" name="Submit" value="Search">


<?
// user sets the file name here.
$data_file = "testdb.txt";
$prt = $_POST['part_num'];
$dataset = file("testdb.txt");
$pdfPath = "";
if( $prt != "" && $_POST['action']=="search" )
{
for( $i = 0; $i < count( $dataset ); $i++ ){
// extract the record key and compare
$dataset[$i] = trim( $dataset[$i] );
$record_key = strtok( $dataset[$i], " " );
$record_key = trim( $record_key );
if( strcmp( $record_key, $prt ) == 0 ) {
$stringArray = explode( " ", $dataset[$i], 2 );
$pdfPath = trim( $stringArray[1] );
?>
<script language="JavaScript" type="text/javascript">
window.open('<?echo($pdfPath);?>');
</script>
<?
break;
} // end of if
} // end of for
} // end of outermost if

// if it gets to this point, the record was not found in the file.
?>
<font color="red">
<?
$error_msg = "";
if( $pdfPath == "" ){
$error_msg = "no part was found for $prt";
}
echo( $error_msg );
?>
</font>
</form>




hook me up

BYE

mwinter
04-26-2005, 08:56 PM
<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.


<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,
<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

miradoro
04-29-2005, 02:25 PM
i havent tested your code but i will this coming week...
i guess my title was confusing...all i wanted is to redirect the page to itself..

thanks for the input and i will get back to you next weeks if i have any questions

later