Jump to content

Proper syntax for php embedded in html web page


eagle1771

Recommended Posts

Greetings Php Helpers,

 

I am about ready to start pulling out (what's left of) my hair. I am new to php programming and could use some help. I have spent a huge amount of time reading through three php books and searching through the forums, tutorials, code examples, etc. here before asking this question. I am also betting that there are a LOT of php 'newbies' that struggle with this same problem. (And yes, I have looked at "syntax" in the Php manual).

 

I have written a rather large form at http://www.wchs1969.com/newaddreg.php

 

Here's the problem. The form displays properly as an html page. It even displays properly with the following php code placed just before the closing "</body>" tag in the web page.

 

<code>

<?php

echo 'Hello, world!';

?>

</code>

 

Now when I change the php portion of the code to this:

 

<code>

<?php

 

//create variables from form data

 

if ($_POST) {

foreach($_POST as $fn => $v) {

    $v = trim($v) ;

    $$fn = $v ;

}

 

echo 'Hello, world!';

?>

</code>

 

I get a blank page. My question, of course, is why? I realize that this is probably a syntax error or that the php processor does not know what to do with this new code. I have been looking all over for something that explains the syntax 'rules' for php and I cannot seem to find what I am looking for.

 

The actual code that I was trying to add to this form is much more extensive than the small amount that I posted above, as it is intended to do some error checking of email addresses, required fields, etc., as well as posting to a database, but I finally just cut it down to try to get the form to display.

 

Can someone please point me to an explanation of what I am probably doing wrong (or just explain it here)?

 

I have used NoteTab Pro and WeBuilder 2007 (purchased) to write the php code. This is being done on a fairly new Toshiba Satellite P105-S6147 with 2Gb ram and (unfortunately) Windows Vista Ultimate. The web host is Host4Profit.net I am using WS_Ftp Pro for file transfer. If needed, I will be happy to post the entire php code as well as the html code, in two separate text files, or however you prefer.

 

And the three books that I mentioned above are:

 

PHP Fast & Easy Web Development 2nd Edition by Julie C. Meloni - pretty good

teach yourself PHP with MySQL by Nat Macbride - also good

PHP and MySQL for Dynamic Web Sites 2nd Edition Visual QuickPro Guide by Larry Ullman - Excellent (in my newbie opinion)

 

Thank you.

Ralph Moore

would-be php programmer

 

Link to comment
Share on other sites

I just realized that the code was not displaying properly in the code blocks. Here they are.

 

<?php
echo 'Hello, world!';
?>

 

<?php

//create variables from form data

if ($_POST) {
foreach($_POST as $fn => $v) {
    $v = trim($v) ;
    $$fn = $v ;
}

echo 'Hello, world!';
?>

Link to comment
Share on other sites

save your self a ton of headaches and make a file called phpinfo.php that just has

<?php
phpinfo();
?>

 

 

and then find the line about display_errors

 

if it says off odds are you will want to change it or find a server you can report errors becuase you are probably going to keep creating errors (I do too), and not be able to see them.

Link to comment
Share on other sites

Where is $v defined.

 

Why two $$ in front of $$fn

 

That section of code is shorthand (I think). What it says is loop through the entire form

and assign variable variables for each named field name / value pair

and then use trim to get rid of any leading or trailing spaces.

 

It's from page 104 of the second book I mentioned.

 

Anyway, I know it does work, when coupled with the form and the email function, because I did in fact get the form to email me the results.

 

Link to comment
Share on other sites

save your self a ton of headaches and make a file called phpinfo.php that just has

<?php
phpinfo();
?>

 

 

and then find the line about display_errors

 

if it says off odds are you will want to change it or find a server you can report errors becuase you are probably going to keep creating errors (I do too), and not be able to see them.

 

I ran phpinfo() a long time ago and while display_errors is off, I have added

ini_set('display_errors', 2048);

right below the opening php tag "<?php" - which is supposed to do the same thing on a per script basis.

 

I'm not seeing any errors.

 

I can also look at the error log in the web host control panel, and while it does show php errors from time to time whenever I 'tweak' the code, it is not meaningful, at least to me.

Link to comment
Share on other sites

cooldude832,

 

Here is the relevant line from the web host error logs:

 

[sat Nov 24 12:33:34 2007] [error] PHP Parse error:  syntax error, unexpected $end in /home/wchs1969/wchs1969-www/newaddreg.php on line 543

 

Uh, when I look in the code, there is no $end that I can see... anywhere.

 

Any suggestions?

Link to comment
Share on other sites

What I am seeing on line 543 is the final </html> closing tag and that is the end of the file. Do I need to then add another bit of code like this?:

 

<?php
?>

 

And if so, what exactly are the rules regarding mixing html and php? I have seen examples that seem to suggest that as long as you name the file with a php extension, it doesn't matter how you start or end the file as long as each set of starting and ending php tags are a complete set.

Link to comment
Share on other sites

I think I found the problem.

 

I actually needed a second "}" to close the "if" code. I think php was looking for that and threw an unexpected end (of file or routine) because it did not know how else to handle it.

 

Anyway, it now displays with the added code where it would only give me a blank screen before.

 

Thanks for all the help. I'm sure I will have more questions later.

Link to comment
Share on other sites

Where is $v defined.

 

Why two $$ in front of $$fn

 

$$fn would be the variable with the name stored in $fn.

 

if $fn=="table" then the variable $$fn==$table.

 

So, what this loop is doing is storying each key=> value pair as $$key=>$value

 

So if our list of keys are {"table","chair","monkey"} then $table,$chair,and $monkey will exist

 

These are known as variable variables.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.