Jump to content

Recommended Posts

You can use javascript validation. There are lots of free codes are available

Javascript validation is only good for informing the user quickly and without a round-trip to the server. Users have the option of turning off Javascript, and some browsers may not support it at all. So you can NOT depend on Javascript validation to force any kind of restrictions or ... uhh ... validation, at all.
Link to comment
https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415757
Share on other sites


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>MSM Offer</title>
</head>
<body style="color: rgb(0, 0, 0); background-color: rgb(8, 8, ;"
alink="#000099" link="#000099" vlink="#990099">
<div style="text-align: center;"><img
style="width: 534px; height: 272px;" alt=""
src="http://offer.mirrorshow.com/offerpage3.jpg"><br>
<br>
<form method="post" action="contact.php">Fields marked (*) are required
<p>NN<span style="color: rgb(246, 246, 246);"><span
style="font-weight: bold;">Name</span></span>ame:<br>
<input name="Name" type="text">
</p>
<p>Co    <span style="color: rgb(246, 246, 246);"><span
style="font-weight: bold;">Company</span></span>pany:<br>
<input name="Company" type="text">
</p>
<p>Telep<span style="color: rgb(246, 246, 246);"><span
style="font-weight: bold;">Telephone</span></span>hone:<br>
<input name="Telephone" type="text">
</p>
<p>Em<span style="color: rgb(246, 246, 246);"><span
style="font-weight: bold;">Email</span></span>ail:<br>
<input name="Email" type="text">
</p>
<p><input name="submit" value="Submit" type="submit"></p>
</form>
<p></p>
<a href="http://mirrorshowmanagement.com"><img
style="border: 0px solid ; width: 447px; height: 125px;" alt=""
src="http://offer.mirrorshow.com/logo.jpg"></a></div>
</body>
</html>


<?php
// Website Contact Form Generator
// http://www.tele-pro.co.uk/scripts/contact_form/
// This script is free to use as long as you
// retain the credit link

// get posted data into local variables
$EmailFrom = "offerpage@mirrorshow.com";
$EmailTo = "chris.rice@mirrorshow.com";
$Subject = "Offer Submission";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Email = Trim(stripslashes($_POST['Email']));

// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Link to comment
https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415797
Share on other sites

To make a required input, you need to check if that input is empty.

 

<?php
// get posted data into local variables
$EmailFrom = "offerpage@mirrorshow.com";
$EmailTo = "chris.rice@mirrorshow.com";
$Subject = "Offer Submission";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Email = Trim(stripslashes($_POST['Email']));

 
if(empty($Name)) {
 $message = 'You are required to input a name.';
}
Link to comment
https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415818
Share on other sites

 

To make a required input, you need to check if that input is empty.

 

<?php
// get posted data into local variables
$EmailFrom = "offerpage@mirrorshow.com";
$EmailTo = "chris.rice@mirrorshow.com";
$Subject = "Offer Submission";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Email = Trim(stripslashes($_POST['Email']));

 
if(empty($Name)) {
 $message = 'You are required to input a name.';
}

I entered that portion into the code and it still let me submit with the name field blank.

Link to comment
https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415852
Share on other sites

I entered that portion into the code and it still let me submit with the name field blank.

 

 

Just return false.

 

if(empty($Name)) {
 $message = 'You are required to input a name.';
 return false; 
}

 

How is treated an integer by 0 in the form , as true or false?

Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415853
Share on other sites

This is what I got when I visited the link to the generator:

You are viewing an archived website. Would you like to visit the new updated website?

Now, when it comes to code it is very much a perishable goods, so whenever you see a message like that the best advice is to simply move on. Don't use the code on such a site, as it is bound to be out of date and thus insecure and/or buggy.

 

That said, the steps you can take to make this code serviceable, and to add validation is first and foremost to remove all of those stripslashes.

Then, as jcbones stated, you'll need to check if the input values are empty. Or, if they otherwise does not confirm to the pattern of expected legal input. In other words, check that an e-mail address actually looks like and e-mail address, that a number is actually a number and so forth. A lot of this is done with filter_var and the various "ctype_* ()" functions such as ctype_alnum.

 

If validation fails for one of the elements: Add an error message, I recommend using arrays for this, and then set the validation flag to false. Of course, this assumes you've set it to true by default.

Another way to check for failed validation, is to check whether or not the error array is empty. Then you don't need the validation flag.

Link to comment
https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415855
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.