View Full Version : Form and if statement help please

05-19-2011, 01:37 PM
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

if ($Storey == "Single_Storey")
echo ("<b>Correct</b>");
echo ("<b>Incorrect</b>");

from html form

<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

05-19-2011, 02:14 PM
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?

// your code here

3. $Storey really ought to be

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

05-19-2011, 02:52 PM
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.

$error="The following errors occured while processing your form input.<ul>";
$sum_total = $Length * $Width;
$area_totalcost = $sum_total * 1000;
$valley = $area_totalcost / 10;

if($Email=="" || $Length=="" || $Width=="" || $Storey=="" ){
$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";
if($errors==1) echo $error;
$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");
$to_put .= $Email."|".$Length."|".$Width."|".$Storey."

<!-- 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>


<!-- Do not change anything below this line -->


<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>

I left out my attempt at putting in the if statement because the script don't run.

05-19-2011, 06:14 PM
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.

05-20-2011, 10:20 AM
Can someone please help. I tried this in the code and it runs but doesn't do anything.

if($Storey > ( 0 )){
$sum_total * 2;

05-20-2011, 04:24 PM
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:

if(isset($_REQUEST['Storey']) && strcasecmp($_REQUEST['Storey'], 'Single_Storey') == 0){
//this is where you do the "Single_Storey" stuff...
echo "<b>Correct</b>";
//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.)

05-20-2011, 07:54 PM
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.:)