php form
Moderator: General Moderators
php form
Hi!
I would like to create something that resembles a Java Applet in the sense that the user enters input in a "form" and gets info back in the same interface/window. So far I ahve only seen interafces that redirects the user to a new php-page. I want the user to be able to use the same form over and over again (the interface calculates a value depending on the input). How can I do this?
/Paulina
I would like to create something that resembles a Java Applet in the sense that the user enters input in a "form" and gets info back in the same interface/window. So far I ahve only seen interafces that redirects the user to a new php-page. I want the user to be able to use the same form over and over again (the interface calculates a value depending on the input). How can I do this?
/Paulina
Yeah, redirect
Yes, exactly like this...the output is published in the same inteface as input is entered. Like this calculator:
http://cs-people.bu.edu/mzou/php/calculator.php
How can I create one of these?
/Paulina
http://cs-people.bu.edu/mzou/php/calculator.php
How can I create one of these?
/Paulina
in your html form tag, set action to direct to the same page, which will cause the form to send all the inputs (variables) to the same page. in the beginning of your code(php), check if the inputs (variables) exist, if it exist then execute whatever, if it dosnt, then it means no information exists, so output default. this might be confusing, so feal free to ask question.
Code: Select all
$foo = $_POST(['$foo'];
if ($foo)
{
print "You entered ".$foo."!";
}
else
{
?>
<form method="post" action="thispage.php">
<input type="text" name="foo"><br><br>
<input type="submit" value="Go!">
</form>
<?php
}Code: Select all
<input type="text" name="text" value="<?php if($_POST['text']){ print $_POST['text']; } ? >">
Last edited by Zoxive on Thu Nov 17, 2005 5:04 pm, edited 2 times in total.
Code: Select all
//untested...
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" name="form1">
<input type="text" name="fst_num" size=7 maxlength="5" value="<?php echo $_GET['snd_num'];?>">
<select name="operator" size=1>
<option value="*" <?php echo $_GET['operator']=='*'?'selected':'';?>>*</option>
<option value="+" <?php echo $_GET['operator']=='+'?'selected':'';?>>+</option>
<option value="-" <?php echo $_GET['operator']=='-'?'selected':'';?>>-</option>
<option value="*" <?php echo $_GET['operator']=='*'?'selected':'';?>>x</option>
<option value="/" <?php echo $_GET['operator']=='/'?'selected':'';?>>/</option>
</select>
<input type="text" name="snd_num" size="7" maxlength="5" value="<?php echo $_GET['snd_num'];?>">
=
<input type="text" name="ans_num" size="9" value="">
<br>
<input type=submit value="Calculate">
<input type=reset value="clear">
</form>This calculator only adds up.
<edit>Now works! Note to self: Drink less
</edit>
We just output the php variable into the text box's 'value' parameter.
<edit>Now works! Note to self: Drink less
Code: Select all
<?php
$foo = $_POST['foo'];
$bar = $_POST['bar'];
if ($foo AND $bar)
{
$answer = $foo + $bar;
}
?>
<form method="post" action="thispage.php">
<input type="text" value="<?php
print $foo;
?>" name="foo">
+
<input type="text" value="<?php
print $bar;
?>" name="bar">
=
<input type="text" name="answer" value="<?php
print $answer;
?>">
<br><br>
<input type="submit" value="Add them!">
</form>
<?php
exit;
Last edited by Grim... on Thu Nov 17, 2005 5:42 pm, edited 1 time in total.
Are you serious
Hi, are you seriously trying to help me?
No, we thought we'd post some made-up code for fun.
Good Lord, boy, where's your respect?
I've changed mine round a bit to make it simpler, and uploaded it so you can see it working: http://www.lonford.co.uk/random/thispage.php

Good Lord, boy, where's your respect?
I've changed mine round a bit to make it simpler, and uploaded it so you can see it working: http://www.lonford.co.uk/random/thispage.php
The first input field needs the $fst_num, not $snd_num, and the ans_num field needs a value...hawleyjr wrote:Tested mine, works fine
Last edited by Grim... on Thu Nov 17, 2005 5:58 pm, edited 2 times in total.