number plus number then show result

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
eeau1973
Forum Commoner
Posts: 27
Joined: Sat Aug 19, 2006 4:49 pm

number plus number then show result

Post by eeau1973 »

hi ....

i would like to know how to ask to the user for two numbers ( maybe using a form )and then process those data to show the result (submitting those data)

thanks in advance
:D
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post by richmix »

Erm... might I ask why you want to do this?
eeau1973
Forum Commoner
Posts: 27
Joined: Sat Aug 19, 2006 4:49 pm

re:why i must do this ...

Post by eeau1973 »

thats because i wanna know how the User input data and then how is it collected via the form and processed by the script :)

thaks again ,,,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Basic math.

Code: Select all

$result = $num1 + $num2;
8O
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

HTML sender Page
==============

Code: Select all

.............................................
<form method = "get" action = "rcv.php">
  <input type="textbox" name="n1" />
  <input type="textbox" name="n2" />
</form>
...............................................
Reaciever php Script ( rcv.php )
======================

Code: Select all

<?php
  echo "n1 : ".$_GET[n1];
  echo "n2 : ".$_GET[n2];
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

neel_basu, you're missing quotes around the named elements of $_GET.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

although Its Good To Use Quoutes
But It would Work With / without qoutes
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post by richmix »

The basic form structure in HTML looks something like this:

Code: Select all

<form action="action.php" method="post">
<input type="hidden" name="inputHidden" value="This input is hidden.">
<input type="text" name="inputText">
<input type="checkbox" name="inputCheckbox">
<input type="submit" value="Submit">
</form>
Breakdown:

Code: Select all

<form action="action.php" method="post">
This bit makes sure the form sends all of its variables to the file action.php. Setting the method to post is necessary because you're using this form to SEND information rather than retrieve it.

Code: Select all

<input type=...
Here you define your input types. Each input type should have a unique name.

Now you need to set up your action.php file.

Code: Select all

<?php

/* This line extracts the POST variables; without it, your form variables will be part of the
$_POST array and look like $_POST['inputHidden'], $_POST['inputText'], etc. With this line,
your variables will look more presentable, such as $inputHidden, $inputText, etc. */

extract($_POST);

/* This line will echo "This input is hidden." */

echo $inputHidden;

/* This line will echo whatever is submitted from the text field. */

echo $inputText;

/* This chunk will echo "Yes" if the checkbox is checked and "No" if not. */

if ($inputCheckbox) {
     echo "Yes";
     exit;
     } else {
     echo "No";
     exit;
     }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:lol:

If you want your code to run without any errors firing, it's required. Even if you don't mind E_NOTICE errors firing, they still happen if you turn them off, thus slowing your application down.

edit: it should be noted that extract() can lead to security compromises if not treated carefully. We will generally suggest using isset() or array_key_exists() against $_POST instead.
Post Reply