Jump to content

PHP display page


vj5
 Share

Recommended Posts

I am relatively new to php and learning php from the tutorials. How do I retrieve information from the first page and display in the second page? My code:

firstpage.php<html><body><form action="secondpage.php" method="post">First Name: <input type="text" name="fname" /><br >Last Name: <input type="text" name="lname" /><br />Country:<input type="text" name="country" /><br /><input type="submit" /></form></body></html>secondpage.php<html><body><?php echo $_Post["fname"] <br />;  echo $_Post["lname"] <br />;  echo $_Post["country"]; ?></body></html>

The above code doesn't work. I even tried:

<?php  echo $_Post["fname"];. <br /> <?php  echo $_Post["lname"];.<br />  <?php echo $_Post["country"]; ?>

Link to comment
Share on other sites

You never need to put quotes around a variable to print it. That's becoming a pet peeve of mine seeing people do stuff like this:echo "$var";It's totally unneccessary, just print the variable:echo $var;Exact same thing.You need to use $_POST instead of $_Post.

<?phpecho "{$_POST['fname']} <br />"; echo $_POST["lname"] . "<br />"; echo $_POST['country']; ?>

Link to comment
Share on other sites

You never need to put quotes around a variable to print it. That's becoming a pet peeve of mine seeing people do stuff like this:echo "$var";It's totally unneccessary, just print the variable:echo $var;Exact same thing.You need to use $_POST instead of $_Post.
<?phpecho "{$_POST['fname']} <br />"; echo $_POST["lname"] . "<br />"; echo $_POST['country']; ?>

I changed the $_Post to $_POST and added the bracket but still it isn't working. I am not getting any error message also. Page is empty and when viewed in the view source, that is empty too.
Link to comment
Share on other sites

echo $_POST['country']; and repeat for the other variable,As far as i am concerened, that is a vary basic script and if it is not working, i would look out side that file,the url, the spelling, and every thing else outside the script.
I tried all your codes, and still doesnt work. I even checked the spelling, url, etc.... Is there any line by line debugging available in php?
Link to comment
Share on other sites

I tried all your codes, and still doesnt work. I even checked the spelling, url, etc.... Is there any line by line debugging available in php?
Well, first thing, is the secondpage.php in the same folder as the first page?The second thing is, is php installed :)The third thing is: It should be this by me:echo $_POST[leerlingnummer] . "</br>";I think you forgot the qoutations at the </br>Otherwise I don't know what you are doing:PGood Luck
Link to comment
Share on other sites

Did you add the code I posted?
yes, I added your error code to the second page. It looks like this:
<html><body><?phpecho "display_errors: " . ini_get("display_errors") . "<br/>";ini_set("display_errors", 1);error_reporting(E_ALL); echo $_POST["fname"] <br />;  echo $_POST["lname"]<br />;  echo $_POST["country"]; ?></body></html>

I am able to run other codes. I have installed php. Everything else is fine except for this code. I even tried _REQUEST[""], still same problem. Nothing appears in the page, not even the error msg.

Link to comment
Share on other sites

The very first statement there is an echo statement, if you aren't seeing the "display_errors" message then either PHP is not running at all, or it is configured to not show errors and there are startup errors happening. Is this a server that you are renting online or did you set it up yourself? If you set it up yourself, open php.ini, find the display_errors option and the display_startup_errors option, and make sure they are set to 1. Also find the error_reporting option and make sure it is set to E_ALL. You said you are able to run other code, what code are you running? Submitting a form to a PHP page and reading the form using $_POST is pretty basic, there's not a lot that can go wrong with it. Change your second page to be just this and see what happens:

<?phpvar_dump($_POST);?>

Also, just to mention, you can't run a PHP web page by double-clicking on the file to open it. If the URL in your browser does not start with http or https, then the PHP code is not getting executed.

Link to comment
Share on other sites

And remember, all super-globals have to be in capitals, or else they won't work. $_POST, $_GET, $_REQUEST, etc. or else you will just create a new variable.This is still incorrect:

echo $_POST["fname"] <br />; echo $_POST["lname"]<br />; echo $_POST["country"];

It has to be

echo $_POST["fname"] . "<br />"; echo $_POST["lname"] . "<br />"; echo $_POST["country"];

Link to comment
Share on other sites

The very first statement there is an echo statement, if you aren't seeing the "display_errors" message then either PHP is not running at all, or it is configured to not show errors and there are startup errors happening. Is this a server that you are renting online or did you set it up yourself? If you set it up yourself, open php.ini, find the display_errors option and the display_startup_errors option, and make sure they are set to 1. Also find the error_reporting option and make sure it is set to E_ALL. You said you are able to run other code, what code are you running? Submitting a form to a PHP page and reading the form using $_POST is pretty basic, there's not a lot that can go wrong with it. Change your second page to be just this and see what happens:
<?phpvar_dump($_POST);?>

