Jump to content


Photo

Global Variables not working as expected


  • Please log in to reply
3 replies to this topic

#1 raversnet

raversnet
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 August 2006 - 01:52 AM

Greetings all.  Im currently trying to learn PHP and have stumbled across this problem on numerous occasions already. 

I understand that when i submit a form in example1.php and with the ACTION arguement call another file such as example2.php.  That i cannot pass the values without register_globals being set to ON in the php.ini file.  Unless i use the built in $HTTP_POST_VARS array.

But if im not calling another file and simply wish to submit a form back to its own document using global $PHP_SELF(or just be default it should do this anyways).  Having register_globals set to OFF shouldnt matter.

The script im trying to get working is this:

<html>
<head>
</head>
<body>
<?php

$PARAMS=$HTTP_POST_VARS;    ///   I ADDED THIS JUST TO SEE IF THE VARIABLES DID IN FACT EXIST
foreach ($PARAMS as $key=>$value){
  	print "$key=$value <br>";
}


if (isset ($domain) && isset($sex) && isset ($mail)){
  // check user input here!
  $dberror="";
  $ret=add_to_database($domain, $sex, $mail, $dberror);
  if (!$ret)
  		print "Error: $dberror<BR>";
	else
		print "Thank you very much";
} else {
  write_form();
}

function add_to_database($domain, $sex, $mail, &$dberror){
  $user = "raversnet";
  $pass = "5434697";
  $db = "test";
  $link=mysql_pconnect("myth3",$user, $pass);
  
	if (!$link){
    	$dberror="Couldnt connect to MySQL server";
			return false;			
	}
	if (!mysql_select_db($db, $link)){
  		$dberror=mysql_error();
		  return false;		
	}
	$query="INSERT INTO domains (domain, sex, mail) values ('$domain','$sex','$mail')";
	
	if (!mysql_query($query, $link)){
	  $dberror=mysql_error();
	  return false;
	} 
	return true;
}

function write_form(){
  	global $PHP_SELF;
  	print "<form method=\"POST\">";
  	print "<input type=\"text\" name=\"domain\">";
  	print "The domain you would like<p>";
  	print "<input type=\"text\" name=\"mail\">";
  	print "Your mail address<p>";
  	print "<select name=\"sex\">";
  	print "\t<option value=\"M\"> Male";
  	print "\t<option value=\"F\"> Female";
  	print "</select>";
  	print "<input type=\"submit\" value=\"submit!\"></form>";
}


?>
</body>
</html>
 

The script displays the variables whether the register_globals are on or off.  Doesnt matter.  However the script Function ISSET doesnt work unless its set to ON. 

I can get the script working with register_globals OFF if i cycle through the array but really as far as i know....i shouldnt have to.

Any help is appreciated,

Thanks,

Rob

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 18 August 2006 - 09:07 AM

You should turn register_globals off. When this setting is off you'll have to use the $_POST superglobal array to access any POST'd data, for example to get the information from the domain field ion your form you use $_POST['domain'] rather than $domain. You change the string between [' and '] to the form field you want to access. If your form method is GET you use $_GET instead of $_POST. The same with cookies you use $_COOKIE or for sessions you use $_SESSION

YOu do not need register_globals to be on if you are submitting the form to another page. Also for the form to submit to itself
you use action=\"" . $_SERVER['PHP_SELF'] . "\"

$HTTP_*_VARS are old and have been replaced by the newer superglobals which were explained above.

#3 raversnet

raversnet
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 August 2006 - 10:06 PM

Ok i removed the Post Vars loop and replaced the ISSET line with:

if (isset ($_POST['domain']) && isset($_POST['sex']) && isset ($_POST['mail'])){

It works now which is fantastic.  Thing is i never had to replace all other references to $domain, etc.  They just work.  Why would ISSET be the only function affected.  If that makes any sense.

Thanks for the help btw.

R.

#4 raversnet

raversnet
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 August 2006 - 10:37 PM

Ok after i turned off the Register_Globals it quit working.  Shoulda did that in the first place.  After updating the line:

$ret=add_to_database($_POST['domain'], $_POST['sex'], $_POST['mail'], $dberror);

It worked just fine with Globals off. 

Thanks again,

R.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users