...

View Full Version : Help needed with Mailing List PHP script.



sonofjack
12-01-2008, 10:36 PM
Hi

I've created a page that allows people to enter details to join a mailing list.
If required fields aren't entered then it is meant to display "Your name is required" etc. It actually does all that now, but the "Your name is required" and "Your address is required" bits are on the page right from the beginning, and I want them to only appear after the Submit button has been pressed.
The code looks like it should do that, but something is missing somewhere.
Could someone please check it out for me please?

The page is

http://www.dancesounds.co.uk/testingphp/mailinglist.php

The code is

<?php

$required = array("name" => "Name",
"address1" => "Address",
"town" => "Town/City",
"postcode" => "Postcode");

foreach($required as $field => $label) {
if (!$_POST[$field]) {
$err .= "Please enter your $label<br>";

}

}
if ($err) {
echo ($err);

}
else {

$body = "To be added to the postal mailing list\n\n";

foreach($_POST as $field => $value) {

$body .= sprintf("%s = %s\n", $field, $value);
}
mail("postalmailinglist@dancesounds.co.uk", "Add to Mailing List", $body,
'From: "Dancesounds Mailing List" <postalmailinglist@dancesounds.co.uk>');

echo "Thank you for joining our mailing list";
}


?>
<br>
<b>Mailing List</b>
<br /><br />
To <b>JOIN</b> our postal mailing list please enter your name and school/home address here:
<br /><br />
<form action="mailinglist.php" method="post">
<table border="0" cellpadding="4" cellspacing="4" summary="feedback form">
<tr><td>Please fill in the fields: [* required]</td></tr>
<tr><td>*Name:</td><td><input type="text" name="name" size="25" VALUE="<?php echo $_POST["name"];?>" /></td></tr>
<tr><td>*Postal address:</td><td><input type="text" name="address1" size="25" VALUE="<?php echo $_POST["address1"];?>" /></td></tr>
<tr><td></td><td><input type="text" name="address2" size="25" VALUE="<?php echo $_POST["address2"];?>"/></td></tr>
<tr><td>*Town/city:</td><td><input type="text" name="town" size="25" VALUE="<?php echo $_POST["town"];?>" /></td></tr>
<tr><td>County:</td><td><input type="text" name="county" size="25" VALUE="<?php echo $_POST["county"];?>"/></td></tr>
<tr><td>*Postcode:</td><td><input type="text" name="postcode" size="25" VALUE="<?php echo $_POST["postcode"];?>" /></td></tr>
<tr><td>Country:</td><td><input type="text" name="country" value="UK" size="25" VALUE="<?php echo $_POST["country"];?>"/></td></tr>
<tr><td>Please state which age/level/style<br /> of dance you teach/study:</td><td><input type="text" name="category" size="25" VALUE="<?php echo $_POST["category"];?>"/></td></tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Submit" /><br /><br /><br />
</td>
</tr>
</table>
</form>


Ideally I'd quite like the alerts to appear in the form, maybe in red and bold or something. All help welcome.

[i don't know how to add code in a seperate box like a lot of people on here do with code. Can anyone tell me how to do that in future please?]

Many thanks

Fumigator
12-01-2008, 11:50 PM
Here's your answer to one of your questions:

http://www.codingforums.com/showthread.php?t=68462

sonofjack
12-02-2008, 09:00 AM
Thanks for that, I will try and remember that. I assume its the same for html too?

Hopefully someone out there can help with the first question :)

Thanks

Fumigator
12-02-2008, 03:19 PM
Yeah go ahead and wrap HTML and PHP in those tags... you can also use [ code ] [ /code ] but that doesn't color-code the syntax.

Anyways as to your question, at the beginning of your script you need to detect if you're coming in fresh, or if the user has submitted the form. You can do this by checking the $_POST array. The $_POST array will only be set if the form has been submitted. If you're curious as to what the $_POST array looks like, you can add



echo "<pre>".print_r($_POST,true)."</pre>";


to your script which will echo the contents of the array in a format you can read. You'll see an entry in the array called "Submit" probably (though you really do need to add a name="submitBtn" to your form's submit button), which is what you can check to see if the form's been submitted or not.



