php form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pauspling
Forum Newbie
Posts: 24
Joined: Thu Nov 17, 2005 4:06 pm

php form

Post 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
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

redirect to the same page?? Or however those php chatrooms are made?
pauspling
Forum Newbie
Posts: 24
Joined: Thu Nov 17, 2005 4:06 pm

Yeah, redirect

Post 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
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post 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.
pauspling
Forum Newbie
Posts: 24
Joined: Thu Nov 17, 2005 4:06 pm

Code?

Post by pauspling »

Do you have any examples in code?

/Paulina
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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
}
pauspling
Forum Newbie
Posts: 24
Joined: Thu Nov 17, 2005 4:06 pm

More info

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

<input type="text" name="text" value="<?php if($_POST['text']){ print $_POST['text']; } ? >">
-NSF
Last edited by Zoxive on Thu Nov 17, 2005 5:04 pm, edited 2 times in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
Last edited by Grim... on Thu Nov 17, 2005 5:42 pm, edited 1 time in total.
pauspling
Forum Newbie
Posts: 24
Joined: Thu Nov 17, 2005 4:06 pm

Thanks

Post by pauspling »

Thanks guys, you are really nice, but have any of you actually tested the code? Nothing really works... :)

/Paulina
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Tested mine, works fine :)
pauspling
Forum Newbie
Posts: 24
Joined: Thu Nov 17, 2005 4:06 pm

Are you serious

Post by pauspling »

Hi, are you seriously trying to help me?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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... :?
Last edited by Grim... on Thu Nov 17, 2005 5:58 pm, edited 2 times in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Yeah, bye, don't mention it...
Post Reply