SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    urgent help with radio button working with text field working in php

    Okay I'm not sure how to put this right.. Let me try to explain.

    I want to use one text box and the info inside it will vary depending on radio button selection, screen shot...

    radio.JPG

    if this is the text box
    HTML Code:
    <input type="text" name="i" id="nomeff" value="<?php ?>" size="5" />
    and these are the radio button
    HTML Code:
    <input type="radio" name="rate" value="i">Effective Value <input type="radio" name="rate" value="r">Nominal value
    how are they going to integrate.

    >>>>>>>>>>>
    Previously I had 2 text boxes so I coded with php in that way..

    The php code
    PHP Code:

    <?php
    if(isset($_REQUEST['submit'])) {
        
    define("e""2.71828183");
        
    $i=trim($_REQUEST['i'])/100;
        
    $r=trim($_REQUEST['r'])/100;
        
        
    //to accomodate different compounding periods including "other"
        
    if(empty($_REQUEST['n2'])){
            
    $n=trim($_REQUEST['n']);
            }else{
            
    $n=trim($_REQUEST['n2']);
        }
        
    // Effective value Calculation
        
    if(empty($_REQUEST['r'])){
            if(
    $n == "Continuous" ){
                
    $result=(pow(e,$i)-1)*100;}
            else{
                
    $result=((pow(1+($i/$n),$n))-1)*100;}
                
                echo 
    "The effective value is ".number_format(round($result3), 3)."%";
        }
        
    // Nominal value Calculation
        
    if(empty($_REQUEST['i'])){
            if(
    $n == "Continuous"){
                
    $result=((log10($r+1))/(log10(e)))*100;}
            else{
                
    $result=((pow(($r+1), 1/$n)-1)*$n)*100;}
                
                echo 
    "The nominal value is ".number_format(round($result3), 3)."%";    
        }
    }
    ?>
    and this the text box html..

    HTML Code:
    <tr>
    			<td>Nominal annual interest rate:</td><td><input id="id_i" type="text" name="i" value="<?php if(!empty($_REQUEST['i'])){ echo trim($_REQUEST['i']);} ?>" size="5" /> <span class=nt>%</span></td>
    		</tr>
    		<tr>
    		  <td>Effective Interest Rate:</td><td><input id="id_i" type="text" name="r" value="<?php if(!empty($_REQUEST['r'])){ echo trim($_REQUEST['r']);} ?>" size="5" /> <span class=nt>%</span></td>
    		</tr>
    Now, how can I serve the same purpose with radio buttons and a text box instead of 2 text boxes,
    I hope i was able to explain it some what. Any help highly appreciated.

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,027
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ameerulislam View Post
    Okay I'm not sure how to put this right.. Let me try to explain.

    I want to use one text box and the info inside it will vary depending on radio button selection, screen shot...

    if this is the text box
    HTML Code:
    <input type="text" name="i" id="nomeff" value="<?php ?>" size="5" />
    Instead use

    HTML Code:
    <input type="number" name="i" id="nomeff" value="<?php echo $_REQUEST['i'] ?>" size="5">
    Older browsers will treat number as a text field anyway. Newer ones will filter the inputs automatically, smartphones visiting the site will present a numeric keypad instead of a full keyboard.

    and these are the radio button
    HTML Code:
    <input type="radio" name="rate" value="i">Effective Value <input type="radio" name="rate" value="r">Nominal value
    how are they going to integrate.
    If neither radio button is clicked, $_REQUEST['rate'] will not be set. Otherwise, it will have the value of the radio button that was checked when the form was submitted.

    I would recommend setting a default with the checked attribute and using the label tag for the radio button's label.

    HTML Code:
    <input type="radio" name="rate" value="i" checked id="effective"><label for="effective">Effective Value</label>
    The for attribute of label links it to the radio button. The value of the attribute must match the id of the radio button. Once set, it allows the radio button to be checked by clicking the label.

  3. #3
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Although these solutions didn't address my main problem, they are still priceless advise. I guess I was not able to articulate my self well. Any way I was able to solve the main problem.
    Thanks

    Quote Originally Posted by Michael Morris View Post
    Instead use

    HTML Code:
    <input type="number" name="i" id="nomeff" value="<?php echo $_REQUEST['i'] ?>" size="5">
    Older browsers will treat number as a text field anyway. Newer ones will filter the inputs automatically, smartphones visiting the site will present a numeric keypad instead of a full keyboard.



    If neither radio button is clicked, $_REQUEST['rate'] will not be set. Otherwise, it will have the value of the radio button that was checked when the form was submitted.

    I would recommend setting a default with the checked attribute and using the label tag for the radio button's label.

    HTML Code:
    <input type="radio" name="rate" value="i" checked id="effective"><label for="effective">Effective Value</label>
    The for attribute of label links it to the radio button. The value of the attribute must match the id of the radio button. Once set, it allows the radio button to be checked by clicking the label.

  4. #4
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    number seems to doesn't accept decimals. What can be done here?

  5. #5
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ameerulislam View Post
    number seems to doesn't accept decimals. What can be done here?
    Got it, step="any" attribute..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •