Required Field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cluelessphp
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:28 am

Required Field

Post by cluelessphp »

Hi there, I write html and css but I know nothing about php. I have a mailing list form that uses a list.php file to work. I copied the code from somewhere a long time ago. I have three websites using this code for a mailing list. My mailing list doesn't have required fields and it only has one field, email. I'm getting tons of emails from someone just clicking my submit button and sending an email with no address. I get those regularly, but for two weeks I've been getting one every hour starting at 3am for hour after hour on two of my sites.

I can't go through all the pages on my site and change the form in my html right now. I've been looking for a way to change the list.php to require the field so I can just overwrite that once. I've looked and looked and I've probably found the solution, but it always includes these other fields -- name, address, age, first born -- and I believe that when I remove them since I don't know php, I'm ruining the code by adding a space in the wrong place or a bracket or something like that.

My form code is:
<form action="list.php" method="post" style="margin:0px"><input id="email" type="text" name="email" size="13" value=" Mailing List" onFocus="this.value=''"><input type="submit" value=" Join "></form>

Here is my list.php code:

Code: Select all

<?php
$to = 'xxx@domain.com';
$subject = 'Mailing List';
$from = 'email@domain.com';
$message ='';
 
foreach($_POST as $name => $data)
{
  if(is_array($data))
  {
    foreach($data as $datum)
    $message .= $name . ": " . $datum . " \n";
  }
else
  $message .= $name . ": " . $data . " \n";
}
 
{
header("Location: http://www.domain.com/list.html"); 
} 
 
# Attempt to send email
if(mail($to, $subject, $message, "From: $from"))
  echo "Mail sent";
else
  echo "Mail send failure - message not sent";
?>
Could anyone be so kind as to just rewrite it and include the code that's needed to require the "email" field?

Many, many thanks if anyone can. :)
phoenixrises
Forum Newbie
Posts: 8
Joined: Sun Nov 15, 2009 8:24 am

Re: Required Field

Post by phoenixrises »

Not going to write it for you. But, check if the $_POST['email'] has a value (empty()) after the foreach. If the value is empty then header redirect back to the original form before the mail() call. Simple.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Required Field

Post by califdon »

It's pretty hard to help you, since the code doesn't make much sense to me. Your form has only one input field, but your script is looking for an array, then you don't store the data anywhere, just send an email. But that aside, if you just want to kill the process if there's no data, you could insert the following line immediately after the opening <?php line:

Code: Select all

if (empty($_POST['email'])) exit;
cluelessphp
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:28 am

Re: Required Field

Post by cluelessphp »

It won't be empty. It will say "Mailing List". That's probably my problem with finding a solution.
phoenixrises
Forum Newbie
Posts: 8
Joined: Sun Nov 15, 2009 8:24 am

Re: Required Field

Post by phoenixrises »

Eh? No it wont. If the form is submitted with an empty 'email' input, then $_POST['email'] will be an empty equivalent.
cluelessphp
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:28 am

Re: Required Field

Post by cluelessphp »

Is "empty 'email' input" the same as nothing in the email text field? Because the email text field will have text in it that says "Mailing List". That's how my form is set up.

I don't know php, so you don't have to talk down to me. I know I'm clueless about it.
phoenixrises
Forum Newbie
Posts: 8
Joined: Sun Nov 15, 2009 8:24 am

Re: Required Field

Post by phoenixrises »

Gotcha, was just me being a tool and only skimming the HTML :P.

Two options.

1) check for empty and for the text 'Mailing List' and if either is true, then redirect.

2) Use regex to confirm that the input is a valid e-mail address. 'Mailing List' and empty will fail, then redirect.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Required Field

Post by califdon »

I overlooked that, too. So just change the line to:

Code: Select all

if ($_POST['email'])=='Mailing List') exit;
Were it me, I wouldn't put a default value in there like that. Also, I now see that you are inviting them to enter one or more email addresses there, but your code doesn't support that. Just entering a comma delimited list will not result in a PHP array when you read the Post value, it will just be a string with some commas in it. That's what I mean about the code not making sense. That's the problem with copying code from somewhere when you don't know the language. But at least you can avoid the blank messages by doing the above.

Note to ~phoenixrises: all your points are technically correct, but they will have no meaning or value to someone who does not understand PHP. In this forum, the objective is to help people.
cluelessphp
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:28 am

Re: Required Field

Post by cluelessphp »

I'm sure it does work, but it gives me this: "Parse error: syntax error, unexpected T_IS_EQUAL in list.php on line 20". That's why I wanted someone to write it in the code so I could just paste the whole thing because I don't know where to put it.

I thank you both for your time. I appreciate that you tried. It was very nice of you.
:D
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Required Field

Post by califdon »

The very first thing you must learn if you're going to deal with any programming language is to pay scrupulous attention to details. Computers interpret your code very literally and you can't be casual about anything. Ever.

The error message you quoted refers to line 20 of your script. My instructions to you were to insert the line "immediately after the opening <?php tag" which would mean that it should be line 2.
cluelessphp
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:28 am

Re: Required Field

Post by cluelessphp »

Gives me the same error on line 2, "Parse error: syntax error, unexpected T_IS_EQUAL in list.php on line 2"

But forgawdsakes DO NOT -- DO NOT -- put it in the code in full where it will work and both of us could on to better things.

No, no, no, no!!!!!! Don't!!! You guys had time to write that you would NOT write it for me, lecture me, and babbling about in php code that I didn't understand, and lecture each other -- BUT NOOOOOOOOOOOOO do NOT just put it in the code so that it will work for someone is clueless about php.

Because that would be too easy ...................


Thank you, thank you very much


for wasting my time.
cluelessphp
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:28 am

Re: Required Field

Post by cluelessphp »

Moderator, you're welcome to delete and ban me.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Required Field

Post by califdon »

I won't delete and ban you, but I will advise you to simmer down and think about what you're asking for. This is a PHP Developer's Network forum. Many of us make our living writing code for pay (I don't, anymore). We are willing to help other developers who have problems, and even help newcomers get started learning PHP. We are not here to supply fully coded solutions to the public for free. If that's what you need, you should be asking for it in a forum that does that.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Required Field

Post by califdon »

Oops! Thanks, McInfo. I always do that when I'm in a hurry.

Code: Select all

if ($_POST['email']=='Mailing List') exit;
Sorry.
Post Reply