Reputation: 294
I'm breaking my head here. First time toying with PHP. I have a form with several fields that are required. If a user tries to send an empty field, it needs to be caught. If I type a space, the form goes through. Any help will be truly appreciated.
This is the code for one field
if( isset($_POST['first_name']) and !empty($_POST['first_name']) and trim($_POST['first_name']) != '' ) $first_name = filter_var($_POST['first_name'], FILTER_SANITIZE_MAGIC_QUOTES);
else {
$redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList' and header ( "Location: $redirect" );
}
Upvotes: 0
Views: 39
Reputation: 189
Just an advice since you're a beginner on PHP robb, take a look at some framework to work with PHP, like Codeigniter, they'll have a few features that can help you, like a form validation. Another advice, you also have to do this validation before you send the form to the server ok, using Javascript (jQuery will give you a hand).
Regards, and good luck with PHP!!
Upvotes: 0
Reputation: 64657
You are setting $redirect
to what the expression
$redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList' and header ( "Location: $redirect" );
evaluates to. When you do
header ( "Location: $redirect" );
$redirect
has not been set yet.
For example:
php > $x = 'a' && 'a' . $x;
PHP Notice: Undefined variable: x in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
You need to just break it up:
$redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList';
header("Location: $redirect");
or make it evaluate the expression before the and
first:
($redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList') and header ( "Location: $redirect" );
Upvotes: 1