Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with tutorial

Featured Replies

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

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

  • 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. :unknw:

Anyway, thanks for your time.

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. :unsure:

or

<?php echo $_SERVER['PHP_SELF'] ; ?>

 

If the error persists then look to your server setup.

  • 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>

post-4094-1212577323_thumb.jpg

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.