The Server Pages

»

TheServerPages Articles

»

Webmasters

»

PHP

Troubleshooting Trick (PHP)

Author: Wojjie     Posted: 2004-07-19     Viewed: 18,032

Introduction:

An aspect of programming that can be difficult to grasp is the ability to fix problems within your own code. The difficult part is not when you run into a syntax error, but when the program compiles, but does not work exactly how it should.

Tricks:

One common way of debugging problems is to have some sort of debugging output. In various different laguages, people insert print statements in parts of their program to see what a variable value is at a given point, or to see if the program manages to reach that given point.

In PHP there are two print functions that can prove to be useful, and they are:

echoGood for outputting a string or quick debug message
print_rQuick way to print a whole array or object

Example:

Here we will not use simple examples, instead the readers should try debugging the example with the article.



Example 1:
<html>
<body>
<?php
if ($_POST["submit"]=="submit") {
	$var1=str_replace(array("<",">"),"",$_POST["var1"]);
	echo $_POST["var1"];
}
?>
<form method="post">
<input type="text" name="var1">
<input type="image" name="submit" 
  src="http://www.theserverpages.com/files/ex-submit.gif"
  value="submit" border="0">
</body>
</html>
For those that are too lazy to try this on their own servers, here is a link

Now if you try the above link in FireFox, it works perfectly and ends up printing what is in the text box when you hit submit. If you tried the same thing in Internet Explorer, you will see that the page only refreshes when you hit submit. So what went wrong here?

If you know PHP well, you can probably skip alot of debugging work and almost get straight to the answer by running the code in your mind. I will pretend like I do not have a clue why this is happening, so the first thing I would do is check wether it is posting, and wether the if statement works.

The lines highlighted in blue are inserted into the code to provide some sort of debugging output.

<html>
<body>
<?php

print_r($_POST); // use of print_r

if ($_POST["submit"]=="submit") {

	echo "if statement is working...";  // use of echo

        $var1=str_replace(array("<",">"),"",$_POST["var1"]);
        echo $_POST["var1"];
}
?>
<form method="post">
<input type="text" name="var1">
<input type="image" name="submit"
  src="http://www.theserverpages.com/files/ex-submit.gif"
  value="submit" border="0">
</body>
</html>
Here is the link for this script: link

FireFox output:
 

Internet Explorer output:
 

As we see from the outputs, there is not $_POST["submit"] variable when using Internet Explorer. So we should check if $_POST["submit_x"] and $_POST["submit_y"] are set.

<html>
<body>
<?php
if (isset($_POST["submit_x"]) && isset($_POST["submit_y"])) {
        $var1=str_replace(array("<",">"),"",$_POST["var1"]);
        echo $_POST["var1"];
}
?>
<form method="post">
<input type="text" name="var1">
<input type="image" name="submit"
  src="http://www.theserverpages.com/files/ex-submit.gif"
  value="submit" border="0">
</body>
</html>
Here is the link for the final code: link


Comments

Copyright © 2004-2015: TheServerPages.com