if (isset($_POST['Submit']) && $_POST['Submit'] == "Submit") {
//Do your form processing here!
} else {
//Bypass all form processing-- the form has not been submitted yet!
}

sonofjack
12-02-2008, 05:37 PM
Thanks for your help.
I tried it but it didn't seem to work. It got rid of the alerts but then I couldn't submit the form.
Please could you tell me where the code is meant to go within my script?
Or if you could show me that would be great.

Lets have a go at this..



$required = array("name" => "Name",
"address1" => "Address",
"town" => "Town/City",
"postcode" => "Postcode");

foreach($required as $field => $label) {
if (!$_POST[$field]) {
$err .= "Please enter your $label<br>";
}
}
if ($err) {
echo ($err);
}
else {
$body = "To be added to the postal mailing list\n\n";
foreach($_POST as $field => $value) {
$body .= sprintf("%s = %s\n", $field, $value);
}
mail("postalmailinglist@dancesounds.co.uk", "Join Mailing List", $body,
'From: "Dancesounds Mailing List" <postalmailinglist@dancesounds.co.uk>');

echo "Thank you for joining our mailing list";
}

[html]

<br>
<b>Mailing List</b>
<br /><br />
To <b>JOIN</b> our postal mailing list please enter your name and school/home address here:
<br /><br />
<form action="mailinglist.php" method="post">
<table border="0" cellpadding="4" cellspacing="4" summary="feedback form">
<tr><td>Please fill in the fields: [* required]</td></tr>
<tr><td>*Name:</td><td><input type="text" name="name" size="25" VALUE="<?php echo $_POST["name"];?>" /></td></tr>
<tr><td>*Postal address:</td><td><input type="text" name="address1" size="25" VALUE="<?php echo $_POST["address1"];?>" /></td></tr>
<tr><td></td><td><input type="text" name="address2" size="25" VALUE="<?php echo $_POST["address2"];?>"/></td></tr>
<tr><td>*Town/city:</td><td><input type="text" name="town" size="25" VALUE="<?php echo $_POST["town"];?>" /></td></tr>
<tr><td>County:</td><td><input type="text" name="county" size="25" VALUE="<?php echo $_POST["county"];?>"/></td></tr>
<tr><td>*Postcode:</td><td><input type="text" name="postcode" size="25" VALUE="<?php echo $_POST["postcode"];?>" /></td></tr>
<tr><td>Country:</td><td><input type="text" name="country" value="UK" size="25" VALUE="<?php echo $_POST["country"];?>"/></td></tr>
<tr><td>Please state which age/level/style<br /> of dance you teach/study:</td><td><input type="text" name="category" size="25" VALUE="<?php echo $_POST["category"];?>"/></td></tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="submit" value="Submit" /><br /><br /><br />
</td>
</tr>
</table>
</form>

Existing subscribers: <br /><br />
<b>
<a href="updatemailinglist.php">UPDATE your details here</a><br /><br />
<a href="removemailinglist.php">REMOVE your details here</a>
</b>

</html]


can't seem to do the same with the html code. oh well

Fumigator
12-02-2008, 08:08 PM
Word of caution, don't name your submit button "submit"... it tends to mess with IE for some reason. Name it something like "submitBtn". Yeah it's dumb...

So assuming your submit button is now named submitBtn, add this to the top of your script:



if (isset($_POST['submitBtn']) && $_POST['submitBtn'] == "submitBtn") {
//Do your form processing here!
} else {
//Bypass all form processing-- the form has not been submitted yet!
}


Inside the "if" statement, where I have the comment "//Do your form processing here!", put all that PHP code you listed in post #5. You only want to run through that code when the form has been submitted, which is what the "if" statement asks: Has the form been submitted?

p.s. there is no [HTML] tag... you just use the [ PHP ] tags for html.

sonofjack
12-02-2008, 08:27 PM
I'm sure I'm nearly there. Its not quite working though.

The full code for the page is now...



