handing calculations back to a 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

Post Reply
macisaacj
Forum Newbie
Posts: 2
Joined: Sun Jan 17, 2010 1:44 pm

handing calculations back to a form

Post by macisaacj »

I'm creating a PHP page that reads data from form inputs, does calculations and spits it back out to more input boxes in the form. So if you were to type your first and last names in two input boxes, in the same form in another input box it could regurg back first and last in one box. I know the js equivalent is document.form.input.value = names;

not sure how to get it in PHP, have tried severel variations of different things and keep getting errors. the above js output prompted an unexpected '='

Any help would be greatly appreciated

Dean
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: handing calculations back to a form

Post by mellowman »

this is just a simple html forum that submits to a script and it use the $_POST function after your calculations...

ok your probably confused as hell by what i mean...ill explain :drunk:

here is a simple adding script which sends it to add.php<---a new page :mrgreen: .

index.php

Code: Select all

 
<form action="add.php" method="post">
  <p>First number:
    <input type="text" name="number1" />
    + Second number:
    <input type="text" name="number2" />
    <input type="submit" value="Submit" />
  </p>
  </form> 
 


And here is the add.php..its pretty simple to understand that im pulling 2 numbers from the form and im just adding 2 variable together :lol:.But then i put the 2 variables that we stored previously into the same kind of html fields as before :]

both of your submited numbers are blue
there added together when its green
then it just spits its all back out into the fields in teal

add.php

Code: Select all

 
 
<?php 
[color=#0000FF]$number1 = $_POST["number1"];[/color]
[color=#0000FF]$number2 = $_POST["number2"];[/color]
[color=#00FF00]$number1 + $number2 = $addednumbers;[/color]
?>
 
[color=#40FFFF]<form action="add.php" method="post">[/color]
  [color=#40FFFF]<p>First number:[/color]
    [color=#40FFFF]<input type="text" name="number1" value="<? php echo $number1 ?>" />[/color]
 [color=#40FFFF]   + Second number:[/color]
   [color=#40FFFF] <input type="text" name="number2" value="<? php echo $number2 ?>"  />  = <? php echo $addednumbers; ?> [/color]
    [color=#40FFFF]<input type="submit" value="Submit" />[/color]
[color=#40FFFF]  </p>[/color]
  [color=#40FFFF]</form> [/color]
 

Hope this helped...if u need more help just aim me :]
imcmellowman
macisaacj
Forum Newbie
Posts: 2
Joined: Sun Jan 17, 2010 1:44 pm

Re: handing calculations back to a form

Post by macisaacj »

ok so I thnk I understand, but does it have to leave the original form page and then go back? I'm actually making a BMI calculator, Yes I know they exist. mostly in JS. I'm trying to build my chops by creating from scratch. From there I am going to incorporate more complex modules and tie it into a MySQL db.

the js version never leaves the page, oh wait it does... the html calls the js and then puts the numbers in the proper form inputs below the inputs you had filled out...

THANKS SO MUCH for the help, will let you know what comes of it

DM
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: handing calculations back to a form

Post by mellowman »

well there is a easy solution to keeping it on the same page...just put the form into a iframe...when it refreshes the page it will just refresh the box instead of the page 8)
Post Reply