Page 1 of 2

php form

Posted: Thu Nov 17, 2005 4:10 pm
by pauspling
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

Posted: Thu Nov 17, 2005 4:28 pm
by nincha
redirect to the same page?? Or however those php chatrooms are made?

Yeah, redirect

Posted: Thu Nov 17, 2005 4:32 pm
by pauspling
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

Posted: Thu Nov 17, 2005 4:38 pm
by nincha
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?

Posted: Thu Nov 17, 2005 4:44 pm
by pauspling
Do you have any examples in code?

/Paulina

Posted: Thu Nov 17, 2005 4:54 pm
by Grim...

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
}

More info

Posted: Thu Nov 17, 2005 4:58 pm
by pauspling
I understand the first part of your reply, but not the last checking part. How can I output the data in a textfield as in the calculator example?

Posted: Thu Nov 17, 2005 5:03 pm
by Zoxive

Code: Select all

<input type="text" name="text" value="<?php if($_POST['text']){ print $_POST['text']; } ? >">
-NSF

Posted: Thu Nov 17, 2005 5:03 pm
by hawleyjr

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>

Posted: Thu Nov 17, 2005 5:06 pm
by Grim...
This calculator only adds up.

<edit>Now works! Note to self: Drink less ;)</edit>

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;
We just output the php variable into the text box's 'value' parameter.

Thanks

Posted: Thu Nov 17, 2005 5:29 pm
by pauspling
Thanks guys, you are really nice, but have any of you actually tested the code? Nothing really works... :)

/Paulina

Posted: Thu Nov 17, 2005 5:31 pm
by hawleyjr
Tested mine, works fine :)

Are you serious

Posted: Thu Nov 17, 2005 5:40 pm
by pauspling
Hi, are you seriously trying to help me?

Posted: Thu Nov 17, 2005 5:45 pm
by Grim...
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


hawleyjr wrote:Tested mine, works fine :)
The first input field needs the $fst_num, not $snd_num, and the ans_num field needs a value... :?

Posted: Thu Nov 17, 2005 5:50 pm
by Grim...
Yeah, bye, don't mention it...