Reply to Re: PHP forms completely blank

Your name:

Reply:


Posted by Jerry Stuckle on 02/02/08 14:47

Jezza wrote:
> Jerry Stuckle wrote:
>> Animesh K wrote:
>>> Jerry Stuckle wrote:
>>>> Jezza wrote:
>>>>> Hi
>>>>>
>>>>> I wonder if anyone can help me out. I am a newby and have bought
>>>>> "PHP5 in easy steps" to work through. One project is to have an
>>>>> email form and responder built into one page. The form simply asks
>>>>> for a username, email address and comment. It validates to make
>>>>> sure all fields are filled in, checks the integrity of the email
>>>>> address and then posts it off.
>>>>>
>>>>> I have copied the code from the book exactly & even checked &
>>>>> re-checked the code. Desperately I even scanned the code in via OCR
>>>>> in case I missed something. When I run the php script the resulting
>>>>> web page is blank. Looking at the HTML page source, that is also
>>>>> blank (no <html>, <body> etc. tags). I suspect the code to be sound
>>>>> and that this problem is more to do with something within the
>>>>> php.ini file.
>>>>>
>>>>> I do have PHP files that run fine
>>>>>
>>>>> I am running Apache 2.2 server with PHP version 4.3.2 & here is the
>>>>> code:
>>>>>
>>>>>
>>>>> <html><head><title>Combined Feedback Form</title></head>
>>>>> <body> <?php #assign form values when applicable
>>>>>
>>>>>
>>>>>
>>>>> $username = $_POST['username'];
>>>>> $useraddr = $_POST['useraddr'];
>>>>> $comments = $_POST['comments'];
>>>>> $sent = $_POST['sent'];
>>>>>
>>>>>
>>>>> #the HTML form that can be written dynamically
>>>>>
>>>>> $form ="<form action=\"$_SERVER[`PHP_SELF`]\" method=\"post\">";
>>>>> $form.="Name:<input type=\"text\" name=\"username\"";
>>>>> $form.=" size=\"30\" value=\"$username\" > <br> <br>";
>>>>> $form.="Email:<input type=\"text\" name=\"useraddr\"";
>>>>> $form.=" size=\"30\" value=\"$useraddr\"> <br> <br>";
>>>>> $form.="Comments:<textarea name=\"comments\" ";
>>>>> $form.="cols=\"30\" rows=\"5\">$comments</textarea>";
>>>>> $form.="<br> <input type=\"submit\" name=\"sent\" ";
>>>>> $form. = "value=\"Send Form\"></form>" ;
>>>>>
>>>>>
>>>>> #execute this code if the form has been submitted once
>>>>> if($sent)
>>>>> { $valid=true; #set variable default value
>>>>>
>>>>> #check username field is not blank
>>>>> if( !$username )
>>>>> { $errmsg.="Enter your name:<br>"; $valid = false; }
>>>>>
>>>>> #check email useraddr field is not blank
>>>>> if ( !$useraddr )
>>>>> { $errmsg. =" Enter email address : <br>" ; $valid=false; }
>>>>> #check comments field is not blank
>>>>> if ( !$comments )
>>>>> { $errmsg. = "Enter your comments : <br>" ; $valid=false; }
>>>>> }
>>>>>
>>>>> if( !$useraddr )
>>>>> { $errmsg .="Enter email address:<br>"; $valid = false; }
>>>>> else ;
>>>>> {
>>>>> $useraddr = trim( $useraddr );
>>>>> #patterns for name,domain and top-level domains
>>>>> $_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_'{|}-]+";
>>>>> $_host = " ( [-0-9A-Z]+\.)+";
>>>>> $_tlds = "([0-9A-Z]){2,4}$/i";
>>>>> #check validity of email format
>>>>> if( !preg_match($_name."@".$_host.$_tlds,$useraddr) )
>>>>> {
>>>>> $errmsg .= "Email address has incorrect format!<br>"; $valid =
>>>>> false; }
>>>>> }
>>>>>
>>>>> #if invalid write the error message/s and the form
>>>>>
>>>>> if ( $valid != true )
>>>>> { echo ( $errmsg . $form ) ; }
>>>>>
>>>>> else #if the form is valid send the email
>>>>>
>>>>> { $to = "jo@test.co.uk";
>>>>> $re = "Feedback from $username" ;
>>>>> $msg = $comments;
>>>>> $headers = "From: $useraddr \r\n" ;
>>>>> if ( mail ( $to, $re, $msg, $headers ) )
>>>>> { echo ("Thanks for your comments, $username" ) ; }
>>>>> }
>>>>> ?>
>>>>>
>>>>> </body></html>
>>>>>
>>>>> ---
>>>>>
>>>>> Many thanks
>>>>>
>>>>> Regards
>>>>>
>>>>> Jeremy
>>>>>
>>>>
>>>> Jeremy,
>>>>
>>>> I didn't check all of your code, but to start, your line:
>>>>
>>>> $form ="<form action=\"$_SERVER[`PHP_SELF`]\" method=\"post\">";
>>>>
>>>> uses back tickeys (`) instead of single quotes ('). Hard to see, I
>>>> know.
>>>>
>>>
>>> I use single quotes and it works fine on a Linux hosting service with
>>> Php5.0
>>>
>>
>> Yes, that's the way you should do it. he didn't. Read my message again.
>>
> Just to clear up a little discrepancy here..... I tried (')s and (`)s.
> Yes my code does have (`)s but this was only out of
> desperation....regardless of which one I use I don't get anything at all
> displayed. I accept that I need to use single quotes. I guess the main
> thing that has helped (thanks Jerry) is to turn error logging on & now I
> am getting an error response from the parser:
>
> "Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting T_STRING or T_VARIABLE or T_NUM_STRING in email2.php on line 12"
>
> This is line 12:
>
> $form ="<form action=\"$_SERVER['PHP_SELF']\"method=\"post\" >";
>
> My suspicions are with the \"$_SERVER['PHP_SELF']\" bit....it is as if
> the parser does not recognise PHP_SELF . Does this need to be enabled
> somehow? I have got register_globals = off in my php.ini (I was advised
> to do this as a security measure)
>
>
> Thanks to all for their input, I am going back to tearing my hair out
> but at least now I have an error message to work with.
>
>
> Regards
> Jeremy
>

Jeremy,

No, nothing needs to be enabled to use the $_SERVER variables. They are
available on any page loaded by a web server.

But since it's an array instead of a simple variable, you need to
surround it with { and }, i.e.

$form ="<form action=\"{$_SERVER[`PHP_SELF`]}\" method=\"post\">";


And BTW, I agree with Vince. So far I'm not crazy about the example
code I've seen. Maybe the rest of the book is better. I hope so.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация