Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    I want to create a radio button option where the options are multiply, add or do both for two numbers that have been input by the user. I am able to use the do both option, but am unable to figure out how to link up the functions for add and multiply to the options for add and multiply. Yelp?

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    Show us your form (where the user enters the numbers and selects the radio button).
    And show us the script you have so far ... even if it doesn't work.


  4. #3
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    PHP Code:
    <?php
    error_reporting
    (0);
    ?>
    <?php
    $error
    =0;
        if(isset(
    $_REQUEST['submitn'])){
            
    $val1=$_REQUEST['firstvalue'];
            
    $val2=$_REQUEST['secondvalue'];
            
    $radio1=$_REQUEST['choose'];
            
    $radio2=$_REQUEST['choose'];
            
    $radio3=$_REQUEST['choose'];
            if(
    $val1 && $val2 && radio3)
                
    $totalval=maths($val1,$val2);
            else
                if(
    $val1 && $val2 && $radio1)
                
    $totalval=$add($val1,$val2);
                else
                    if(
    $val1 && $val2 && $radio2)
                    
    $totalval=$mul($val1,$val2);
                    else
                    
    $error=1;
                }
    ?>
    <?php
    /*PHP functions*/
                
    function maths($val1,$val2){
                
    $add=$val1+$val2;
                
    $mul=$val1*$val2;
                
    $values=array($add,$mul);
                return 
    $values;
                }
                function 
    add($val1,$val2){
                
    $add=$val1+$val2;
                return 
    $add;
                }
                function 
    mul($val1,$val2){
                
    $mul=$val1*$val2;
                return 
    $mul;
                }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>functions</title>
    </head>

    <body>
    <form action="" method="post">
        First Value: <input type="text" name="firstvalue" size="2" maxlength="2" value="<?php if(isset($_REQUEST['submitn'])) echo $val1?>"  /><br />
        Second Value: <input type="text" name="secondvalue" size="2" maxlength="2"
        value="<?php if(isset($_REQUEST['submitn'])) echo $val2?>"  /><br />
        Choose from the following:<br />Multiply<input type="radio" name="choose" value="mul" /><br />
        Add<input type="radio" name="choose" value="add"  /><br />
        Both Add & Multiply<input type="radio" name="choose" value="Do both" checked="checked" /><br />
        <input type="submit" name="submitn" value="Compute"  />

    </form>
    <?php
            
    if($error==1)
                echo 
    "Enter a value";
            else
            {
                if(isset(
    $_REQUEST['submitn'])){
                    echo 
    "The total is:$totalval[0]"."<br/>";
                    echo 
    "The product is:$totalval[1]";
                    }

            }
    ?>
     
    </body>
    </html>

  5. #4
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    I put my whole code in. My problem is, I can't figure out how to enable the selection of an option on radio buttons will produce the required result. I know how to do the multiply thing but can't figure this out. And my teacher is as unhelpful as ever.

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    There are several mistakes ... and too hard to explain them all,
    so I put in a bunch of comments and reworked some parts for you.

    Normally we don't do homework, but you've shown us your efforts ...

    PHP Code:

    <?php
    error_reporting
    (0);
    ?>
    <?php
    $error
    =0;
    // your form method is POST, so use $_POST ...
    // there will always be a radio button even if
    // they don't enter a value ... so we'll see which
    // button is selected.  There is only one variable
    // for all 3 of your radio buttons.  You only need
    // to see what the 'value' is.  It will be one of the
    // three values you defined in your form ...
    $radio=$_POST['choose'];
    // remember which radio button they selected ...
    if($radio == "mul"){
    $rad1='checked="checked"';
    }
    if(
    $radio == "add"){
    $rad2='checked="checked"';
    }
    if(
    $radio == "both"){
    $rad3='checked="checked"';
    }
    // default
    if(!$radio){
    $rad3='checked="checked"';
    }
        if(isset(
    $_POST['submitn'])){
            
    $val1=$_POST['firstvalue'];
            
    $val2=$_POST['secondvalue'];
      
    // default with an error and clear it if
      // any of the functions are called.
      
    $error=1;
      if(
    $val1 && $val2 && $radio == "add"){
      
    $error=0;
                
    $totalval=add($val1,$val2);
      }
      if(
    $val1 && $val2 && $radio == "mul"){
      
    $error=0;
                
    $totalval=mul($val1,$val2);
      }
      if(
    $val1 && $val2 && $radio == "both"){
      
    $error=0;
                
    $totalvals=maths($val1,$val2);
      }
        }
    /*PHP functions*/
                
    function maths($val1,$val2){
                
    $add=$val1+$val2;
                
    $mul=$val1*$val2;
                
    $values=array($add,$mul);
                return 
    $values;
                }
                function 
    add($val1,$val2){
                
    $add=$val1+$val2;
                return 
    $add;
                }
                function 
    mul($val1,$val2){
                
    $mul=$val1*$val2;
                return 
    $mul;
                }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/URL]">
    <html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>functions</title>
    </head>
    <body>
    <form action="" method="post">
        First Value: <input type="text" name="firstvalue" size="2" maxlength="2" value="<?php if(isset($_POST['submitn'])) echo $val1?>"  /><br />
        Second Value: <input type="text" name="secondvalue" size="2" maxlength="2"
        value="<?php if(isset($_POST['submitn'])) echo $val2?>"  /><br />
        Choose from the following:<br />Multiply<input type="radio" name="choose" value="mul" <?=$rad1?> /><br />
        Add<input type="radio" name="choose" value="add" <?=$rad2?> /><br />
        Both Add & Multiply<input type="radio" name="choose" value="both" <?=$rad3?> /><br />
        <input type="submit" name="submitn" value="Compute"  />
    </form>
    <?php
            
    if($error==1)
                echo 
    "Enter a value";
            else
            {
      
    // You have two functions that return one value ($totalval).
      // The other function returns two values (in an array).
      // So I gave that one a different name ($totalvals).
      // You have to echo an array differently than the single variable.
                
    if(isset($_POST['submitn'])){
        if(
    $totalvals){
        echo 
    "The result is: Add {$totalvals[0]} .... Multiply {$totalvals[1]}";
        }
        else{
                    echo 
    "The total is: $totalval"."<br/>";
                    }
      }
            }
    ?>

    </body>
    </html>


  7. #6
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    Yeah, I can actually tell my mistakes from seeing and comparing your code to mine. Thanks a ton, you're a lifesaver.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:22 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com