Prevent form from being submitted unless http present?

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,145
Reaction score
10,489
I have this form on this website where people can input email and link. It has 2 fields, one for email one for link/URL.

The form prevents people from submitting the form if email is not written correctly, but they can submit it if they enter any text in the URL field. I'd like to make the form prevent the form from being submitted unless there's an actual URL submitted (require "http" present for successful submission would do it).

Anyway, here is the code that blocks submission if email is not present:

PHP:
if(!$_POST) exit;

$email = $_POST['Email_Address'];
$error ="";
$errors ="";
 
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if($errors==1) echo $error;
else{
    $values = array ('Full_Name','Email_Address','Your_Link');
    $required = array('Full_Name','Email_Address','Your_Link');

I've had this website for 5+ years, but recently some asshole started submitted 2-4 times per day, with random emails, and with random text in the URL field. Not sure why he persists, but it's been over a month now and it keeps happening and it's getting on my nerves receiving these emails.

Anyway, if anyone can whip up some magic in the code to require "http" present for successful form submission, I'd appreciate it.

Thank you :)
 
Assuming that
PHP:
$url = $_POST['Your_Link'];
is correct.
PHP:
if(!$_POST) exit;
$email = $_POST['Email_Address'];
$url = $_POST['Your_Link'];
$error =""; 
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if (!filter_var($url, FILTER_VALIDATE_URL)) {
    $error.="Invalid URL entered!";
    $errors=1;
}

if($errors==1) echo $error;

else{
    $values = array ('Full_Name','Email_Address','Your_Link');
    $required = array('Full_Name','Email_Address','Your_Link');
 
Last edited:
Assuming that
PHP:
$url = $_POST['Your_Link'];
is correct.
PHP:
if(!$_POST) exit;
$email = $_POST['Email_Address'];
$url = $_POST['Your_Link'];
$error =""; $errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if (!filter_var($url, FILTER_VALIDATE_URL)) {
    $error.="Invalid URL entered!";
    $errors=1;
}

if($errors==1) echo $error;

else{
    $values = array ('Full_Name','Email_Address','Your_Link');
    $required = array('Full_Name','Email_Address','Your_Link');

Killer. That did it.

Thanks a lot man, much appreciated :)
 
Assuming that
PHP:
$url = $_POST['Your_Link'];
is correct.
PHP:
if(!$_POST) exit;
$email = $_POST['Email_Address'];
$url = $_POST['Your_Link'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if (!filter_var($url, FILTER_VALIDATE_URL)) {
    $error.="Invalid URL entered!";
    $errors=1;
}

if($errors==1) echo $error;

else{
    $values = array ('Full_Name','Email_Address','Your_Link');
    $required = array('Full_Name','Email_Address','Your_Link');

Hey man, can i bother you for another one?

The same form. I just want to prevent form submissions if the characters entered are less than 10 characters.

I tried this, but it throws an error no matter how many characters I write:

Code:
$email = $_POST['Email_Address'];
$quantity = $_POST['Your_Message'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if (filter_var($quantity, FILTER_VALIDATE_INT, array("options" => array("min_range"=>1, "max_range"=>10))) === false) {
    $error.="Message too short!";
    $errors=1;
}

It's the "$quantity" bit I'm adding.

Thanks again.
 
Hey man, can i bother you for another one?

The same form. I just want to prevent form submissions if the characters entered are less than 10 characters.

I tried this, but it throws an error no matter how many characters I write:

Code:
$email = $_POST['Email_Address'];
$quantity = $_POST['Your_Message'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if (filter_var($quantity, FILTER_VALIDATE_INT, array("options" => array("min_range"=>1, "max_range"=>10))) === false) {
    $error.="Message too short!";
    $errors=1;
}

It's the "$quantity" bit I'm adding.

Thanks again.
Sure, always ready to help
This should do it

PHP:
$email = $_POST['Email_Address'];
$message = $_POST['Your_Message'];
$quantity = strlen($message);
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if ($quantity < 10) {
    $error.="Message too short!";
    $errors=1;
}
 
Sure, always ready to help
This should do it

PHP:
$email = $_POST['Email_Address'];
$message = $_POST['Your_Message'];
$quantity = strlen($message);
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;
}

if ($quantity < 10) {
    $error.="Message too short!";
    $errors=1;
}

Killer, it works. You're the man, man! Thank you :)
 
@joshgarbinson can I bother you for another one?

The same form, if it's submitted with an invalid email the user is taken to a blank page with the message - "Invalid email address entered!"

Code:

Code:
$email = $_POST['Email_Address'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;

}

I want to edit the message to add a Hyperlink, but it won't accept the HTML. It's like, it accepts this <br>

But it doesn't accept this <a href="#">text</a>

I'm not sure why. I want to make something like this:

Code:
$email = $_POST['Email_Address'];
$error ="";
$errors ="";
 
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered! <br><br> Go <a href="LINK">back</a> and resubmit the form.";
    $errors=1;
}

Any idea how to add HTML/Hyperlink in the error message?

Thanks
 
@joshgarbinson can I bother you for another one?

The same form, if it's submitted with an invalid email the user is taken to a blank page with the message - "Invalid email address entered!"

Code:

Code:
$email = $_POST['Email_Address'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered!";
    $errors=1;

}

I want to edit the message to add a Hyperlink, but it won't accept the HTML. It's like, it accepts this <br>

But it doesn't accept this <a href="#">text</a>

I'm not sure why. I want to make something like this:

Code:
$email = $_POST['Email_Address'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error.="Invalid email address entered! <br><br> Go <a href="LINK">back</a> and resubmit the form.";
    $errors=1;
}

Any idea how to add HTML/Hyperlink in the error message?

Thanks
What do you mean by it doesn't accept it? It just prints it out without parsing it? Since you probably want that "go back and resubmit form" message regardless of the specific error, I suggest you hard code it on the error page template instead of passing it inside of the $error variable. If you do need to pass it inside of the $error variable though, I'm going to need to see how you're putting it on the html page to help you further.
 
What do you mean by it doesn't accept it? It just prints it out without parsing it? Since you probably want that "go back and resubmit form" message regardless of the specific error, I suggest you hard code it on the error page template instead of passing it inside of the $error variable. If you do need to pass it inside of the $error variable though, I'm going to need to see how you're putting it on the html page to help you further.

When I get to the error page I just get a weird error text.

Yeah I should probably hardcode it, though I don't know how.

I should probably revise that entire form to be honest. An unsuccessful submission takes people to a blank page with an error (so outdated), and even a successful submission just takes people to the homepage, and they can't be sure if their submission was successful or not.
 
When I get to the error page I just get a weird error text.

Yeah I should probably hardcode it, though I don't know how.

I should probably revise that entire form to be honest. An unsuccessful submission takes people to a blank page with an error (so outdated), and even a successful submission just takes people to the homepage, and they can't be sure if their submission was successful or not.

Hi

Can you share your html please?
What's the error text you are getting?
It's not good to "hardcode" things in programming, this is considered a bad practice!

If I was you I will try to learn how to properly do form validation / submission with PHP :)
 
Hi

Can you share your html please?
What's the error text you are getting?
It's not good to "hardcode" things in programming, this is considered a bad practice!

If I was you I will try to learn how to properly do form validation / submission with PHP :)
Implementing something at the base when it is used by all instances of that thing is bad practice?
 
Back
Top