Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Form and if statement help please

    I have been trying to do a form using javascript and found it hard so I switched to php which seems easier. I am a learner so please take that into account. I put this in the code

    Code:
     if ($Storey == "Single_Storey")
    {
    echo ("<b>Correct</b>");
    }
    else
    {
    echo ("<b>Incorrect</b>");
    }
    from html form

    Code:
    <input type=radio name='Storey' value='Single_Storey'>Single Storey<br>
    <input type=radio name='Storey' value='Two_Storey'>Two Storey<br>}
    and it prints
    if ($Storey == "Single_Storey") { echo ("Correct"); } else { echo ("Incorrect"); }
    to the browser screen.

    What I am actually trying to eventually do is say if the value of Storey is Single_Storey then Storey value is 1, else 2 (Two_Storey). Hope this makes sense. Any help is appreciated

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Can't really help you much with the code snippet you provided.
    Post more of the code, especially the part where the form tags are and which files are involved as well as the filenames.

    Otherwise, the problems I see are:
    1. do you have php installed?
    2. If yes, the first code snippet provided, are they enclosed in tags?
    PHP Code:
    <?php
    // your code here
    ?>
    3. $Storey really ought to be
    PHP Code:
    if(isset($_REQUEST['Storey']) && strcasecmp($_REQUEST['Storey'], 'Single_Storey') == 0
    1st argument in the conditional basically checks if the variable 'Storey' exists from a form submit of type "post" or "get".
    2nd argument compares the value of 'Storey' with 'Single_Storey', returns 0 if they're equal. I don't like using equality sign for comparing string values and strcasecmp is a case insensitive string comparison function.
    4. there's an extra curly brace at the end of your second input tag; not sure if that's a typo or intentional

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you for replying.
    What I have been trying is when a radio button is selected I can then work a value into the output.

    Code:
    <?php
    include("global.inc.php");
    $errors=0;
    $error="The following errors occured while processing your form input.<ul>";
    $sum_total = $Length * $Width;
    $area_totalcost = $sum_total * 1000;
    $valley = $area_totalcost / 10; 
    
    pt_register('POST','Email');
    pt_register('POST','Length');
    pt_register('POST','Width');
    pt_register('POST','Storey');
    if($Email=="" || $Length=="" || $Width=="" || $Storey=="" ){
    $errors=1;
    $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
    }
    if (!is_numeric($Length)) 
    {     echo' Length is not numeric please go back and try again'; 
    } 
    if (!is_numeric($Width)) 
    {     echo 'Width is not numeric please go back and try again'; 
    } 
    
    
    
    if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
    $error.="<li>Invalid email address entered";
    $errors=1;
    }
    if($errors==1) echo $error;
    else{
    $where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
    $message="Email: ".$Email."
    Length: ".$Length."
    Width: ".$Width."
    Storey: ".$Storey."
    Area: ".$sum_total."
    Cost:  ".$area_totalcost."
    ";
    $message = stripslashes($message);
    mail("me@mywebsite.co.uk","Form Submitted at your website",$message,"From: phpFormGenerator");
    $make=fopen("admin/data.dat","a");
    $to_put="";
    $to_put .= $Email."|".$Length."|".$Width."|".$Storey."
    ";
    fwrite($make,$to_put);
    ?>
    
    
    <!-- This is the content of the Thank you page, be careful while changing it -->
    
    <h2>Thank you!</h2>
    
    <table width=50%>
    <tr><td>Email: </td><td> <?php echo $Email; ?> </td></tr>
    <tr><td>Length: </td><td> <?php echo $Length; ?> </td></tr>
    <tr><td>Width: </td><td> <?php echo $Width; ?> </td></tr>
    <tr><td>Storey: </td><td> <?php echo $Storey; ?> </td></tr>
    <tr><td>Area: </td><td> <?php echo $sum_total; ?> </td></tr>
    <tr><td>Area Cost: </td><td> <?php echo '&pound'; ?> <?php echo$area_totalcost; ?>  </td></tr>
    <tr><td>Valley Cost: </td><td> <?php echo '&pound'; ?> <?php echo$valley; ?>  </td></tr>
    
    
    
    </table>
    
    <!-- Do not change anything below this line -->
    
    <?php 
    }
    ?>
    Code:
    <form enctype='multipart/form-data' action='process.php' method='post'>
    <input type=text name='Email' size=25>
    <input type=text name='Length' size=10>
    <input type=text name='Width' size=10>
    <input type=radio name='Storey' value='Single_Storey'>Single Storey<br>
    <input type=radio name='Storey' value='Two_Storey'>Two Storey<br>
    
    
    <input type=submit value='Submit Form'>
     <input type=reset value='Reset Form'></font>
    </form>
    I left out my attempt at putting in the if statement because the script don't run.

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by shadowmaniac View Post

    if(isset($_REQUEST['Storey']) && strcasecmp($_REQUEST['Storey'], 'Single_Storey') == 0)
    I tried this and understand that if 'Single_Storey' is selected the value is 0. Can someone please show me how I put a 'else' value ie: making the Two_Storey value 10 so that I can use the value. Help please.

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Can someone please help. I tried this in the code and it runs but doesn't do anything.
    Code:
    if($Storey > ( 0 )){
    $sum_total * 2;
    }

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by malkie View Post
    I tried this and understand that if 'Single_Storey' is selected the value is 0. Can someone please show me how I put a 'else' value ie: making the Two_Storey value 10 so that I can use the value. Help please.
    That is not correct, exactly. The value of 0 is returned from the function comparing the strings. That value does not go anywhere outside of the "if" statement check. In otherwords, the value "0" is not a useable value assigned to anything - it is only used to compare values.

    The code snippet presented does not actually do anything with the findings of that check, either. A more complete snippet would be something like this:
    PHP Code:
    if(isset($_REQUEST['Storey']) && strcasecmp($_REQUEST['Storey'], 'Single_Storey') == 0){
        
    //this is where you do the "Single_Storey" stuff...
        
    echo "<b>Correct</b>";
    }
    else{
        
    //this is where you do the "anything other than Single_Storey" stuff...
        
    echo "<b>Incorrect</b>";

    Some thoughts to add to the discussion...

    Using $_REQUEST is not a good standard practice to take up. It's an old habit for some folks, but there is no reason to get a beginner accustomed to using $_REQUEST because of its potential for abuse by malicious users.

    To the OP: It makes more sense to use values of "1" and "2" for the "Storey" variable since we are dealing with only values of "1" and "2". Then you don't need to transform anything. Just test them to see if the value is ==1 or ==2 and then act accordingly. That's more of a matter of preference, but it seemed worth mentioning.

    I also don't like the idea of testing for one case only, and then assuming that all failures of that case are automatically the other option. I tend to test for each specific case, and then return error messages to anything that doesn't match one of the expected, "approved" cases. (This is part of a more robust form solution that would be accompanied by client-side script to check before even sending the form to the server.)
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    malkie (05-20-2011)

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you for your reply and I appreciate what you say. I will spend some time doing what you say as it seems a better way forward for me, easier, using the 1 & 2 values.
    Last edited by malkie; 05-20-2011 at 07:55 PM. Reason: logic


  •  

    Posting Permissions

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