php help

He has: 1,380 posts

Joined: Feb 2002

[i'm retarded...please move this to the Scripting Help]

hey, i've been dabblin' in PHP and i cant these two scripts to work...basically, i have one thats inside a page that is supposed to read a file and then print it, but only the text of the script comes up (check it out at http://westernciv.sarvihosting.com/baseball), and then i have one thats supposed to write to the db (its flat-file)...here it is:

<?php

  $fh
= fopen("data.txt", "w");

 
fucntion validate_form()
  {
    global
$subject, $body;

   
$errors=0
   
if (!trim($subject))
    {
    echo
"<BR><font color="red"><B>Subject</B> is required.</font>";
   
$errors++;
    }

    if (!
trim($body))
    {
    echo
"<BR><font color="red"><B>Message Body</B> is required.</font>";
    }

    switch (
$errors)
    {
    case
0:
        return
TRUE;

        case
1:
        echo
"<BR><BR><font color="red">Please use your browser's back button to return to the form.";
        return
FALSE;

        default:
        echo
"<BR><BR><font color="red">Please use your browser's back button to return to the form.";
        return
FALSE;
        }
    }

    function
update_database()
    {
    echo
"<BR>Updating the database...";
    }

   
$ok = validate_form();
    if (
$ok)
   
update_database();   

@
fclose($fh)
?>


</BODY>
</HTML>
'

is the "PHP"-politically correct? cuz, i'm getting an error on "line 5"...a page is using this script to post to a site...is this even right? i'm so confused....

one more thing...how would i automaticall add a date (date that it was when submitted) and a when they submit (date on top and on bottom)...thanks

Busy's picture

He has: 6,151 posts

Joined: May 2001

you have no ";" after $errors=0
also you are doubling quotes in your echo statments
echo "Subject is required.";
should be
echo "Subject is required.";
or use single quotes around it, there is a couple of others there too

not sure about the rest

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

fucntion validate_form() {

should be
function validate_form() {

If you don't fix the double quotes, once it gets past the first error it'll die there too... you'll get an line 10 too if you don't put the ; after $errors = 0

Also, on the site, you must not have an open tag for php

<?php
</strong> or <strong> <? </strong>depending on what you are allowed to do
<?
php
   
function update_database()
    {
    echo \
"<BR>Updating the database...\";
    }
?>

Does that actually update the DB or just print that?

Laughing out loud

He has: 1,380 posts

Joined: Feb 2002

i also just realized that it only validates it...i'm retarded...i'm updating the code...lets see what happens

He has: 1,380 posts

Joined: Feb 2002

ok heres the acutal code:

<?php

 
function validate_form()
  {
    global
$subject, $body;

   
$errors=0;
    if (!
trim($subject))
    {
    echo
"<BR><font color=\"red\"><B>Subject</B> is required.</font>";
   
$errors++;
    }

    if (!
trim($body))
    {
    echo
"<BR><font color=\"red\"><B>Message Body</B> is required.</font>";
    }

    switch (
$errors)
    {
    case
0:
        return
TRUE;

        case
1:
        echo
"<BR><BR><font color=\"red\">Please use your browser's back button to return to the form.";
        return
FALSE;

        default:
        echo
"<BR><BR><font color=\"red\">Please use your browser's back button to return to the form.";
        return
FALSE;
        }
    }

function
update_database()
{
          echo
"<br>Updating database.";
$ok = validate_form()
$fp = fopen ("/home/westernc/public_html/baseball/data.txt", "w");
 
fwrite($fp, "$subject");
 
fwrite($fp, "$body");
 
fclose ($fp);
}

if (
$ok)
      
update_database();
?>


</BODY>
</HTML>
'

i'm getting an error on line 38...anybody see anything...cuz thats the path and everything...? thanks

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

<?php
 
function validate_form()
  {
    global
$subject, $body;

   
$errors=0;
    if (!
trim($subject))
    {
    echo \
"<BR><font color=\\"red\\"><B>Subject</B> is required.</font>\";
   
$errors++;
    }

    if (!trim(
$body))
    {
    echo \"<BR><font color=\\"
red\\"><B>Message Body</B> is required.</font>\";
    }

    switch (
$errors)
    {
    case 0:
        return TRUE;

        case 1:
        echo \"<BR><BR><font color=\\"
red\\">Please use your browser's back button to return to the form.\";
        return FALSE;

        default:
        echo \"<BR><BR><font color=\\"
red\\">Please use your browser's back button to return to the form.\";
        return FALSE;
        }
    }

$ok = validate_form();
function update_database()
{
          echo \"<br>Updating database.\";

$fp = fopen (\"/home/westernc/public_html/baseball/data.txt\", \"w\");
  fwrite(
$fp, \"$subject\");
  fwrite(
$fp, \"$body\");
  fclose (
$fp);
}

if (
$ok) {
       update_database();
}


</BODY>
</HTML>
?>

You were missing a semi at the end of 37 (Which I moved anyways)
Also, you didn't have the { and } for the if ($ok) update_Database()

I moved $ok = validate_form() because if you keep it in the function for update_database() it won't run until update_database() is run too
I ran it through the Zend Development Environment Personal Studio and it turned up OK...

Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.