0

I am trying to teach myself PHP, im trying to generate a form that validates in php Im having issues with my checkbox value calculations I know i should construct a function Im trying to make it so if the user clicks the mealplan box the appropriate price gets added to the convention cost.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body
{

background-color:#FF00FF;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
h1 {text-align:center;
}
</style>
<title>Assignment3</title>
</head>
<body>

<h1>Personal Information </h1>

<?php 
// define the variables
$fName = $_POST["fName"];
$lName = $_POST ["lName"];
$city = $_POST ["city"];
$state = $_POST ["state"];
$zipCode = $_POST["zipCode"];
$other = $_POST ["other"];
$chkOne = $_POST["chkOne"];
$chkTwo = $_POST["chkTwo"];
$chkThree = $_POST["chkThree"];
$mealPlan = $_POST[""];
  //1 to 1000
//generate conf id number
$confId = rand(1,1000);
$no_errors = TRUE;
$total = 0;

//begin form filter
if(!isset($_POST['mealPlan']) && 
   $_POST['mealPlan'] == 'Yes') 
{   $mealPlan== 'Yes';
     $no_errors = FALSE;
}

checkInput();
calcPrice();
printConf();


//checkInput
function checkInput() {

if (filter_has_var(INPUT_POST, "chkOne")){
  print "<p>ONE DAY CONFERENCE</p> \n";
  $total += filter_input(INPUT_POST, "chkOne");

}

if (filter_has_var(INPUT_POST, "chkTwo")){
  print "<p>TWO DAY CONFERENCE</p> \n";
  $total += filter_input(INPUT_POST, "chkTwo");

}

if (filter_has_var(INPUT_POST, "chkThree")){
  print "<p>THREE DAY CONFERENCE</p> \n";
  $total += filter_input(INPUT_POST, "chkThree");

}
//end $total 


if (filter_has_var(INPUT_POST, "mealPlan")){

}

if (filter_has_var(INPUT_POST, "noFood")){

}
// do they want the meal plan

$track = filter_input(INPUT_POST, "track");
// what conference track are they on 


if(empty($fName))
{
 $no_errors = FALSE;

echo '***FIRST NAME FIELD REQUIRED***** <br />' ;


    }
    //get users first name



if(empty($lName))
{
$no_errors = FALSE;

 echo '****LAST NAME FIELD REQUIRED***** <br /> ';


    }
    // get users last name

if(empty($city))
{
$no_errors = FALSE;

 echo '****CITY FIELD REQUIRED***** <br /> ';

    }
    //get user city

if(empty($state))
{
$no_errors = FALSE;

 echo '***STATE NAME FIELD REQUIRED***** <br /> ';
    }
//get user state


if(empty($zipCode))
{
$no_errors = FALSE;

 echo '****ZIPCODE FIELD REQUIRED***** <br /> ';

    }
//get user zip code



if(empty($other))
{
$no_errors = FALSE;



 echo '****TEXT FIELD REQUIRED***** <br /> ';

}
//get user additional comments


if ($no_errors == FALSE) {

echo "<br> <a href='major1progress.html'>Go back to form</a>";
} 
    }//end function checkInput

//beging function calculateCost
function calcPrice(){

    $confTotal =filter_input(INPUT_POST,"confTotal");
    $total = filter_input (INPUT_POST, "total");
    $mealPlan = filter_input (INPUT_POST, "mealPlan");

    IF (! filter_has_var (INPUT_POST, "mealPlan" )){
    $total =="100" ; $confTotal = $total+= 50;
    }
    else if ($total=="175"){ $confTotal = $total +=75;
    }
    else if ($total =="225"){$confTotal = $total +=100;
$no_errors == TRUE;
    }}
//end function cacluclateCost






//begin function printConf
function printConf(){
$no_errors = FALSE;

if ($no_errors == TRUE) {
print  " <p> $fName $lName, <br />you have registered for Nerd Roundup 2013<br />
            your planned track is $track<br />
            The total cost is: \$$total <br />
            <br />  Please make sure to bring cash, or your credit card to   
            pay for the conference on the first day<br />  
            Your additional comments are as follows: 
            $other <br /> </p>
<p>Your Nerd Roundup confirmation number is  $confId .</p> <br /> \n";

 }
    }
 //end function printConf







?>

</body>
</html> 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

<style>

body
{
color:black;
font-size:20px;

}
h1
{
color:orange;
text-align:center;
font-size:30px;

}
p
{

font-family:"Times New Roman";
font-size:20px;

}
</style>

<title>MAJOR PROJECT 1 PHP </title>

</head>
<body>

<h1>Nerd Roundup </h1>


<form method = "POST"
      action = "http://localhost/Assignments php class/threeprogress.php">
  <fieldset>
    <label>
      Please type your  first name:
    </label>
    <input type = "text"
           name = "fName"
           value = "" /> <br />
            <label>
      Please type your  Last name:
    </label>
    <input type = "text"
           name = "lName"
           value = "" /> <br />


    Please type your  city:
    <input type = "text"
           name = "city"
           value = "" /> <br />
             Please type your  State:
    <input type = "text"
           name = "state"
           value = "" /> <br />
    Please type your  Zipcode:
    <input type = "text"
           name = "zipCode"
           value = "" /> <br />

<label for="other">Anything else you want to add?</label>
 <textarea id="other" name="other">
2
Contributors
1
Reply
21
Views
4 Years
Discussion Span
Last Post by ryantroop
0

First.. I don't see a checkbox anywhere.. so you may want to check your html..

Second, to me you are using php incorrectly...

PHP is a back end system, which allows for "secure" actions. You can easily move most of what you are doing to the front end with pure html, or html and javascript. Send the data to php for parsing and validation, and return your response..

For example, if you actually had check boxes, you could assign them a simple value depending on how you want to handle the data in the php.. you can give them bitwise values, or simply give them a unique name and give it a value of 1 and, as you are doing already, add a value based on the flag of 1 for checked, and 0 for not.

Before you go hopping off into the world of php, make sure your fundamental html is solid, and you understand how forms work and what options you have available.

Then, with php, you need to think about how to handle forms as a single piece, and how to manage validation. User input data is always always always dirty, and you can assume that even someone who is NOT ACTIVELY TRYING TO BREAK YOUR WEBSITE is passively trying to do it instead. With PHP, your first assumption is always going to be "incomnig data always needs to be validated" whether that means you only accept numbers, specific patterns of text, or just outright stripping out content that is harmful to the processes and functions on your page.

The data itself is always secondary. What does it mean to your code? If it's valid, what does it mean and how do you use it? Why do you have to break up all your data processing into 5 functions, when it can easily be done with 1 function that handles all the data that you expect, and simply discards the stuff you don't want?

Also, you don't need to do filter_has_var.. it's generally more common to see

if (isset($_GET['var']))

or, alternatively,

if (!isset($_GET['var']))...

Same thing, and to many a lot easier to read.

The filter functions are meant to make your life easier when validating data.. do you have a valid email? a valid ip address? etc... a shame they don't do a filter for phone numbers... for those we must use regex...

The best advice I can give you is do one of two things...

One - take some classes about php or programming in general. In fact, if you take some C or C++ courses, you will pick up php and feel right at home, but be very angry that you don't have to type cast.

If you pick up Python, on the other hand, PHP will feel very friendly and familliar.. but you may ask yourself why you're bothering with PHP when Python can do everything you want with PHP and then some, while running circles around PHP... but I digress.. I love PHP and Python, so don't get me wrong here....

anyhow.. two - find a new, up and coming website, or make friends with an experienced admin who runs personal projects, who will let you debug/read through their code, and learn from reading their code. You would be amazed how much you can learn from seeing well written code from an experienced developer. Just start small, and figure out what's going on by reading through the steps.

Sorry for the long windedness... I hope I helped..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.