APD1993 Posted January 30, 2012 Share Posted January 30, 2012 I have two files with coding in. One of them is the HTML form file: <?php <html><head><title>Car Accident Program</title></head> <body> <!----In this block of code I am creating a form with 4 text boxes and a button as well as user prompts to get user inputted values to work with----> <h4>Car Accident Report Form</h4> <form action="Car.php" method="post"> <b>First Name:<b><br> <input type="text" size = "45" name="firstname"><br> <b>Surname:<b><br> <input type="text" size = "45" name="surname"><br> <b>Age:<b><br> <input type="text" size = "45" name="age"><br> <b>Number of weeks since accident:<b><br> <input type="text" size = "45" name="weeks"><br> <input type="submit" value="Submit report"> </form> </body> </html> and the PHP/Validation file: <!----In this block of code, I am creating a PHP script that gets the user inputted values and can display them in a report as well as use an IF statement to show an extra line to appear if the user enters an age below 18 or a time since accident below 1 week or if they miss out a field or more----> <?php $firstname= $_POST ['firstname']; $surname =$_POST ['surname']; $age=$_POST ['age']; $weeks=$_POST ['weeks']; //Here, I am providing various paths and the outcomes in a PHP script if (empty($_POST['firstname']) or empty($_POST['surname']) or empty($_POST['age']) or empty($_POST['weeks'])) {$msg= "You missed out one or more fields. Click on the link below to go back to the form and enter information into all of the fields";} else if (is_numeric($age) && $age<0) {$msg="You cannot be under 0 years of age";} else if (is_numeric($weeks) && $weeks<0) {$msg="The number of weeks since an accident cannot be below 0";} else if (is_numeric($age) && $age>0 && $age<18) {$msg= "You are too young to file an accident report";} else if (is_numeric($weeks) && $weeks<2) {$msg= "You cannot file an accident report that happened less than two weeks ago";} else { setcookie(" $msg= "First Name: $firstname<br>"; $msg .="Surname: $surname<br>"; $msg .= "Age: $age<br>"; $msg .="Number of weeks since accident: $weeks<br>"; $msg .="Your report has been accepted. Please click on the link below to go back to the Accident Report Page";} echo ($msg) ?> </body> </html> <html> <a href="http://localhost/Car.htm"><br><br>Click here to add/edit an Accident Report</a> I was wondering how, if suitable, I would add a cookie or session into coding like this. Any help is appreciated, Andrew Link to comment https://forums.phpfreaks.com/topic/256045-how-if-possible-could-i-implement-cookies-and-sessions-into-a-code-like-this/ Share on other sites More sharing options...
jotorres1 Posted January 30, 2012 Share Posted January 30, 2012 Hi APD1993, I have a post that talks about sessions here: http://www.jotorres.com/en/2011/12/sessions-in-php/ Once you understand how to create sessions, you can download a sessions class I have created. I explain how to use it on this post: http://www.jotorres.com/myprojects/simple-sessions-class/ Hope it helps. Link to comment https://forums.phpfreaks.com/topic/256045-how-if-possible-could-i-implement-cookies-and-sessions-into-a-code-like-this/#findComment-1312583 Share on other sites More sharing options...
tobimichigan Posted January 30, 2012 Share Posted January 30, 2012 Its better to use <php session_start(); ?> to store all the variables for later use. Link to comment https://forums.phpfreaks.com/topic/256045-how-if-possible-could-i-implement-cookies-and-sessions-into-a-code-like-this/#findComment-1312585 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.