Subject: PHP code - works in Firefox, fails in IE
Posted By: gingerbraid Post Date: 10/5/2005 1:02:32 PM
Hi,

I'm new to the whole web development idea; despite that, I've managed to learn PHP on the fly and write some code for a web application.  The application, however, works in Firefox and not in IE.

I've traced the execution and found out that it breaks at the following section:

foreach( $postvars["ID_field"] as $key => $value )
{
    foreach( $_SESSION["RType_Name_Prefix"] as $name => $prefix )
    {            
        if( strpos( strtoupper( $value ) , $prefix ) === 0 )
        {
            $rTypeID_tmp = $_SESSION["RType_Name_ID"][ $name ];
            $groupID_tmp = substr( $value, strlen( $prefix ), strlen( $value ));
            break;
        }        
    }
}

Apparently, the code is not computing strpos correctly.  For example, if prefix is 'V' and $value is 'V23', strpos returns 1.  Then, of course, the if statemnt also fails and produces incorrect result.

This happens only in IE.  Firefox works fine.

Any help (preferably at the earliest) is much appreciated.

Reply By: meow Reply Date: 10/5/2005 1:09:26 PM
Hi there.

What's the output of the script and the context? Browsers know nothing about PHP. They only see the markup it may produce.

--
http://yupapa.com
Reply By: gingerbraid Reply Date: 10/5/2005 1:50:57 PM
Hi, "meow"

Ok, here goes:

This script is executed on the server after the submission of the following form:

<form method=post action="<?php echo $_SERVER["PHP_SELF"] . "?View=7&Mod=" . $contID . ""; ?>
    <table>
        <tr>
            <td>ID</td>
        </tr>
        <tr>
            <td><input type="text" name="ID_field[]" value="&nbsp;"></td>
        </tr>
        <tr>
            <td><input type="submit" name="id_submit_btn" value="Submit"></td>
        </tr>
    </table>
</form>

The user enters an ID in the form e.g. V23.  The script is then called, comparing the input value against $_SESSION["RType_Name_ID"], which is an associative array of the form:

Array(Vector=>V, Insert=>I, Oligo=>O, etc)

The script is supposed to extract the letter prefix from the input value and match it against the values of the array.

For some reason, when I enter "v23" into the form and hit submit, strpos("V23", "V") returns 1 and not 0, making my entire program fail.

This happens only when the application is viewed in Internet Explorer on Windows XP.

Thanks! :)





Reply By: meow Reply Date: 10/5/2005 2:12:50 PM
Sadly enough I don't know PHP (yet;-)). But neither does IE so lets concentrate on the HTML.

Both your input tags lack the closing ">". But maybe you have it in the real page? "ID_field[]" isn't a kosher value for name. The spec says it should be CDATA but there are limitations.
http://www.w3.org/TR/html401/types.html#type-id

If the error is in the script someone else has to jump in. But the fact it only happens when you use IE points to that something's askew HTML wise.

--
http://yupapa.com
Reply By: richard.york Reply Date: 10/5/2005 2:28:40 PM
ID_field[]  works fine for the name attribute. This is array syntax, in PHP this will become a variable like $_POST['ID_field'][$i], where $i is offset by the number of ID_fields that occur.

Check the value of the POST array, you might be getting different values from Explorer.  Do this by dumping the contents of $_POST like so:

echo "<pre>";
var_dump($_POST);
echo "</pre>";

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply By: gingerbraid Reply Date: 10/5/2005 2:40:44 PM
Here is the dump result, Richard:

array(7) {
  ["ID_field"]=>
  array(1) {
    [0]=>
    string(4) " v23"
  }
... /other input values/ ...
  ["id_submit_button"]=>
  string(6) "SUBMIT"
}

It's the same in both browsers... why would "v23" have length 4??

Reply By: richard.york Reply Date: 10/5/2005 2:44:48 PM
Do you see the same output when you click view source and look at the dump?

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply By: richard.york Reply Date: 10/5/2005 2:46:18 PM
> why would "v23" have length 4?

It would if the browser evaluated "&nbsp;" to a single space, which is the default value of your input field.  I'd get rid of that.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply By: gingerbraid Reply Date: 10/5/2005 2:47:22 PM
Got it!  You're right.  Thanks so much, Rich!



Reply By: richard.york Reply Date: 10/5/2005 2:52:52 PM
BTW, eventhough the error ended up being the default value of "&nbsp;".  You can (and should) account for erraneous input problems like this by cleaning up your input data.  You are already setting the input string to uppercase, but you aren't trimming leading and trailing whitespace.  That's done with the trim() function.

http://www.php.net/trim



Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply By: meow Reply Date: 10/5/2005 2:53:31 PM
There you see. It was the HTML.

Richard, why does the error occur only with IE when the dump was the same for both browsers? Just trying to learn a bit here.

--
http://yupapa.com
Reply By: gingerbraid Reply Date: 10/5/2005 2:57:50 PM
You were right too, "meow".  Thank you both very much.

In regards to trim(), Rich - I AM using it: before going into the loop, I say:

$value = trim( $value );

I think it only trims trailing spaces, though.  Let me check the PHP manual again.

Reply By: richard.york Reply Date: 10/5/2005 3:13:41 PM
> why does the error occur only with IE

I'm not convinced that was the case.  Having that default value of &nbsp; introduced some potential for inconsistency, then using a function that relies on a particular schema to be present in a string, where even if the input string is correct (not-with-standing spaces), the mechanism can potentially fail.  I think that looking for the existence of the string in the other string would probably be a better approach.  Though, I don't know the specifics of this project so I can't say for sure.  If that is the case, that would be done with strstr() or stristr().  http://www.php.net/strstr and http://www.php.net/stristr.  The latter is case-insensitive.

> I AM using it: before going into the loop, I say $value = trim( $value );

If you're doing it *before* the loop, then you aren't trimming the value.  $value is set with each iteration of the foreach loop, so that statement must appear inside of the loop for it to work.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply By: meow Reply Date: 10/6/2005 6:33:45 PM
OK, then I understand better, I think. Thanks.

--
http://yupapa.com

Go to topic 35176

Return to index page 458
Return to index page 457
Return to index page 456
Return to index page 455
Return to index page 454
Return to index page 453
Return to index page 452
Return to index page 451
Return to index page 450
Return to index page 449