Jump to content

form validation


jimfog

Recommended Posts

So...because I got confused a little...is the above code implementation correct?It seems to work ok so far:

<?php function check_page_title(){    global $page_title;    if (!isset($page_title))	 $page_title = 'agendopio';}check_page_title();?>

Do you have to notice anything wrong with the above?

Link to comment
Share on other sites

So...because I got confused a little...is the above code implementation correct?It seems to work ok so far:
<?php function check_page_title(){	global $page_title;	if (!isset($page_title))	 $page_title = 'agendopio';}check_page_title();?>

Do you have to notice anything wrong with the above?

The global keyword creates a copy of the variable, not a reference to the variable itself.
Link to comment
Share on other sites

And what does it matter here-in the case above?

Link to comment
Share on other sites

It means that $page_title is local to the function and can't be read from outside.After that function is executed, if $page_title was undefined it will continue to be undefined. I believe global &$page_title will fix that, though I don't think a function is necessary to check the variable in the first place.

Link to comment
Share on other sites

so, you are saying that code like the below is not good, from a logic point of view:

<?php function check_page_title(){	global $page_title;	if (!isset($page_title))	 $page_title = 'agendopio';}check_page_title();?><!DOCTYPE html>	<head>		<title><?php echo $page_title;?></title>		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />		 <link rel="stylesheet"  href="css/admingeneral.css"/>		<script  type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script>		<script type="text/javascript" src="js/js-code.js"></script>	</head>

And something else which I just noticed:In Netbeans, in the line where the title tags appear above I get a message which says the $page_title is not initialized.Despite the code works as expected(the title tag gets the name I want-agedopio) I wonderif there is something that requires adjustment

Edited by jimfog
Link to comment
Share on other sites

$page_title doesn't have a value. Like I told you, when the function stops running the variables in it are destroyed, including $page_title.If you take that code out of the function then it will work.

Link to comment
Share on other sites

To add to that, you'll get an undefined variable error if you try to pass an undefined variable to a function. Even though you might be checking if things are set in that function, trying to pass an undefined variable to the function is an error. Trying to use an undefined variable at all, other than to check if it is set, is an error. That's why I suggested the one line of code to just set the variable if it's not set.
See this code and tell me if what you described originally-thanks:
  $page_title = 'agendopio';	    require 'header.php';

Contents in header.php file:

<?php function check_page_title(){    global $page_title;    if (!isset($page_title))	 $page_title = 'agendopio';}check_page_title();?><!DOCTYPE html>    <head>	    <title><?php echo $page_title;?></title>	    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />		 <link rel="stylesheet"  href="css/admingeneral.css"/>	    <script  type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script>	    <script type="text/javascript" src="js/js-code.js"></script>    </head>

Link to comment
Share on other sites

Ok, apart from using a function...which is "too much" as you say...is the rest of the code ok you think?Is that what you meant from the beginning?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...