Page 1 of 2

potential problems with $PHP_SELF?

Posted: Mon Mar 07, 2005 6:46 am
by lscarmichael
i have a VERY simple form that takes an email address and adds it to a mysql database. the form works great in firefox but i've been having problems with it in IE. strangely enough, i have other forms that work fine in both browsers. the only difference is that the broken form is using $PHP_self as the action. could this cause a problem? could there be browser options that aren't allowing this to work correctly?

when an email is input the user should simply see "email added" below the input widow (and the window will clear). right now the window just clears but nothing is displayed.

let me know if you need to see code or have any other questions.

thanks

lee

Posted: Mon Mar 07, 2005 6:54 am
by John Cartwright
you most likely have register globals off.. it is now

$_SERVER['PHP_SELF'] I do believe..

Posted: Mon Mar 07, 2005 7:00 am
by lscarmichael
this is what i have
<form action="<?php echo $PHP_SELF;?>" method="post" name="signupForm">

so you're saying it should be
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="signupForm">

correct?

Posted: Mon Mar 07, 2005 7:59 am
by lscarmichael
well, i tried that and it doesn't seem to work. any other ideas?

Posted: Mon Mar 07, 2005 8:11 am
by onion2k
Whats it actually echoing out to the form? I usually use <?php echo basename($_SERVER['PHP_SELF']);?> .. helps with sub directories..

Posted: Mon Mar 07, 2005 8:16 am
by lscarmichael
i know you probably didn't want all this but this might clear up any other questions

thanks for your help with this.

lee



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>

<body>
<body bgcolor="#000000" background="../images/bg.jpg" vlink="#FF00FF">

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="signupForm">
<font face="Verdana" size="2"><input type="text" name="new_email" align="TOP" size="15">
<input type="submit" value="Submit" name="submit" align="MIDDLE" style="font-family: Verdana">
</form>




<?php
include 'functions.php';
//******************************************************
// Check for completed field.
//******************************************************
if (isset($submit)) {
if(!$new_email) {
die("<br>Enter an email address first!");
}
else{
$status = ValidateEmail($new_email);
switch($status){
case 0:
break;
case 1:
die("<br>Invalid address, try again.");
break;
case 2:
die("<br>Invalid address, try again.");
break;
}
connect_and_select_db();
$result = mysql_query("SELECT email FROM mailingList");
$num_rows = mysql_num_rows($result);
for($i=0; $i<$num_rows; $i++){
$email = mysql_result($result,$i,"email");
if($email == $new_email){
die("<br>You're already on the list.");
}
elseif($email != $new_email and $i+1==$num_rows){
mysql_query("INSERT INTO `mailingList` ( `primary ` , `first` , `last` , `email` ) VALUES ('', '', '', '$new_email');");
echo "<br>Address added successfully.";
echo "<br>", $num_rows, " total entries";
}

}
}

}


?>
</body>

</html>

Posted: Mon Mar 07, 2005 8:42 am
by patrikG
edit your post and use code tags around your code, please.

Posted: Mon Mar 07, 2005 8:50 am
by lscarmichael
sorry it wouldn't let me edit the previous post. here it is again.

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>

<body>
<body bgcolor="#000000" background="../images/bg.jpg" vlink="#FF00FF">

<form action="<?php echo $_SERVER&#1111;'PHP_SELF'];?>" method="post" name="signupForm">
        <font face="Verdana" size="2"><input type="text" name="new_email" align="TOP" size="15">
        <input type="submit" value="Submit" name="submit" align="MIDDLE" style="font-family: Verdana">
</form>




<?php
include 'functions.php';
//******************************************************
// Check for completed field.
//******************************************************
if (isset($submit)) &#123;
        if(!$new_email) &#123;
                die("<br>Enter an email address first!");
        &#125;
        else&#123;
                $status = ValidateEmail($new_email);
                switch($status)&#123;
                        case 0:
                                break;
                        case 1:
                                die("<br>Invalid address, try again.");
                                break;
                        case 2:
                                die("<br>Invalid address, try again.");
                                break;
                        &#125;
                connect_and_select_db();
                $result = mysql_query("SELECT email FROM mailingList");
                $num_rows = mysql_num_rows($result);
                for($i=0; $i<$num_rows; $i++)&#123;
                        $email = mysql_result($result,$i,"email");
                        if($email == $new_email)&#123;
                                die("<br>You're already on the list.");
                        &#125;
                        elseif($email != $new_email and $i+1==$num_rows)&#123;
                                mysql_query("INSERT INTO `mailingList` ( `primary ` , `first` , `last` , `email` ) VALUES ('', '', '', '$new_email');");
                                echo "<br>Address added successfully.";
                                echo "<br>", $num_rows, " total entries";
                        &#125;

                &#125;
        &#125;
        
&#125;


?>
</body>

</html>

Posted: Mon Mar 07, 2005 12:42 pm
by lscarmichael
any ideas?

Posted: Mon Mar 07, 2005 2:20 pm
by smpdawg
I never saw the answer to the question about register_globals. Is it on or off right now?

Posted: Mon Mar 07, 2005 2:32 pm
by lscarmichael
wait, i didn't get what he said about register_globals and the <? ...?> he had after it...should they be on or off? did that php line turn them on or off? i'm confused
you most likely have register globals off.. it is now

$_SERVER['PHP_SELF'] I do believe..

Posted: Mon Mar 07, 2005 2:46 pm
by smpdawg
You can check register_globals by running phpinfo() and looking for the entry that mentions register_globals.

If it is off, you will not be able to access post variables, etc. by name and must you the $_POST superglobal instead. BTW - This is the preferred behavior in PHP.

So instead of saying this

Code: Select all

if (isset($submit)) &#123;
You would say

Code: Select all

if (isset($_POST&#1111;'submit'])) &#123;
And this would also apply to the POST variable new_email.

Posted: Mon Mar 07, 2005 5:32 pm
by lscarmichael
nope, they're on...any other ideas? i'm just going to "start over" by adding pieces of code in one at a time to see where it messes up.

Posted: Mon Mar 07, 2005 5:35 pm
by John Cartwright
lscarmichael wrote:nope, they're on...any other ideas? i'm just going to "start over" by adding pieces of code in one at a time to see where it messes up.
standard debugging...



One thing I recommend is immediatly turning off Register Globals in your php.ini

Posted: Mon Mar 07, 2005 5:51 pm
by lscarmichael
ok, see what you think of this.

i put an echo after each line of code (starting inside the <?php). it looks like this:

Code: Select all

<?php
echo "<br>in php block";
include 'functions.php';
echo "<br>after include funtions";
//******************************************************
// Check for completed field.
//******************************************************
echo "<br>submit = ", $submit;
if (isset($_POST&#1111;'submit'])) &#123;
echo "<br>in isset";
        if(!$new_email) &#123;
        echo "<br>in if";
                die("<br>Enter an email address first!");
        &#125;
        else&#123;   echo "<br>in else";
and so on......

so, when i fill in my form and hit enter i get:
in php block
after include funtions
submit =
so when i hit the button something fishy is going on...it's like it's not taking the button action...

what could this mean?