Jump to content


Photo

Having trouble with PHP 4 book and running PHP 5!


  • Please log in to reply
6 replies to this topic

#1 darrenfinch

darrenfinch
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 March 2006 - 10:20 AM

Hi,

I know I should find some more up to date material - but getting this problems is one way of REALLY understanding the code!

I have been following PHP 4 weekend crash course, and I cannot for the life of me get this to work.

If I create a form and $_POST to say handler.php
handler.php contains
<?php phpinfo() ?> // this works - if I scroll down to the variables I can see all the input from the form

however, if I change the form to $_POST to handler1.php, which contains
<?php
//Generic form data parser
// Begin HTML page and field table
echo "<html><head><title>Check Form</title></head><body><table cellpadding=20 border=1>";
echo "<tr><td><b>Name</b></td><td><b>Value</b></td></tr>";
// For each $_POST value
foreach ($_POST as $key => $value)
{ // Print the key in the first column
print "<tr><td>" . $key . "&nbsp; </td>\n";
print "<td>";

// If the field is an empty array, parse it
if (is_array($value))
{ foreach ($value as $selkey => $selvalue)
{ print $selkey . " : ". $selvalue ."&nbsp; <br />";}
} else { // Else (not array) just print the value
// in second column
print $value . "&nbsp;";
}
print "</td></tr>\n";
}
print "</table></body></html>";
?>

All I get is the HTML header

Can anyone help please ?

#2 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 10 March 2006 - 10:26 AM

<?php
//Generic form data parser
// Begin HTML page and field table
echo "<html><head><title>Check Form</title></head><body><table cellpadding=20 border=1>";
echo "<tr><td><b>Name</b></td><td><b>Value</b></td></tr>";
// For each $_POST value
foreach ($_POST as $key => $value)
{ // Print the key in the first column
    print "<tr><td>" . $key . "&nbsp; </td>\n";
    print "<td>"; 
    
    // If the field is an empty array, parse it
    if (is_array($value)) 
    { 
        foreach ($value as $selkey => $selvalue)
        { 
            print $selkey . " : ". $selvalue ."&nbsp; <br />";
        }
    } 
    else 
    { 
        // Else (not array) just print the value
        // in second column
        print $value . "&nbsp;";
    }
print "</td></tr>\n";
}
print "</table></body></html>";
?>

worked absolutely fine for me.

try placing



<form method='post'>

<input type='text' name='test1' /><br />
<Input type='text' name='test2' /><br />
<input type='text' name='test3' /><br />
<input type='submit'>

</form>

directly under the handler in handler1.php

load the page up and fill out the form. If you get it OK from there something's going wrong between your form and the handler page. Have you definitely set the action to the correct handler?
call me a safe bet, i'm betting i'm not

#3 darrenfinch

darrenfinch
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 March 2006 - 10:47 AM

I thought with PHP 5 you were supposed to use the superglobal variables ?
I found with other forms I have had to use $_POST in all cases (POST returns blanks)
i.e. POST becomes $_POST

I tried it without the $_ and I still get the same

and if I use the same method ($_POST) with the phpinfo () I see the variables, so I assume that the form input is correct but somehow the variables are not being passed/parsed correctly in the handler1.php script but I cannot see why!

I dont think it is the web server either (everything else I have thrown at it works great)

[!--quoteo(post=353576:date=Mar 10 2006, 05:26 AM:name=lessthanthree)--][div class=\'quotetop\']QUOTE(lessthanthree @ Mar 10 2006, 05:26 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<?php
//Generic form data parser
// Begin HTML page and field table
echo "<html><head><title>Check Form</title></head><body><table cellpadding=20 border=1>";
echo "<tr><td><b>Name</b></td><td><b>Value</b></td></tr>";
// For each $_POST value
foreach ($_POST as $key => $value)
{ // Print the key in the first column
    print "<tr><td>" . $key . "&nbsp; </td>\n";
    print "<td>"; 
    
    // If the field is an empty array, parse it
    if (is_array($value)) 
    { 
        foreach ($value as $selkey => $selvalue)
        { 
            print $selkey . " : ". $selvalue ."&nbsp; <br />";
        }
    } 
    else 
    { 
        // Else (not array) just print the value
        // in second column
        print $value . "&nbsp;";
    }
print "</td></tr>\n";
}
print "</table></body></html>";
?>

worked absolutely fine for me.

try placing

<form method='post'>

<input type='text' name='test1' /><br />
<Input type='text' name='test2' /><br />
<input type='text' name='test3' /><br />
<input type='submit'>

</form>

directly under the handler in handler1.php

load the page up and fill out the form. If you get it OK from there something's going wrong between your form and the handler page. Have you definitely set the action to the correct handler?
[/quote]


#4 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 10 March 2006 - 10:51 AM

The form method should still be set to POST if that's what you mean.

I think i might have misunderstood your post though.
call me a safe bet, i'm betting i'm not

#5 darrenfinch

darrenfinch
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 March 2006 - 02:20 PM

Also, I just tried viewing the page source; all I get is the HTML!

i.e.
<html>
...
<body>
Name
Value
</tr></table></html></body>

It is ignoring everything after the foreach()

even if I put a print"hello world"; after the foreach() it will not display hello world

This is crazy!



[!--quoteo(post=353574:date=Mar 10 2006, 05:20 AM:name=DarrenFinch)--][div class=\'quotetop\']QUOTE(DarrenFinch @ Mar 10 2006, 05:20 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi,

I know I should find some more up to date material - but getting this problems is one way of REALLY understanding the code!

I have been following PHP 4 weekend crash course, and I cannot for the life of me get this to work.

If I create a form and $_POST to say handler.php
handler.php contains
<?php phpinfo() ?> // this works - if I scroll down to the variables I can see all the input from the form

however, if I change the form to $_POST to handler1.php, which contains
<?php
//Generic form data parser
// Begin HTML page and field table
echo "<html><head><title>Check Form</title></head><body><table cellpadding=20 border=1>";
echo "<tr><td><b>Name</b></td><td><b>Value</b></td></tr>";
// For each $_POST value
foreach ($_POST as $key => $value)
{ // Print the key in the first column
print "<tr><td>" . $key . "&nbsp; </td>\n";
print "<td>";

// If the field is an empty array, parse it
if (is_array($value))
{ foreach ($value as $selkey => $selvalue)
{ print $selkey . " : ". $selvalue ."&nbsp; <br />";}
} else { // Else (not array) just print the value
// in second column
print $value . "&nbsp;";
}
print "</td></tr>\n";
}
print "</table></body></html>";
?>

All I get is the HTML header

Can anyone help please ?
[/quote]


#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 March 2006 - 02:59 PM

Two questions:

#1 - are you actually saving your work as a .php file rather than as a .html file?

#2 - are you testing this on a live server or just in some editor preview mode?
Legend has it that reading the manual never killed anyone.
My site

#7 darrenfinch

darrenfinch
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 March 2006 - 03:06 PM

Andy,

Am using Abyss web server
form is .html file parsing into handler1.php

I found the error (just), the book is incorrect!
(rather embaraased that I did not spot it sooner!)

The form uses $_POST to post the info on the server, and php script was also using the $_POST method (i..e not $_GET)

this works now, and I can go home happy!

Many thanks



[!--quoteo(post=353629:date=Mar 10 2006, 09:59 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Mar 10 2006, 09:59 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Two questions:

#1 - are you actually saving your work as a .php file rather than as a .html file?

#2 - are you testing this on a live server or just in some editor preview mode?
[/quote]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users