Author Topic: Generating two random numbers in a texarea and adding their values.  (Read 650 times)

0 Members and 1 Guest are viewing this topic.

Offline knoahgTopic starter

  • Irregular
  • Posts: 1
    • View Profile
Hi people im still a begginer in PHP this is the code i have so far and it only accepts entering values and adding with the button. So how can i generate two random numbers on the two boxes and calculate the result.
Thank You.

<form name="form1" method="post" action="">
   value1:
     <label>
  <input type="text" name="value1">
  </label>
  <p>&nbsp;</p>
  <p>
    <label>
    value2: 
    <input type="text" name="value2">
    </label>
  </p>

 
  <p>
    <label>
    <input type="submit" name="submit" value="calculate">
    </label>
   </form>
<?php
$_POST[calculate] =="add"; {
$result = $_POST[value1] + $_POST[value2];
}
echo $result;
?>


[attachment deleted by admin]

Offline The Little Guy

  • Freak!
  • Posts: 6,103
  • Gender: Male
  • Jinkies!
    • View Profile
    • PHPSnips
Re: Generating two random numbers in a texarea and adding their values.
« Reply #1 on: March 19, 2010, 01:17:18 AM »
You can do it like this:

Code: [Select]
<form name="form1" method="post" action="">
   value1:
     <label>
  <input type="text" name="value1" value="<?php echo rand(01000000); ?>">
  </label>
  <p>&nbsp;</p>
  <p>
    <label>
    value2: 
    <input type="text" name="value2" value="<?php echo rand(01000000); ?>">
    </label>
  </p>

 
  <p>
    <label>
    <input type="submit" name="submit" value="calculate">
    </label>
   </form>
<?php
if($_POST[calculate] == "add"){
$result $_POST['value1'] + $_POST['value2'];
}
echo 
$result;
?>
« Last Edit: March 19, 2010, 01:18:29 AM by The Little Guy »
phpLive - A powerful library that implements many common tasks to make php programming faster. Supports extensions and plugins. Current version: 1.0.0-Alpha
JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In
http://dreamhost.com (promo code: 8RN4)
$30 off 1 year of hosting
$40 off 2 years of hosting