Jump to content

blt4424

Members
  • Posts

    17
  • Joined

  • Last visited

    Never

Everything posted by blt4424

  1. Still working on this...I'm trying to rename my upload to the username of the logged in user. I'm trying to rename a file uploaded to the username of the logged in user. I can't seem to figure it out. I'm trying to mimic how I got the username and details on my profile pages. The pictures are uploaded to a folder, not a database. Here's my uploads.php <?php session_start(); include 'mysql-connect2.php'; # Setting auser as SESSION['user'] $auser = $_SESSION['user']; # SQL protecting variables $username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username; if(isset($auser)) { $sql = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'"); // directory path. // get the dir to send file to and the file name. $uploadfile = $username . basename($_FILES['userfile']['name']); // get the current file information. $file=$uploadfile; //get the . and file exstention. $ext = substr($file, -4); //convert varable to the uplaoded directory the new id //and extention. $uploadfile=$sql.$ext; //rename the file to the new one. @rename($file,$uploadfile); // if all the conditions are correct send the file to the directory. if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){ // success echoed echo " <font color='red'>File is valid, and was successfully uploaded.</font>"; }else { //unsuccesfull echoed echo "<font color='red'>File was unsuccesful sorry</font>"; } } // show the form. echo" <form enctype='multipart/form-data' action='uploads.php' method='POST'> <input type='hidden' name='MAX_FILE_SIZE' value='30000000000000000000000'> send this file <input name='userfile' type='file' > <input type='submit' name='submit' value='Send File'> </form>"; ?> And my profile page code for reference. <?php /*?><?php error_reporting(E_ALL ^ E_NOTICE); ?><?php */?> <?php # Starting the session session_start(); # Requiring SQL connection require_once 'mysql-connect2.php'; # Setting auser as SESSION['user'] $auser = $_SESSION['user']; # SQL protecting variables $username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username; # Checking through each query if(isset($auser)) { $sql = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'"); if(mysql_num_rows($sql)) { while($row = mysql_fetch_array($sql)) { $page = "<h1>User Info</h1>". "<b>Username: {$row['username']}<br /><br />". "<b>First Name: {$row['firstname']}<br /><br />". "<b>Last Name: {$row['lastname']}<br /><br />". "<b>Email: {$row['email']}<br /><br />". "<form name=\"backlistfrm\" method=\"post\" action=\"members2.php\">". " <input type=\"submit\" value=\"Back to The List\">". "</form><br />"; } } else { $page = "ERROR: No member found for username: <strong>{$_GET['username']}</strong>."; } } else { $page = "ERROR: Not logged in."; } # Printing the final output print $page; ?> Edit: sorry had wrong code. I'm getting resource id #5 on the file name save instead of the username?? I really need help with this!! I'm a noob to php. Thanks in advance. Need help getting this to work!
  2. I need some help working through this. I have an uploader for files, but I'd like it to create a link of the file uploaded and automatically post it to a page. Each user has a profile page and I'd like these pictures to be posted to their profile page. I guess with a session, it would take the logged in username and post the pictures uploaded on the page " member_profile2.php?username=xxxx " Here is my upload file for reference. uploads.php <?php $Dir = "temporary"; if (isset($_POST['upload'])) { //if file uploaded if (isset($_FILES['new_file'])) { //if successful if (move_uploaded_file($_FILES['new_file']['tmp_name'], $Dir . "/" . $_FILES['new_file']['name']) == TRUE){ chmod($Dir . "/" . $_FILES['new_file']['name'], 0644); echo "<b>File \"" . htmlentities($_FILES['new_file']['name']) ." was successfully uploaded </b><br />\n"; //displays file info echo "Name: ". htmlentities($_FILES['new_file']['name']) . "<br />". "Size: " . $_FILES['new_file']['size'] . " bytes" . "<br />". "Type: " . $_FILES['new_file']['type']; } //if not successful else { echo "<b>Uploading \"" . htmlentities($_FILES['new_file']['name']) . " was unsuccessful</b><br />\n"; } } } ?> <form action="uploads.php" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="250000" /><br /> Select file to upload:<br /> <input type="file" name="new_file" /> 250kB limit!<br /> <input type="submit" name="upload" value="Upload File" /><br /> </form> ?> and my member_profile2.php <?php /*?><?php error_reporting(E_ALL ^ E_NOTICE); ?><?php */?> <?php # Starting the session session_start(); # Requiring SQL connection require_once 'mysql-connect2.php'; # Setting auser as SESSION['user'] $auser = $_SESSION['user']; # SQL protecting variables $username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username; # Checking through each query if(isset($auser)) { $sql = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'"); if(mysql_num_rows($sql)) { while($row = mysql_fetch_array($sql)) { $page = "<h1>User Info</h1>". "<b>Username: {$row['username']}<br /><br />". "<b>First Name: {$row['firstname']}<br /><br />". "<b>Last Name: {$row['lastname']}<br /><br />". "<b>Email: {$row['email']}<br /><br />". "<form name=\"backlistfrm\" method=\"post\" action=\"members2.php\">". " <input type=\"submit\" value=\"Back to The List\">". "</form><br />"; } } else { $page = "ERROR: No member found for username: <strong>{$_GET['username']}</strong>."; } } else { $page = "ERROR: Not logged in."; } # Printing the final output print $page; ?> I'd like the pictures to be displayed after the user details. Any help with this would be great. I was also thinking, could I just have the uploader save it as a filename with the user's username? For example, "user1-image1.jpg". And then have their profile page pull and display any files with the name user1-image1-10.jpg (10 being max possible uploads for that user). How would I do this? This sounds maybe easier.
  3. I'm trying to set up a very simple (WIP) members page that you can click registered users to see their profile page that will display basic information. I'm having trouble with the sessions and retrieving this info from my database. I'm very new to this so it's all pretty amateur. I've been looking at this code for several hours trying to fix things but I start to make some progress, then change stuff, and go backwards. I had a members page that displayed the registered users in my database, but after a while of altering to try to get the profiles to work, I messed it up :mad:. I have basic login and register pages. I need some seperate eyes to take a look. Any help is so much appreciated. Thanks! members.php <?php session_start(); require 'mysql-connect.php'; $auser=$_SESSION['user']; if(isset($auser)){ $Members = mysql_query("SELECT * FROM user WHERE username='$username'") or die(mysql_error()); $numRowsMembers = mysql_num_rows($Members); ?> <table border="1"> <?php for($count = 1; $count <= $numRowsMembers; $count++) { $name = mysql_fetch_array($Members); ?> <tr> <?php echo '<td><a href="member_profile.php?username=' . $name['username'] . '">' . $name['username'] . '</a></td>'; } } ?> </tr> </table> member_profile.php <?php session_start(); require 'mysql-connect.php'; $auser=$_SESSION['user']; if(isset($auser)){ $username = $_GET['username']; $user = mysql_query("SELECT * FROM user WHERE username = '$username'"); echo $user; $user=mysql_fetch_assoc($user); echo "<h1>User Info</h1>"; echo "<b>Username:".$user['username']."<br>"; echo "<br>"; echo '<form name="backlistfrm" method="post" action="members.php">'; echo '<input type="submit" value="Back to The List">'; echo '</form>'; echo "<br>"; } ?> my login handler <?php include 'mysql-connect.php'; $username = $_POST['user']; $password = $_POST['pass']; $query1 = mysql_query("SELECT * FROM user WHERE username='$username'"); $result = mysql_num_rows($query1); if($result == 0) { echo '<h1>Error!</h1>The username you specified does not exist!'; } else { $checkuser = mysql_query("SELECT * FROM user WHERE username='$username'"); $row = mysql_fetch_array($checkuser); $password2 = $row['password']; //$status = $row['status']; if ($password == $password2) { echo "Hi $username."; include("index.php"); } else { echo '<h1>Error!</h1>The username and password combination you entered does not match the ones we have in the database.'; } } ?> mysql-connect.php <?php $host = "localhost"; $username = "root"; $password = ""; $database = "ug54"; $link = mysql_connect($host, $username, $password);//Connects to database with host, username, and password $select = mysql_select_db($database); ?> and my simple database CREATE TABLE IF NOT EXISTS `user` ( `id` int(4) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL, `password` varchar(32) NOT NULL, `firstname` varchar(20) NOT NULL, `lastname` varchar(20) NOT NULL, `email` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; -- -- Dumping data for table `user` -- INSERT INTO `user` (`id`, `username`, `password`, `firstname`, `lastname`, `email`) VALUES (1, '', '', '', '', '0'), (2, 'abc', '123', '', '', '0'), (3, 'a', 'b', 'c', 'd', '0'), (4, 'hfg', 'rgfdg', 'gdfg', 'dfgdf', '0'), (5, '999', '999', '999', '999', '999');
  4. I changed to POST, still same problem. Could it be where I close my forms?
  5. I've searched forever and can't find a solid answer. I'm trying to have a form which then when submitted, is redisplayed with another form under it. Then after submitting the second form, it will redisplay both forms. I almost have this, however I'm running into an unidentified index notice in my two fields from the first form. Here's the inital form.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Form 2</title> </head> <body> <?php ?> <form action = "formhandler2.php" method = "GET"> First name: <input type = "text" name = "fName" /> <br /> Last name: <input type = "text" name = "lName" /> <br /> <input type = "submit" value = "SUBMIT" /> </form> </body> </html> And my first formhandler.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Handler 2</title> </head> <body> <?php function redisplayForm1($inputString) { ?> <form action = "formhandler2.php" method = "GET"> First name: <input type = "text" name = "fName" value ="<?php echo $_GET['fName'];?>" /> <br /> Last name: <input type = "text" name = "lName" value ="<?php echo $_GET['lName'];?>" /> <br /> <input type="submit" name="submit" value = "SUBMIT" /> </form> <?php } echo "This is your completed Part One of the form <br />\n"; echo "<br />\n"; $inputString1 = ""; redisplayForm1($inputString1); //echo "First Name: ". $_REQUEST['fName']. "<br />"; //echo "Last Name: ".$_REQUEST['lName']. "<br />"; ?> <br /> Please complete Part Two of the form <form action = "formhandler3.php" method = "GET"> City: <input type = "text" name = "city" /> <br /> State: <input type = "text" name = "state" /> <br /> <input type = "submit" value = "SUBMIT" /> </form> </body> </html> And my second handler.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Handler 3</title> </head> <body> <?php function redisplayForm1($inputString1) { ?> <form action = "formhandler2.php" method = "GET"> First name: <input type = "text" name = "fName" value ="<?php echo $_GET['fName'];?>" /> <br /> Last name: <input type = "text" name = "lName" value ="<?php echo $_GET['lName'];?>" /> <br /> </form> <br /> Please complete Part Two of the form <br /> <?php } function redisplayForm2($inputString2) { ?> <form action = "formhandler3.php" method = "GET"> City: <input type = "text" name = "city" value ="<?php echo $_GET['city'];?>" /> <br /> State: <input type = "text" name = "state" value ="<?php echo $_GET['state'];?>" /> <br /> </form> <?php } echo "This is your completed Part One of the form <br />\n"; echo "<hr />"; echo "<br />\n"; $inputString1 = ""; redisplayForm1($inputString1); $inputString2 = ""; redisplayForm2($inputString2); echo "<hr />"; ?> </body> </html> I realize this probably can be done in an easier way, but I'd like to just get a working solution. I appreciate any help!
  6. I change post to get, thinking that might be the problem, but no. <?php function redisplayForm($label='', $age='') { if($label != '') { echo '<p>Your '.$label.' is required!'.'</p>'; } ?> <form action = "formhandler.php" method = "get"> First name: <input type = "text" name = "fName" value ="<?php echo $_GET['fName'];?>" /> <br /> Last name: <input type = "text" name = "lName" value ="<?php echo $_GET['lName'];?>" /> <br /> Age: <input type = "text" name = "age" value ="<?php echo $_GET['age'];?>"/> <br /> <input type="submit" name="submit" value = "SUBMIT" /> </form> <?php } function displayRequired($fieldName) { echo "The field \"$fieldName\" is required.<br />n"; } function validateInput($inputString,$label) { if (empty($inputString)) { echo "Please re-enter the information below.<br />\n"; $errorCount++; redisplayForm($label); exit(); } else { if(empty($inputString)) { $errorCount++; $returnval = ""; displayRequired($fieldName); } else { $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0) { return true; } else { return false; } } function validateAge($age,$errorCount) { if ($errorCount > 0) { echo "Please re-enter the information below.<br />\n"; redisplayForm($age); exit(); } else { if(empty($age)) { echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($age); exit(); } else { $age = intval(trim($age)); if(is_numeric($age)) { $age = round($age); if(($age >= 1) && ($age <= 130)) { $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; redisplayForm($age); $errorCount++; $agereturnval = ""; } } else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; redisplayForm($age); $errorCount++; $agereturnval = ""; } } return($agereturnval); } return $age; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if(isset($_GET['submit'])) { $errorCount= 0; $fName = validateInput($_GET['fName'],'First Name'); $lName = validateInput($_GET['lName'],'Last Name'); $age = validateAge($_GET['age'],'Age'); if($errorCount == 0) { echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; } } else { redisplayForm(); } ?> </body> </html>
  7. OK, one last question. I have a php program that has the form, the form then goes to the form handler for validation,printing. However, I have to hit the submit button twice for it to do anything.
  8. Perhaps you're right, but this is how I've learned <?phpfunction redisplayForm($label='',$age=''){ if($label != '') { echo '<p>Your '.$label.' is required!'.'</p>'; } ?><form action = "formhandler.php" method = "post">First name: <input type = "text" name = "fName" /> <br />Last name: <input type = "text" name = "lName" /> <br />Age: <input type = "text" name = "age" /> <br /><input type="submit" name="submit" value = "SUBMIT" /></form><?php}function displayRequired($fieldName){ echo "The field \"$fieldName\" is required.<br />n";}function validateInput($inputString,$label){ if (empty($inputString)) { echo "Please re-enter the information below.<br />\n"; redisplayForm($label, $age); exit(); } else { if(empty($inputString)) { $errorCount++; $returnval = ""; displayRequired($fieldName); } else { $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0) { return true; } else { return false; }}function validateAge($age,$errorCount){ if ($errorCount > 0) { echo "Please re-enter the information below.<br />\n"; redisplayForm($label, $age); exit(); } else { if(empty($age)) { echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($inputString, $age); exit(); } else { $age = intval(trim($age)); if(is_numeric($age)) { $age = round($age); if(($age >= 1) && ($age <= 130)) { $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } return($agereturnval); } return $age;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><?phpif(isset($_POST['submit'])){ $errorCount= 0; $fName = validateInput($_POST['fName'],'First Name'); $lName = validateInput($_POST['lName'],'Last Name'); $age = validateAge($_POST['age'],$errorCount); if($errorCount == 0) { echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; }}else{ redisplayForm();}?></body></html> Works but get Notice: Undefined variable: age in C:\wamp\www\formhandler.php on line 26 Which is...(in bold) function validateInput($inputString,$label) { if (empty($inputString)) { echo "Please re-enter the information below.<br />\n"; redisplayForm($label, $age); exit();
  9. I've made alterations, however I'm now just getting a blank page... <?phpfunction redisplayForm($label='',$age=''){ if($label != '') { echo '<p>Your '.$label.'</p>'; } ?> <form action = "formhandler.php" method = "post">First name: <input type = "text" name = "fName" /> <br />Last name: <input type = "text" name = "lName" /> <br />Age: <input type = "text" name = "age" /> <br /><input type = "submit" value = "SUBMIT" /></form> <?php}function displayRequired($fieldName){echo "The field \"$fieldName\" is required.<br />n";}function validateInput($inputString, $label){if (empty($inputString)) { echo "Please re-enter the information below.<br />\n";redisplayForm($label, $age); exit(); } else { if(empty($inputString)) { $errorCount++;$returnval = ""; displayRequired($fieldName); }else {$returnval = trim($inputString);$returnval = stripslashes($returnval);}return($returnval);}if($errorCount==0) { return true; } else { return false; }}function validateAge($age,$errorCount) {if ($errorCount>0) {echo "Please re-enter the information below.<br />\n";redisplayForm($inputString, $age); exit();}else {if(empty($age)) { echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($inputString, $age); exit(); } else { $age = intval(trim($age)); if(is_numeric($age)) { $age = round($age); if(($age >= 1) && ($age <= 130)) { $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; ++$errorCount; $agereturnval = ""; } } else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; ++$errorCount; $agereturnval = ""; }}return($agereturnval);} return $age;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form Handler</title></head><body><?phpif(isset($_POST['submit'])) { $errorCount=0; $fName = validateInput($_POST['fName'], "First name"); $lName = validateInput($_POST['lName'], "Last name"); $age = validateAge($_POST['age'], "Age"); if($errorCount ==0) {echo "Hi ".$fName." ".$lName."."."<br />";echo "You are ".$age." years old. <br />";echo $_SERVER['SCRIPT_FILENAME'];}else{ redisplayForm();} }?></body></html>
  10. Stupid me, I didn't have a redisplayForm function with the form code. However, it isn't redisplaying the input. <?php $fName = validateInput($_POST['fName']); $lName = validateInput($_POST['lName']); $age = validateAge($_POST['age']); function redisplayForm($inputString, $age){ ?> <form action = "formhandler.php" method = "post"> First name: <input type = "text" name = "fName" /> <br /> Last name: <input type = "text" name = "lName" /> <br /> Age: <input type = "text" name = "age" /> <br /> <input type = "submit" value = "SUBMIT" /> </form> <?php } function displayRequired($fieldName){ echo "The field \"$fieldName\" is required.<br />n"; } function validateInput($inputString){ if (empty($inputString)){ echo "Please re-enter the information below.<br />\n"; redisplayForm($inputString, $age); } else{ if(empty($inputString)){ displayRequired($fieldName); $errorCount++; $returnval = ""; } else{ $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0){ return true; }else { return false; } } $erorrCount= 0; function validateAge($age){ global $errorCount; if ($errorCount>0){ echo "Please re-enter the information below.<br />\n"; redisplayForm($inputString, $age); } else{ if(empty($age)){ echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($inputString, $age); } else{ $age = trim($age); $agereturnval = ""; $age = stripslashes($agereturnval); if(is_numeric($age)){ $age = round($age); if(($age >= 1) && ($age <= 130)){ $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } }else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } return($agereturnval); } } $errorcount = 0; echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; ?>
  11. Ok I tried combining the $fName and $lName, however, I'm having trouble with those if(!validateInput ()) statements. I'm getting a parse error. Commenting those out and trying to run my code, it somewhat works, displaying the input except for the age input. However it will give me the erorr saying it wasn't between 1 and 130 when the input was say "20". Also, if I don't enter say a first name, it gives me the error to re-enter the info, but doesn't redisplay the form with the input. Here's my code at the moment. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Handler</title> </head> <body> <?php $fName = validateInput($_POST['fName']); $lName = validateInput($_POST['lName']); $age = validateAge($_POST['age']); function displayRequired($fieldName){ echo "The field \"$fieldName\" is required.<br />n"; } if (!validateInput($_POST['fName']) { echo "First name is required.<br />n"; } if (!validateInput($_POST['lName']) { echo "Last name is required.<br />n"; } function validateInput($inputString){ if (empty($inputString)){ echo "Please re-enter the information below.<br />\n"; redisplayForm($inputString); } else{ if(empty($inputString)){ displayRequired($fieldName); $errorCount++; $returnval = ""; } else{ $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0){ return true; }else { return false; } } $erorrCount= 0; function validateAge($age){ global $errorCount; if ($errorCount>0){ echo "Please re-enter the information below.<br />\n"; redisplayForm($$age); } else{ if(empty($age)){ echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($age); } else{ $age = trim($age); $agereturnval = ""; $age = stripslashes($agereturnval); if(is_numeric($age)){ $age = round($age); if(($age >= 1) && ($age <= 130)){ $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } }else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } return($agereturnval); } } $errorcount = 0; echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; ?> </body> </html>
  12. Thanks I counted like 6 times and couldn't keep track... Now it runs but it doesn't echo anything I input. This validation is kicking my butt. Too many if statements together... Is it ok how I have errorCount for both inputValidation and ageValidation? I was unsure how I could do validation function for both all in one. Might've made this simpler... However, he's my output as of now... eek! I really appreciate your help guys. I'm a newbie to this!
  13. Whoops that's not supposed to say errorCountInput Thanks Pikachu! Now... I'm getting an issue with the last line, </html>...? I think that means I have a missing curly brace, right?
  14. Ok didn't know that. I'm having trouble on line 28 in validateInput, something with my if statement? function validateInput($fName, $lName){if ($errorCountInput>0){echo "Please re-enter the information below.<br />\n";redisplayForm($firstName, $lastName);}else{[b]if(empty($fName, $lName)){[/b] displayRequired($fieldName); $errorCount++; $returnval = "";}else{ $returnval = trim($fName,$lName); $returnval = stripslashes($returnval);}return($returnval);}}$erorrCount= 0;
  15. I'm trying to do a basic form with php which asks for first, last name, and age with validation. I'm having trouble with my validation and the error counter. I want it to redisplay the form with the errors and entered input. My formhandler.php is here: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form Handler</title></head><body><?php$fName = validateInput($_POST['fName']);$lName = validateInput($_POST['lName']);$age = validateAge($_POST['age']); function displayRequired($fieldName){echo "The field \"$fieldName\" is required.<br />n";}function validateInput($fName, $lName){if ($errorCountInput>0){echo "Please re-enter the information below.<br />\n";redisplayForm($firstName, $lastName);}else{if(empty($fName, $lName)){ displayRequired($fieldName); ++$errorCount; $returnval = "";}else{ $returnval = trim($fName,$lName); $returnval = stripslashes($returnval);}return($returnval);}}$erorrCount= 0;function validateAge($age){global $errorCount;if ($errorCount>0){echo "Please re-enter the information below.<br />\n";redisplayForm($age); }else{if(empty($age)){ echo "<p>The age field is required!</p>\n"; ++$errorCount; $returnval = ""; redisplayForm($fName, $lName, $age); }else{ $age = trim($age); $age = stripslashes($age); if(is_numeric($age)){ $age = round($age); if(($age >= 1) && ($age <= 130)){ $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; ++$errorCount; $agereturnval = ""; } }else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; ++$errorCount; $returnval = ""; }}return($returnval); }$errorcount = 0;echo "Hi ".$fName." ".$lName."."."<br />";echo "You are ".$age." years old. <br />";echo $_SERVER['SCRIPT_FILENAME'];?></body></html> Any help would be much appreciated. I've looked at this for hours and I just need another set of eyes to pick some things out. Once you get too close to code it gets hard to figure it out.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.