Also, just to mention, you can't run a PHP web page by double-clicking on the file to open it. If the URL in your browser does not start with http or https, then the PHP code is not getting executed.

Thanks for all the information. I have setup the server. Will change the display_errors option and start_up errors option to 1. I able to run all other php codes in the PHP Tutorial.
Link to comment
Share on other sites

Well, you can use this as a form test. It only needs one file and it doesn't matter what you name it, you can use this to test form processing. I've tested it and it works fine.

<?php$php_output = "";if (isset($_POST['mode']) && $_POST['mode'] == "submit"){  $php_output .= "test1: " . $_POST['test1'] . "\n";  $php_output .= "test2: " . $_POST['test2'] . "\n";  $php_output .= "test3: " . $_POST['test3'] . "\n";  $php_output .= "test4: " . $_POST['test4'] . "\n";  ob_start();  var_dump($_POST);  $php_output .= ob_get_contents();  ob_end_clean();}?><html>  <head>	<title>Form Test</title>  </head>  <body>	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">	  <input type="hidden" name="mode" value="submit">	  <input type="text" name="test1"><br>	  <fieldset style="width: 50px;">		<input type="radio" name="test2" value="1">1<br>		<input type="radio" name="test2" value="2">2<br>		<input type="radio" name="test2" value="3">3<br>	  </fieldset>	  <select name="test3">		<option value="1">1</option>		<option value="2">2</option>		<option value="3">3</option>	  </select><br>	  <textarea name="test4" rows="5" cols="60"></textarea><br>	  <input type="submit">	</form>	<pre><?php echo $php_output;?>	</pre>  </body></html>

Link to comment
Share on other sites

Well, you can use this as a form test. It only needs one file and it doesn't matter what you name it, you can use this to test form processing. I've tested it and it works fine.
<?php$php_output = "";if (isset($_POST['mode']) && $_POST['mode'] == "submit"){  $php_output .= "test1: " . $_POST['test1'] . "\n";  $php_output .= "test2: " . $_POST['test2'] . "\n";  $php_output .= "test3: " . $_POST['test3'] . "\n";  $php_output .= "test4: " . $_POST['test4'] . "\n";  ob_start();  var_dump($_POST);  $php_output .= ob_get_contents();  ob_end_clean();}?><html>  <head>	<title>Form Test</title>  </head>  <body>	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">	  <input type="hidden" name="mode" value="submit">	  <input type="text" name="test1"><br>	  <fieldset style="width: 50px;">		<input type="radio" name="test2" value="1">1<br>		<input type="radio" name="test2" value="2">2<br>		<input type="radio" name="test2" value="3">3<br>	  </fieldset>	  <select name="test3">		<option value="1">1</option>		<option value="2">2</option>		<option value="3">3</option>	  </select><br>	  <textarea name="test4" rows="5" cols="60"></textarea><br>	  <input type="submit">	</form>	<pre><?php echo $php_output;?>	</pre>  </body></html>

When I run the code, I get a text box, radio buttons, dropdown list, text area in a form. Is that what is supposed to come, right? It works. I don't why the other simple code isn't working.
Link to comment
Share on other sites

The very first statement there is an echo statement, if you aren't seeing the "display_errors" message then either PHP is not running at all, or it is configured to not show errors and there are startup errors happening. Is this a server that you are renting online or did you set it up yourself? If you set it up yourself, open php.ini, find the display_errors option and the display_startup_errors option, and make sure they are set to 1. Also find the error_reporting option and make sure it is set to E_ALL. You said you are able to run other code, what code are you running? Submitting a form to a PHP page and reading the form using $_POST is pretty basic, there's not a lot that can go wrong with it. Change your second page to be just this and see what happens:
<?phpvar_dump($_POST);?>

Also, just to mention, you can't run a PHP web page by double-clicking on the file to open it. If the URL in your browser does not start with http or https, then the PHP code is not getting executed.

I changed as specified and now I am getting an error message which I dont understand:
Parse error: syntax error, unexpected T_ECHO in C:\Inetpub\wwwroot\PHP\secondpage.php on line 5

Line 5 is:

echo $_POST["fname"] <br />;

Link to comment
Share on other sites

I changed as specified and now I am getting an error message which I dont understand:
Parse error: syntax error, unexpected T_ECHO in C:\Inetpub\wwwroot\PHP\secondpage.php on line 5

Line 5 is:

echo $_POST["fname"] <br />;

You need to isolate the line break, as it's not a PHP token or anything. PHP allows a few ways to do that, but what I'd use is:
echo $_POST["fname"], '<br />';

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...