Jump to content

[SOLVED] Simple Code Not Working


objN00B

Recommended Posts

New to PHP. My simple code is not working. Any Ideas?

Also, I seen in another thread mention a built-in function that prints errors.

My PHP install use to print errors by default, 2 days ago, now it just serves

blank pages on errors. However, I have changed no settings. Strange.

 

<?php

if (!isset($_POST['phase'])) { //phase has not been set
//DISPLAY FORM
echo "<form method='post' action='form.php'>
		Your Name:
	  	<input type='text' name='fFirst_Name'><br />
		<input type='hidden' name='phase' value='validate'>
		<input type='submit' value='Submit Form'>
	  </form>";

} elseif ($_POST['phase'] == "validate") {
$first_name = $_POST['fFirst_Name'];
//define error codes
define(eFirst_Name1, "<font color='red'>You must enter your name please!</font>");
define(eFirst_Name2, "<font color='red'>Your name must be 3 or more characters long</font>");
//validate data

	if (!isset($first_name)){
		echo "<form method='post' action='form.php'>
		Your Name:
	  	<input type='text' name='fFirst_Name'> $eFirst_Name1 <br />
		<input type='hidden' name='phase' value='validate'>
		<input type='submit' value='Submit Form'>
	    </form>";
	} elseif (strlen($first_name) =< 2) {
	    echo "<form method='post' action='form.php'>
		Your Name:
	  	<input type='text' name='fFirst_Name'> $eFirst_Name2 <br />
		<input type='hidden' name='phase' value='validate'>
		<input type='submit' value='Submit Form'>
	    </form>";

	} else {
		echo 'Hello, ' . $first_name . '!';

	}

}
?>

 

Thanks for any input!

 

EDIT: Found missing ; at set variable $first_name. Still not working.

Link to comment
Share on other sites

The ERRORs in the above code were incorrectly using the define function.

I was not closing the constant name with double quotes.

 

Also after getting it to work. I noticed $_POST will return a NULL value

for any empty text fields. So I changed the validation code

if (!isset($first_name)

to

if ($first_name == NULL)

This allows the proper corresponding errors to be displayed.

 

New Code:

<?php
define("eFirst_Name1", "<font color='red'>You must enter your name please!</font>");
define("eFirst_Name2", "<font color='red'>Your name must be 3 or more characters long</font>");

if (! isset($_POST['phase'])) { //phase has not been set
//DISPLAY FORM
echo ('<form method="post" action="form.php">
		Your Name:
	  	<input type="text" name="fFirst_Name"><br />
		<input type="hidden" name="phase" value="validate">
		<input type="submit" value="Submit Form">
	  </form>');

} elseif ($_POST['phase'] == 'validate') {
$first_name = $_POST['fFirst_Name'];
//define error codes

//validate data

	if ($first_name == NULL) {
			echo ('<form method="post" action="form.php">
		Your Name:
	  	<input type="text" name="fFirst_Name">'.eFirst_Name1.'<br />
		<input type="hidden" name="phase" value="validate">
		<input type="submit" value="Submit Form">
	  </form>');
	} elseif (strlen($first_name) <= 2) {
	   	echo ('<form method="post" action="form.php">
		Your Name:
	  	<input type="text" name="fFirst_Name">'.eFirst_Name2.'<br />
		<input type="hidden" name="phase" value="validate">
		<input type="submit" value="Submit Form">
	  </form>');

	} else {
		echo 'Hello, ' . $first_name . '!';

	}

}
?>

Link to comment
Share on other sites

Thanks for the advice. As far as I know, GET method appends the values to the URL

while POST just passes the values using the HTTP protocol.

 

Someone can POST method data from any form to my .php script with improper

or malicious code.

 

My question is how can I validate the input came from my server or script? Is there

a global that holds any identifying information about where the data originated or

who passed the data?

 

This type of validation would not only generically validate the data using variable

checks code but also ensure the data ONLY came from a trusted source.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.