/    Sign up×
Community /Pin to ProfileBookmark

Combine submit form and ‘get’ form?

I want to combine my 2 PHP files into 1, which I’ve seen done, but can’t remember how or where. Here they are:

[code=html]<html>
<body>
<form action=”domath.php” method=”get” />
<p>
Miles: <input name=”distance” type=”text” value=”” size=”10″ maxlength=”10″ />
&nbsp;MPH:
<input name=”speed” type=”text” value=”22″ size=”10″ maxlength=”10″ />
&nbsp;&nbsp;&nbsp;<input type=”submit” value=”Submit” />
</p>
</form>
</body>
</html>[/code]

[code=php]<?php
$mile = $_GET[“distance”];
$mph = $_GET[“speed”];
$time = $mile / $mph * 60;
if ($time < 1)
{
$total = $time * 60;
$unit = ‘seconds’;
}
else
{
$unit = ‘minutes’;
$total = $time;
}
echo $total;
echo ‘ ‘;
echo $unit;
?>[/code]

So I have just one file, index.php (I’ll need to change domath.php to index.php)

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 27.2008 — I'm going to assume you have the rest of a page there, and some good error checking.
[code=php]<body>
<?php
if(isset($_GET["distance"]) && isset($_GET["speed"])
&& is_numeric($_GET["distance"]) && is_numeric($_GET["speed"])){
$mile = (float) $_GET["distance"]; // get numbers
$mph = (float) $_GET["speed"]; // get numbers
$time = $mile / $mph * 60;
if($time < 1)
{
$total = $time * 60;

$unit = 'seconds';
}
else
{
$unit = 'minutes';
$total = $time;
}
echo $total . ' ' . $unit;
};
?><form action="domath.php" method="get" />
<p>
Miles: <input name="distance" type="text" value="" size="10" maxlength="10" />
&nbsp;MPH:
<input name="speed" type="text" value="22" size="10" maxlength="10" />
&nbsp;&nbsp;&nbsp;<input type="submit" value="Submit" />
</p>
</form>
</body>
</html>
[/code]

or if you only wanted to show the form if no data is entered you can just replace the };with }else{ and drop this at the end of the form:[code=php]<?php } ?>[/code]
×

Success!

Help @njven spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...