RE: Re: php form action breaks script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Tim Dunphy [mailto:bluethundr@xxxxxxxxx]
> Sent: 28 June 2012 01:18
> 
> Hey guys,
> 
> It's been a little while since I've toyed with this, and I hope you
> don't mind my coming back to you for some more advice. But I've
> enjoyed some limited success with David R's advice regarding adding
> some strong quoting to the mix. Here is what I last tried -
> 
>  <form method="post" action="' . $_SERVER['[PHP_SELF'] .'">

Wow! That's completely wacko! (OK, just looked at the full code and
seen it's in the middle of a single-quoted echo, so it's not that bad
after all :). You've got a spare [ in there -- the notice saying
Undefined index: [PHP_SELF should have alerted you to this, as the
index you want is just plain PHP_SELF.

   <form method="post" action="' . $_SERVER['PHP_SELF'] .'">

> The pages do work, and the form checking code does its job (empty
> text
> displays what information is missing). Except that there is an
> annoying message that appears on screen and in the logs -
> 
> Notice: Undefined index: subject in
> /Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendema
> il.php
> on line 23 Notice: Undefined index: elvismail in
> /Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendema
> il.php
> on line 24 Notice: Undefined index: [PHP_SELF in
> /Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendema
> il.php
> on line 62

Looking at the relevant bit of your script (assume this is line 23
onward):

>   $subject = $_POST['subject'];
>   $text = $_POST['elvismail'];
>   $output_form = "false";
> 
> 
>   if (isset($_POST['Submit'])) {

You're accessing $_POST['subject'] and $_POST['elvismail'] *before* the
check to see if this is a from a form submission - on the initial access,
to just display the form, these $_POST indexes will not be set so
causing the notices.

You either need to put the assignments inside the

  if (isset($POST['Submit']))

branch, or conditionalise them in some way, such as:

    $subject = isset($_POST['subject']) ? $_POST['subject'] : NULL;
    $text = isset($_POST['elvismail']) ? $_POST['elvismail'] : NULL;


Another oddity in your script is that you're using the string values
"true" and "false" instead of the Boolean true and false. Because of
the way PHP typecasts, both "true" and "false" are actually regarded
as Boolean true, which could get a little confusing -- so it's much
better (and probably more efficient) to use the proper Boolean values.
Also, it enables your later test to be written, with confidence, as
just

    if ($output_form) {


Also, also, with a little bit of rearrangement of the tests, you can
reduce the amount of code a bit:

    if (!empty($subject) && !empty($text)) {
        // Both inputs supplied -- good to go.
        $output_form = false;
    } else {
        // At least one input missing -- need to redisplay form
        $output_form = true;

        if (empty($subject)) {
            if (empty($text)) {
                echo 'You forgot the email subject and body.<br />';
            } else {
                echo 'You forgot the email subject.<br />';
            }
        } else {
            echo 'You forgot the email body text.<br />';
        }
    }

Actually, I think my inclination would be to assign $output_form
first, and then do the rest of the tests:

    $output_form = empty($subject) || empty($text);

    if ($output_form) {
        // At least one input missing -- work out which one
        if (empty($subject))
            if (empty($text)) {
                echo 'You forgot the email subject and body.<br />';
            } else {
                echo 'You forgot the email subject.<br />';
            }
        } else {
            echo 'You forgot the email body text.<br />';
        }
    }

That said, there are lots of personal preferences involved here, and
I'm sure others would offer different possibilities. (Me personally,
I also prefer the alternative block syntax with initial : and end...
tags -- but then, the forests of curly braces others seem to find
acceptable make my eyes go fuzzy, so go figure....)

Cheers!

Mike

-- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Portland PD507, City Campus, Leeds Metropolitan University,
Portland Way, LEEDS,  LS1 3HE,  United Kingdom 
E: m.ford@xxxxxxxxxxxxxx     T: +44 113 812 4730






To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux