Forum Moderators: coopster

Message Too Old, No Replies

PHP redirect, refresh, hidden

         

RussVass

11:37 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



I am learning PHP and i got into stupid problem.
I am using LAMP server to run some of the little apps I create.
Recently I made a converter just for practice because it seems to quite easy. I got HTML part of it.

<html>
<head> </head>
<body>
<form method="post" action="test.php">
<h2> Convert length: </h2>
<p>
Select conversion direction:
<br>
<input type="radio" checked="checked" value="1" name="button">
Feet to meters
<br>
<input type="radio" value="2" name="button">
Meter to feet
<br>
</p>

<p>
Value to be converted:
<br>
<input type="text" name="cvalue">
</p>

<p>
<input type="submit"value="Convert">
</p>

</form>

</body>
</html>

Then I made this PHP file, test.php here is the code:
The code basicaly converts from feet to meter and back, at the same time it stores the each conversion in TXT file on server called conversions.txt.

<?php

if ($_POST['button']=="1")
{
$result=($_POST['cvalue']) / 3.2808399;
echo ($_POST['cvalue'])."Feet=".$result."Meters";
$fp = fopen ('conversions.txt', 'a');
fwrite($fp,"Your latest result =".$result."Meters,");
fclose($fp);

}
else
{
$result=($_POST['cvalue']) * 3.2808399;
echo ($_POST['cvalue'])."Meters=".$result."Feet";
$fp = fopen ('conversions.txt', 'a');
fwrite($fp,"Your latest result =".$result."Feet,");
fclose($fp);

}




//$fp = fopen ('conversions.txt', 'a');
//fwrite($fp,"Your latest result =".$result);
//fclose($fp);

?>

If you would run this code you will see that it works good and neat. The question is how to make first page, that you got from HTML code to stay the same after clicking Convert button which pushes it into PHP script. And Show the result right under into reapering box insted of next page.
I been googling the redirection, but all i got is header redirect which not working properly because i have server data comunication.
I might will find the hidden box tutorial, but i have no clue how to make page not to jump to /text.php and just give result to hidden box. Please help :)

Readie

11:42 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World RussVass.

Just remove the action="" attribute from the <form> and it will post the data to the same page. You can then have the PHP calculation in the same file, by doing something like this:

if(isset($_POST['cvalue']) && !empty($_POST['cvalue'])) {
// Do processing here
// Declare it as a variable
// echo the variable underneath your form
}

Also, I noticed this:

<input type="submit"value="Convert"> 

You need a space after "submit" to be standards compliant HTML there.

RussVass

11:58 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Cool! Thanks, let me play with it ;)