You are not logged in.
I have been following the tutorial at Zend, which is excellent, but some scripts seem to spit out blank pages while others work fine. I configured a LAMP server on my desktop using the LAMP Wiki entry here, while the test file from the wiki worked fine. Also, I am running these scripts from /home/me/public_html/. I don't know if they are just bad scripts or what, but here are the ones that work:
<html>
<head>
<title>PHP Test Page</title>
</head>
<body>
This is Arch Linux, running PHP.
<?php
phpinfo();
?>
</p>
</body>
</html>
<html>
<head>
<title>Please work!</title>
</head>
<body>
<?php echo "I better see this text..."; ?>
</body>
</html>
While these do not work:
<html>
<head>
<title>PHP Cat</title>
</head>
<body>
<!-- Unfinished! -->
<?php
if (!isset($_POST['submit'])) {
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
Number of files to read: <input type="text" name="number">
</form>
<?php
}
else {
$file_num = $_POST['number'];
echo "<form action=\"<?php $_SERVER['PHP_SELF']; ?>\" method="POST">";
for ($int, $int <= $file_num, $int++) {
echo "File $int: <input type=\"text\" name=\"file$int\">";
}
echo "</form>";
?>
</body>
</html>
<html>
<head></head>
<body>
<?php
/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter your age: <input name="age" 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 result
$age = $_POST['age'];
if ($age >= 21) {
echo 'Come on in, we have alcohol and music awaiting you!';
}
else {
echo 'You're too young for this club, come back when you're a little older';
}
}
?>
</body>
</html>
Last edited by Bionic Apple (2008-08-10 05:36:16)
Offline
Hello!
Your problems are the quotes. PHP can differenciate between " and '. And, hm, it`s just hard to explain. Eg:
echo "Hello 'guy'";
would result in: Hello 'guy'
whereby
echo "Hello "guy"";
would just result in a php-parser-error -> so a blank page.
You can output PHP variables inside echo e.g. in this way:
echo "Hello guy", $number,", I really like you!";
But I think a good tutorial-writer can explain this much much better ...
So, just change your first problemematic php-file into:
<html>
<head>
<title>PHP Cat</title>
</head>
<body>
<!-- Unfinished! -->
<?php
if (!isset($_POST['submit'])) {
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
Number of files to read: <input type="text" name="number">
</form>
<?php
}
else {
$file_num = $_POST['number'];
echo "<form action='", $_SERVER['PHP_SELF'], "' method='POST'>";
for ($int, $int <= $file_num, $int++) {
echo "File ",$int,": <input type='text' name='file",$int,"'>";
}
echo "</form>";
?>
</body>
</html>
Well and in your second line it`s at least this line:
else { echo 'You're too young for this club, come back when you're a little older'; }
The php parser would expect to output: "You" and then there would be php-code with this syntax:
re too young for this club, ....
And this stuff is just unreadable for the php-parser ...
so change this line it:
else {
echo "You're too young for this club, come back when you're a little older";
}
Try to understand the php-system for quotes and you will be rid ob at least these errors ...
And I`m not too sure whether I found all errors, because I havn`t got a php-parser at hand now and my solution is just one out of countless ones ... but I know for certain that it`s not an arch-issue at all ;-)
So, good luck with learning php
Offline
for ($int, $int <= $file_num, $int++) {
echo "File ",$int,": <input type='text' name='file",$int,"'>";
}
little offtopic: this isn't clean code at all. please do so:
for ($file_count = 1, $file_count <= $file_num, $file_count++) {
echo 'File ' . $int . ': <input type="text" name="file'. $int . '" />';
}
uhm, and "," is no connector at all, use "." @top_se
and <input> has to be "omitted" to get valid with W3C Standards
for HTML quotes, use ", and for PHP strings use '. But mention that non-printable characters like \n must be quotet with ".
Edit: Althoug, for coding, PHP errormessages should be displayed, to check if its turned on check your php.ini config file.
Last edited by orion (2008-08-10 14:49:15)
Offline
Unfortunately, I won't have access to my Arch desktop for 4 more hours, but there is one thing I would like to point out. The fourth example, where it asks for your age, came straight from the tutorial site. Back when I used Ubuntu, that code worked with no problems. Also, thanks for the tip orion. However, last time I checked double quotes around a string lets you input PHP variables, rendering it unnecessary to have multiple strings. Regarding the error messages, I attempted to change it after I saw blank pages, but I didn't know exactly what to change. I did change an option in php.ini that sounded like it was the one, but the faulty scripts kept on outputting blank pages.
Offline
Alright, I finally found the elusive "Display Errors" option and then googled each error that I found. So, just for reference, here is the unfinished-but-working PHP script:
<html>
<head>
<title>PHP cat</title>
</head>
<body>
<!-- Unfinished! -->
<?php
if (!isset($_POST['submit'])) {
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
Number of files to read: <input type="text" name="number">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else {
$file_count = $_POST['number'];
echo '<form action="<?php $_SERVER[\'PHP_SELF\']; ?>" method="POST">';
for ($file_num = 1; $file_num <= $file_count; $file_num++) {
echo "File $file_num: <input type='text' name='file$file_num'>";
}
}
echo "</form>";
?>
</body>
</html>
Last edited by Bionic Apple (2008-08-11 02:42:12)
Offline
Even though the thread topic has been solved, I would like to see if someone knows why this script always gives a Object Not Found error when a file location is submitted.
<html>
<head>
<title>PHP cat</title>
</head>
<body>
<!-- Sort of finished -->
<?php
if (!isset($_POST['submit1'])) {
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
Number of files to read: <input type="text" name="number">
<input type="submit" name="submit1" value="Submit">
</form>
<?php
}
elseif (isset($_POST['submit1']) && !isset($_POST['submit2'])) {
$file_count = $_POST['number'];
echo '<form action="<?php $_SERVER[\'PHP_SELF\']; ?>" method="POST">';
for ($file_num = 1; $file_num <= $file_count; $file_num++) {
echo "\nFile $file_num: <input type='text' name='file$file_num'> <br />";
}
echo "\n<input type='submit' name='submit2' value='Submit'>\n</form>";
}
else {
echo "\n<table>";
for ($file_num = 0; isset($_POST["file$file_num"]); $file_num++) {
echo "\n<tr>\n<td>";
// set file to read
$file = $_POST["file$file_num"] or die('Could not open file!');
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
// close file
fclose($fh);
// print file contents
echo "\n$data";
echo "\n</td>\n</tr>";
}
echo "\n</table>";
}
?>
</body>
</html>
Offline
No one? It seems as though all scripts involving file-opening are returning Object Not Found errors from the Apache server.
Offline
This line is wrong :
echo '<form action="<?php $_SERVER[\'PHP_SELF\']; ?>" method="POST">';
Look at the source code of the generated web page and the url on the page that says "Object not found". The apache server can't find the file named <?php $_SERVER[\'PHP_SELF\']; ?>
You want the value of $_SERVER['PHP_SELF'] in the generated page, and not <?php $_SERVER[\'PHP_SELF\']; ?> : the generated page won't be processed by php again. (not sure if i'm explaining this well ...)
What you want is :
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
There are other errors. After you hit submit2, $_POST['submit1'] is not set so your "if" block will be executed again.
In your "elseif" block the "for" loop starts at 1 and in your "else" block the "for" loop starts at 0 : $_POST['file0'] is never set, so the loop is never executed.
Offline