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!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi everyone, I'm new here.
I am trying to make the form fields "First name, last name, state, phone, and email" required.
the page is here
http://www.mankatowebdesign.com/SMS/cen ... egion.html
The PHP Code I have currently my friend helped me so I know the (if) statements need to be fixed. If anyone could explain what I need to change the if's to to fix this it would help.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
peschomd wrote:im a noob i do not understand the links
That's no excuse. If you can read English and know what a function is, then the documentation should be pretty clear. They even give code examples. If you're expecting me to just give you the answer to such a simple question, you're mistaken.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
A simpler example of form validation :
form.php
-----------
[syntax="html"]
<html>
<head></head>
<body>
<form action="handler.php" method="post">
First Name:<br /><input type="text" name="first_name" size="20" /><br />
Email:<br /><input type="text" name="the_email" id="the_email_id" size="20" maxlength="60" /><br />
<input type="submit" name="submit_btn" value="check" />
</form>
</body>
</html>
<?php
$email = (string) $_POST['the_email'];
$firstname = (string) $_POST['first_name'];
if (empty($email))
{
die('You did not enter anything for email. Please go <a href="javascript:history.back(-1);">back</a>.');
}
if (empty($firstname))
{
die('You did not enter anything for first name. Please go <a href="javascript:history.back(-1);">back</a>.');
}
echo "Thank You, form successfully submitted";
?>
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
FYI, you are using variables without any verification that they exist. If you turned on error_reporting, this would cause notices to be thrown by attempting to access undefined variables. This is the reason that we have isset().
<?php
$errmsg = '';
$success = false;
if (isset($_POST['submit'])) { // hidden param sent means form sent
// filter post values to accept only characters we allow
$email = isset($_POST['email']) ? preg_replace('/[^a-zA-Z0-9\@\.\-\_]/', '', $_POST['email']) : '';
$firstname = isset($_POST['first_name']) ? preg_replace('/[^a-zA-Z\-\ ]/', '', $_POST['first_name']) : '';
// validate post values
if (empty($email)) {
$errmsg .= 'Please enter your email. ';
}
if (empty($firstname)) {
$errmsg .= 'Please enter your first name. ';
}
if (! $errmsg) {
$success = true;
}
} else {
$email = '';
$firstname = '';
}
if ($success) {
header('Location: http://www.mydomain.com/register_thankyou.php"); // redirect so user cannot resubmit
} else {
include 'template/register.php';
}
edit: superdezign's fixes
Last edited by Christopher on Wed Aug 15, 2007 10:06 pm, edited 2 times in total.
preg_replace calls should have 3 arguments (parse error, I think) and delimiters. Also, you don't need to escape '@' or the underscore. And in the first name... is that a.. space? I'm not sure.
'Location:' header needs to use a full URL.
Even if the form is submitted, you should still check that $_POST data isset(), since POST data can be altered.
I like the procedural separation of code approach though. Wish I thought of that back in the day. ^_^
arborint wrote:I think you should start quoting my sloppy code and fixing it!
Almost did. That's how I noticed the missing commas in preg_replace.
I almost didn't read it. I told myself not to. And then, I saw it was three files, and I was compelled to read!