tags:

views:

145

answers:

7

I've just discovered the email-address-saving form on my website does not work on Opera and Internet Explorer (7 at any rate), and possibly other browsers. Works fine with Firefox. Unfortunately I'm not a developer and no longer have any contact with the guy who wrote the code for the form so I've no idea how to fix it. I assume the problem has something to do with the code below:

<?php
    $str = '';

    if (isset($_POST['submit']))
    {
        if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $_POST['email'])) {
            $str = "<span style='color: red'>Not a valid email address</span>";
        } else {
            $file = 'emails.txt';
            $text = "$_POST[email]\n";

            if (is_writable($file)) {
                if (!$fh = fopen($file, 'a')) {
                    exit;
                }

                if (fwrite($fh, $text) === FALSE) {
                    exit;
                }

                fclose($fh);
            }
            header('Location: thankyou.html');
        }
    }
?>

and then the body bit:

<form action="index.php" method="post">
    <input type="text" name="email" style="width: 250px;" />
    <input type="image" src="img/button-submit.png" name="submit" value="Submit" style="position: relative; top: 5px; left: 10px" />
</form>

<?php echo $str ?>

Anybody feeling pity for a helpless non-dev and have an idea what's not working here?

A: 

Unfortunately, the error, if any at all, is going to be between the Browser and the server, not PHP. If you could provide some details like the HTML form that isn't working in IE7, then we may be able to help out more.

Jordan S. Jones
thanks, I added the html form
A: 

Your form element is self-closed. Remove the trailing / in the opening tag and it should work. (Er, it might work. Either way, there shouldn't be a trailing slash.)

You
I removed the trailing / (see updated html) but unfortunately the problem still remains :/
A: 

Assuming that the php in your code is in the same file as the form ... you might try adding the name of your php file to the form's action.

<form action="" method="post">

... becomes ...

<form action="name_of_php_file" method="post">
Sean Vieira
thanks; the code is indeed in the same file as the form (index.php) so I changed it to action="index.php". Unfortunately it still doesn't work :(
<form action="" method="post"> is perfectly valid and the best way to submit to the current page.
alexanderpas
A: 

I know this doesn't fix your problem, but I don't like the line:

$text = "$_POST[email]\n";

Is that not bad practice? I haven't used PHP for years, but I think you should change it to

$text = $_POST['email'] . "\n";

or something like that. Using $_POST[email] without the quotes around the array key causes PHP to first look for a constant named 'email'. Only after not finding it will it convert email to a string and then pull the value out of the associative array. Just wasted CPU power.

Cory Larson
While we're on that subject; it's better to test for `$_POST['email']` than `$_POST['submit']` as well.
You
Actually no, inside a string this is the correct way to refer to an entry in a one-dimensional array, it's a special case for the parser. If you're trying to access a two-dimensional array, you need to do "{$array['x']['y']}". Also, you can't substitute constants in a string like this to begin with.
deceze
Reference: http://php.net/manual/en/language.types.string.php#language.types.string.parsing
deceze
Well, like I said, it's been a while. Thanks for clearing that up.
Cory Larson
+2  A: 

This is being caused by the fact that the submit input is of type 'image'. On submit, IE7 only returns the x and y coords of the click.

This should do the trick:

Replace:

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

With:

if (isset($_POST['submit']) || isset($_POST['submit_x']))
Greg Annandale
Thank you for the suggestion but unfortunately this seemed to cause the index.php to not load at all on Firefox and didn't fix the problem on Opera. I'm on a Mac so IE is less convenient for me to check, but there doesn't seem much point with the Opera/Firefox problems.
That would be because I missed a bracked after the first ...['submit'])Give it another shot (code above has been corrected).
Greg Annandale
Yep it worked this time! I am extremely appreciative!
+1  A: 

It is a browser based issue

in your form, you have used <input type="image" />

IE doesn't pass name/value pairs for image type input, instead it only sends the key_x/value_x and key_y/value_y pairs

you probaly want to use <input type="submit" /> as replacement/addition, since this is completely supported on all types of browsers (think also about text browsers please, i still use them.)

alexanderpas
Or, if you have to use an image, use <button name="submit"><img src="..." /></button>
Jordan S. Jones
A: 

Include a hidden field in your form that will only be valid and present if you submit the form. Something like:

<input type="hidden" name="checkemail" value="1" />

Then, in your PHP, change the if-condition to check for this particular variable:

<?php
    $str = '';

    if (isset($_POST["checkemail"]))
    {
     //-- rest of your code
    }
?>

This will allow you to keep the image as the submit button and work across browsers which differ in how they send the value, if at all, of the name of image type buttons.

random