Jump to content

Basic form structure not working??


Edward

Recommended Posts

 

Hi,

 

I am using a form on my site. I define the form in a function, and then call the function as required. For some reason the form isn't displaying my variables. OK I admit I am defining them after the form, but I am defining them before I call the function. Like so...

function test() {

echo 'Name: '.$name;

}	

$name = 'John Smith';	

test();

 

Shouldn't this display 'Name: John Smith'? At present it only displays 'Name: '.

Link to comment
Share on other sites

 

Thank you. I've read through examples 1-7. While I'll read them again later, I do get the gist of what is happening, and why it therefore doesn't work. Rather than use 'global $name' or '$GLOBALS['name']', which you say are bad, can you please tell me what you would recommend and also explain why global variables are bad? Are you suggesting I use static variables?

Link to comment
Share on other sites

The (basic!) example below shows why I was trying to use a variable with global scope inside my function. If someone can suggest a better (rather than using the Global array) way to do what I'm trying to achieve, that would be great!

 

<?php

function form() {

echo '<input type="text" name="first_name" size="20" value="<?php echo $_POST['first_name']; ?>" />';
if ($errors = TRUE) { // Here is where I want to reference a variable of global scope
	echo 'There was an error with your name!';
}
echo '<input class="button_submit" type="submit" value="validate" name="submit" />';
}	

if (isset($_POST['submit'])) {

# Validate First Name
if (strlen($_POST['first_name']) < 1) {
	$errors = TRUE;
}								

if ($errors = TRUE) {

	echo '<p>There was an error!</p>';
	test();

} else {

	echo 'Thank you';

}

} else {

test();

}

?>

 

 

Link to comment
Share on other sites

Follow the link in my signature to Part I for PHP forms; it will give you a pretty good outline for building PHP forms.  I do use a global variable in the example to make it slightly more simple; I'm working on Part II where the global is eliminated and a few other things are cleaned up.

 

Globals are bad for a number of reasons, the most significant of which is they create unmaintainable code in large programs.  What will happen is you will have a large number of global variables and be unable to tell from where they are being set and / or changed.  You will create a global named $errors in one script and use it again in another, not meaning to overwrite the existing one.  Program bugs like this can be very difficult to debug.

 

The best solution to access data in a function is to pass it in as a parameter (or argument).  This means your functions can only use data given to them and can't inadvertently modify data in another part of the program.

 

The only global variables in PHP you can use safely are $_POST, $_GET, $_COOKIE (or $_COOKIES), $_SESSION, $_SERVER, and any other non-deprecated auto-globals I forgot to mention.

Link to comment
Share on other sites

Thank you very much! I'd be really interested to see part 2 of your form guide when it's available.

 

Just to be sure, are you suggesting I do something like the following in my function:

 

function form($first_name_validity, $last_name_validity, $email_address_validity, $errors) {

// Form
if (isset($errors)) {
	// display generic error message
}
if ($first_name_validity = FALSE) {
	// display name error message
}

}

 

This will mean I have a very long line of code when I pass the parameters to the function, but you're saying this is a better option to using global variables, right? If so, this is the way I'll proceed.

 

 

Link to comment
Share on other sites

Not quite and I'm not so tired right now so I can give you a bit more of an explanation.

 

ValidateForm() will go through and check all of your validation conditions.  Each time it finds one that fails you add an item to the $errors array.  Therefore, $errors is an array with keys: fname_invalid, lname_invalid, email_invalid, etc.  Each one of these indexes is the error message you wish to display.

 

This allows you in ShowForm() to do something simple like:

  echo '<p>We found the following errors:</p>'
       . '<ul><li>'
       . implode('</li><li>', $errors) // this line expands the $errors array
       . '</li></ul>';

 

Now if you want to eliminate the global variable, you can just have ValidateForm() return a local variable $errors and pass this into your ShowForm() equivalent as a single argument.

Link to comment
Share on other sites

  • 2 months later...

Thanks very much roopurt18. I got the form working but I've just come across a similar problem and I'm not sure how it can be fixed.

 

This time, I am defining a variable INSIDE the function, and I want to access it OUTSIDE the function. Is this possible? I've set up a script on my local server but the variable isn't being accessed. Here is my script so far:

 

function connect_to_mysql() {
if ($db_connect = @mysql_connect('localhost:8889', 'root', 'root')) {
	if (!@mysql_select_db('my_db')) {
		$errors .= '<li class="errors">The database could not be collected because: <b>' . mysql_error() . '</b>.</li>';
	}
} else {
	$errors .= '<li class="errors">The connection to MySQL could not be made because: <b>' . mysql_error() . '</b>.</li>';
}	
}

connect_to_mysql();					

if (!$errors) {			
echo 'ok';
} else {
echo 'error';
}

 

Do you know what I'm doing wrong? THank you very much for your help so far.

Link to comment
Share on other sites

Hi roopurt18, thank you for telling me that, I didn't know local variables were permanently local. I never seen what you demonstrated before, what exactly does that line of code mean? You seem to be assigning a function to a variable's value??

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.