Jump to content

DirkDiggler

Members
  • Posts

    8
  • Joined

  • Last visited

Posts posted by DirkDiggler

  1. I am installing a simple Hit Counter and I get the following error

     

    "Warning: file_put_contents(hitcount.txt) [function.file-put-contents]: failed to open stream: Permission denied in..."

     

    I realize this error is because I do not have the correct permissions set, and I cannot seem to get the chmod() function right to accomplish this.

     

    The current code below gets both the above error and this error:

     

    "Warning: chmod() [function.chmod]: Operation not permitted in..."

     

    HitCounter.php is the page that is running and getting an error. This file is in the "../Chapter.05/Projects" directory of the server. How can I give "/Projects" read and write permissions for everyone??

     

    Thanks!

    <!DOCTYPE html><html><head><title>Hit Counter</title></head><body><?phpchmod("/home/ca86_22/public_html/Chapter.05/Projects",0666);$CounterFile = "hitcount.txt";if (file_exists($CounterFile)) {	$Hits = file_get_contents($CounterFile);	++$Hits;	// if $CounterFile exists, retrieves $Hits value				// and increments by 1} else $Hits = 1;echo "<h1>There have been $Hits hits to this page.</h1>n"; //displays # of hitsif (file_put_contents($CounterFile, $Hits)) //updates value in hitcount.txt file//file_put_contents opens the file if it already exists =OR= creates the file if it doesn't	echo "<p>The counter file has been updated.</p>n";?></body></html>
  2. How can I replace the validateInput functions' if structure with a switch() or case() function? I am trying to make it so that the if statement at line 19 does not echo a result if a field is blank.

    Right now, if Hourly Wage field is blank, the statement "This is your gross salary" echoes as long as a number is entered in "Number of Hours Worked"

  3. Here is the code where I am attempting to use is_numeric.

     

    Thanks for your quick reply.

    <!DOCTYPE html><html><head><title>Paycheck Calculation</title></head><body><?php $hours = validateInput((int) $_POST['Hours'], "Number of Hours Worked"); $wage = validateInput((int) $_POST['Wage'], "Hourly Wage"); $overtime = max($hours - 40, 0);$pay += $overtime * $wage * 1.5;$pay += ($hours - $overtime) * $wage;if($hours > 40) { //Calculate time and a half	echo "<b>This is your gross salary PLUS time and a half: </b>" . $pay; } else if($hours > 0 && $hours < 40) { //Calculate straight hourly	echo "<b>This is your gross salary: </b>" . $pay; } if($errorCount > 0){	echo "<br />n <b>Please use the "Back" button to re-enter the data.</b><br />n";}function displayRequired($fieldName) {	echo "The field "<i>$fieldName</i>" is required.<br />n";}function displayNumeric($fieldName) {	echo "The field "<i>$fieldName</i>" must contain a numeric value.<br />n";}function validateInput($data, $fieldName) {	global $errorCount;	if(empty($data)) {		displayRequired($fieldName);		++$errorCount;		$retval = "";	} else if(!is_numeric($data)) {		displayNumeric($fieldName);		++$errorCount;		$retval = "";	} else {		$retval = trim($data);		$retval = stripslashes($retval);	}	return($retval);}$errorCount = 0;?> </body></html>
  4. Hi,

    I have a simple form with two fields: Number of Hours Worked, and Hourly Wage. The form does what I want it to do, but I am struggling with validating user input. I can validate if the fields are empty, and return error message if that is the case, but I also want to be able to ensure they enter numbers only and return a different error message if they enter any alpha characters.

     

    I tried using is_numeric() to accomplish this but I think I failed somewhere in the nesting of my if...else code.

     

    I have attached the working .php that only validates for input, please help with adding validation for numbers only!

    Thanks!

    <!DOCTYPE html><html><head><title>Paycheck Calculation</title></head><body><?php $hours = validateInput((int) $_POST['Hours'], "Number of Hours Worked"); $wage = validateInput((int) $_POST['Wage'], "Hourly Wage"); $overtime = max($hours - 40, 0);$pay += $overtime * $wage * 1.5;$pay += ($hours - $overtime) * $wage;if($hours > 40) { //Calculate time and a half	echo "<b>This is your gross salary PLUS time and a half: </b>" . $pay; } else if($hours > 0 && $hours < 40) { //Calculate straight hourly	echo "<b>This is your gross salary: </b>" . $pay; } if($errorCount > 0)	echo "<br />n <b>Please use the "Back" button to re-enter the data.</b><br />n";function displayRequired($fieldName) {	echo "The field "<i>$fieldName</i>" is required.<br />n";}function validateInput($data, $fieldName) {	global $errorCount;	if(empty($data)) {		displayRequired($fieldName);		++$errorCount;		$retval = "";	} else {		$retval = trim($data);		$retval = stripslashes($retval);	}	return($retval);}$errorCount = 0;?> </body></html>
×
×
  • Create New...