<?php include("header.php");?>
<?php
if (isset($_POST['submitBtn']) && $_POST['submitBtn'] == "submitBtn") {

$required = array("name" => "Name",
"address1" => "Address",
"town" => "Town/City",
"postcode" => "Postcode");

foreach($required as $field => $label) {
if (!$_POST[$field]) {
$err .= "Please enter your $label<br>";

}

}
if ($err) {
echo ($err);

}
else {

$body = "To be added to the postal mailing list\n\n";

foreach($_POST as $field => $value) {

$body .= sprintf("%s = %s\n", $field, $value);
}
mail("postalmailinglist@dancesounds.co.uk", "Join Mailing List", $body,
'From: "Dancesounds Mailing List" <postalmailinglist@dancesounds.co.uk>');

echo "Thank you for joining our mailing list";
}




} else {
//Bypass all form processing-- the form has not been submitted yet!
}
?>




<br>
<b>Mailing List</b>
<br /><br />
To <b>JOIN</b> our postal mailing list please enter your name and school/home address here:
<br /><br />
<form action="mailinglist.php" method="post">
<table border="0" cellpadding="4" cellspacing="4" summary="feedback form">
<tr><td>Please fill in the fields: [* required]</td></tr>
<tr><td>*Name:</td><td><input type="text" name="name" size="25" VALUE="<?php echo $_POST["name"];?>" /></td></tr>
<tr><td>*Postal address:</td><td><input type="text" name="address1" size="25" VALUE="<?php echo $_POST["address1"];?>" /></td></tr>
<tr><td></td><td><input type="text" name="address2" size="25" VALUE="<?php echo $_POST["address2"];?>"/></td></tr>
<tr><td>*Town/city:</td><td><input type="text" name="town" size="25" VALUE="<?php echo $_POST["town"];?>" /></td></tr>
<tr><td>County:</td><td><input type="text" name="county" size="25" VALUE="<?php echo $_POST["county"];?>"/></td></tr>
<tr><td>*Postcode:</td><td><input type="text" name="postcode" size="25" VALUE="<?php echo $_POST["postcode"];?>" /></td></tr>
<tr><td>Country:</td><td><input type="text" name="country" value="UK" size="25" VALUE="<?php echo $_POST["country"];?>"/></td></tr>
<tr><td>Please state which age/level/style<br /> of dance you teach/study:</td><td><input type="text" name="category" size="25" VALUE="<?php echo $_POST["category"];?>"/></td></tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="submitBtn" value="Submit" /><br /><br /><br />
</td>
</tr>
</table>
</form>

Existing subscribers: <br /><br />
<b>
<a href="updatemailinglist.php">UPDATE your details here</a><br /><br />
<a href="removemailinglist.php">REMOVE your details here</a>
</b>


<?php include("footer.php");?>


But when you enter something on the form and submit it doesn't do anything. No warnings or anything.
Have I understood this correctly?

Thanks

Fumigator
12-02-2008, 08:34 PM
Little bug:



if (isset($_POST['submitBtn']) && $_POST['submitBtn'] == "submitBtn") {


Should be



if (isset($_POST['submitBtn']) && $_POST['submitBtn'] == "Submit") {


Gotta match up the value of $_POST['submitBtn'] with the submit button's "value" argument. That should do it.

To troubleshoot stuff like this you can add echos to the code to see if the logic is proceeding as you'd expect. For example you could add a line of code echo "passed the POST check!<br>"; right after the "if" statement to see if that "if" statement is doing what you expect.

sonofjack
12-02-2008, 10:22 PM
Thankyou so much. That works great.
Much appreciated.

I should leave it there but there's one last thing.
Do you know how to have the alerts coming in by the actual fields instead of the top?
Is it easy to do?

That'll be it, I promise

Cheers

Fumigator
12-02-2008, 10:31 PM
On a scale of 1 to 10 it's a 2. And there are a hundred different ways to handle it. The simplest is probably to embed a PHP "if" statement in the HTML code that checks the form field and displays an extra <div> containing the error if it fails validation. It's kludgy but it's a place to start.



<tr>
<td>*Name:</td>
<td>
<?PHP
if (isset($_POST['submitBtn']) && !empty($_POST['name'])) {
echo "<div class=\"errordiv\">Please enter a name.</div>";
}
?>
<input type="text" name="name" size="25" VALUE="<?php echo $_POST["name"];?>" />
</td>
</tr>

sonofjack
12-02-2008, 10:56 PM
I'll give it a go and see where it gets me.

Once again, many thanks for your help.

Best wishes



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum