June 3, 200818 yr Hi all I'm trying to get my teeth into some php, and so I've bought a book (or two) for that purpose, the default O'Reilly reference manual, and also "how to do everything with PHP & MYSQL". I've copied the code from the book: <html> <head> <title>PHP Test Script 7</title> </head> <body> <?php //if the "submit" variable does not exist //form has not been submitted //therefore display the initial page if(!$_POST['submit']) { ?> <form action="<?=$_SERVER['PHP_SELF']?>" method ="post"> Enter a number: <input name="number" size="2"> <input type="submit" name="submit" value="go"> </form> <?php } else { //if the submit variable exists //the form has been submitted //look for and process form data //display the result $number = $_POST['number']; if ($number > 0) { echo 'You entered a positive number'; } elseif ($number<0) { echo 'You entered a positive number'; } else { echo 'You entered 0'; } } ?> </body> </html> and I'm using wampserver. I've got two problems. 1: the page renders the form, but then also adds the following lines of code as text. Suggesting to me that the code has ended the php prematurely somehow. <0) { echo 'You entered a positive number'; } elseif ($number < 0) { echo 'You entered a positive number'; } else { echo 'You entered 0'; } } ?> problem 2: on entering a number into the form area, I get this error message: Forbidden: You don't have permission to access /< on this server. Any ideas what's happening? I've checked again and again for a typo - but to no avail (which is not to say there isn't one). I don't know if the code has a typo error - though I suspect from the error that somehow the code is escaping php interpretation somehow. I know this is probably a simple problem, but this has got me stumped, and it's a fairly early tutorial, so I'd rather resolve this before I move on. any help would be much appreciated. cheers SWK
June 3, 200818 yr Not sure what to suggest. I have just tested your file locally on my pc and it works. It doesnt bring up any errors. Here is a copy of what I tested. <html> <head> <title>PHP Test Script 7</title> </head> <body> <?php //if the "submit" variable does not exist //form has not been submitted //therefore display the initial page if(!$_POST['submit']) { ?> <form action="<?=$_SERVER['PHP_SELF']?>" method ="post"> Enter a number: <input name="number" size="2"> <input type="submit" name="submit" value="go"> </form> <?php } else { //if the submit variable exists //the form has been submitted //look for and process form data //display the result $number = $_POST['number']; if ($number > 0) { echo 'You entered a positive number'; } elseif ($number<0) { echo 'You entered a positive number'; } else { echo 'You entered 0'; } } ?> </body> </html> Try copying and pasting this and try it. I know it is identical but I have known strange characters to creep in if you are using notepad. Pat
June 3, 200818 yr Author Thanks for the timely reply - unfortunately the error remains. Perhaps there is a problem with the wampserver? It's managed to execute all the other code samples I've entered from the book, it appears to be just this particular snippet. Anyway, thanks for your time.
June 3, 200818 yr On the line: <form action="<?=$_SERVER['PHP_SELF']?>" method ="post"> you're using short tags. Depending on your set up, this might cause problems. Try this instead: <form action="<?php =$_SERVER['PHP_SELF']?>" method ="post"> Just a thought.
June 4, 200818 yr or <?php echo $_SERVER['PHP_SELF'] ; ?> If the error persists then look to your server setup.
June 4, 200818 yr Author Tried that again, this time it seems to have worked - I guess it was the short-tags thing. Thanks for your help <resume: sanity>
Create an account or sign in to comment