hosker Posted September 20, 2012 Share Posted September 20, 2012 I have a form I created that writs to a text file. I have a variable issue. Basically, I need to know where to put this variable so that when the page loads it is empty and when the forms is filled out and submitted the variable is filled with a text string. Below is my code <?php //include files include_once('classes/Validation.php'); //initiate object of the validation class $Validation = new Validation(); //array that will store the error messages $errorArray = array(); if (isset($_POST['submit'])){ validate(); } global $added; $added = " "; function validate() { //anything created outside the function must be global to be used inside a function. //or passed via the parameter. Also any variables created inside the function that are //set as global can be used outside the function. global $errorArray; global $Validation; //This checks the entry and if there is an error puts the message into the errorArray $errorArray[0] = $Validation->checkForBlanks($_POST['fname']); $errorArray[1] = $Validation->checkForBlanks($_POST['lname']); $errorArray[2] = $Validation->checkForBlanks($_POST['pnumber']); $errorArray[3] = $Validation->checkForBlanks($_POST['email']); /* * This does a final check for errors if none are found it will write the form values to a text file, clear the $_POST variables and * fill in a variable to alert the user the form was submitted correctly. */ if (!$Validation->checkErrors()){ $file = fopen("content.txt","a") or die("Cannot Open File"); $formdata = $_POST['fname'] . ',' . $_POST['lname'] . ',' . $_POST['pnumber'] . ',' . $_POST['email'] . "\n"; fwrite($file,$formdata); fclose($file); $_POST['fname'] = ""; $_POST['lname'] = ""; $_POST['pnumber'] = ""; $_POST['email'] = ""; return $added = "<p>Contact has been added<p>"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link rel="stylesheet" type="text/css" href="css/project1.css" /> </head> <body> <div id="project1-wrapper"> <div id="project1-header"> <h2>INP 271<br /> PHP Web Site</h2> </div> <div id="project1-menu"> <ul> <li><a href="index.php" title="Home">Home</a></li> <li><a href="add-contact.php" title="Add Contact">Add Contact</a></li> <li><a href="view-contacts.php" title="View Contacts">View Contacts</a></li> </ul> </div> <div id="project1-form"> <h2>Add Contact</h2> <?php echo $added; ?> <form method="post" action="" > <!-- When this form is submitted, but now all fields hold a value, the form will display error messages next to the label that is not filled in. Those fields that were filled in will still hold the value the user entered. --> <p><label for="fname">First name:</label><?php if(isset($errorArray[0])){echo "<span class='error'>{$errorArray[0]}</span>";} ?><br /> <input type="text" name="fname" id="fname" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];} ?>" /></p> <p><label for="lname">Last name:</label><?php if(isset($errorArray[1])){echo "<span class='error'>{$errorArray[1]}</span>";} ?><br /> <input type="text" name="lname" id="lname" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];} ?>" /></p> <p><label for="pnumber">Phone Number:</label><?php if(isset($errorArray[2])){echo "<span class='error'>{$errorArray[2]}</span>";} ?><br /> <input type="text" name="pnumber" id="pnumber" value="<?php if(isset($_POST['pnumber'])){echo $_POST['pnumber'];} ?>" /></p> <p><label for="email">Email:</label><?php if(isset($errorArray[3])){echo "<span class='error'>{$errorArray[3]}</span>";} ?><br /> <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>" /></p> <p><input type="submit" value="submit" name="submit" id="submit" /></p> </form> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted September 20, 2012 Share Posted September 20, 2012 Don't use 'global'. Ever. Functions have an argument list for a reason. Rewrite your function(s) to accept arguments, and in the code that invokes the function(s), pass in the proper parameters. 'global' is the single worst part of PHP, mostly because every crap book and tutorial still floating around since 2004 uses it exclusively, and thus there is a ton of bad code that relies on it as well. If you have a resource that uses 'global' to pass in parameters, burn/destroy it for the good of us all. Quote Link to comment Share on other sites More sharing options...
hosker Posted September 20, 2012 Author Share Posted September 20, 2012 Okay, so global is a bad way to go. ANy thoughts on how I can make it work using the global variable format for now? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 20, 2012 Share Posted September 20, 2012 Don't use 'global'. Ever. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted September 20, 2012 Share Posted September 20, 2012 Yeah, I'm not going to show you how to continue to use 'global'. Instead, simply change your function definition's first line to: function validate($errorArray, $Validation) And remove from within there: global $errorArray; global $Validation; --- Okay, now, at the beginning of your script, remove: global $added; $added = " "; and replace it with: $added = validate($errorArray, $Validation); --- Finally, in your HTML/display part, replace: <?php echo $added; ?> With: <?php if(isset($added)) echo $added; ?> That should (hopefully) cover it. Keep in mind, that none of this is tested, so it might not work right off the bat. Kinda written off the cuff.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.