Jump to content

[SOLVED] PHP Validation!


KashMoney

Recommended Posts

Hello,

 

I have some issues with my php validation form.

 

I need help with email field validation, Send the data to my email, and finally when submit have it say on the next page or even same page, Successfully sent.

 

<?
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
    // Each time there?s an error, add an error message to the error array
    // using the field name as the key.
    if ($_POST['firstname']=='')
        $arrErrors['firstname'] = 'Please provide your first name.';
    if ($_POST['lastname']=='')
        $arrErrors['lastname'] = 'Please provide your last name.';
    if ($_POST['email']=='')
        $arrErrors['email'] = 'A valid email address is required.';
    if ($_POST['subject']=='')
        $arrErrors['subject'] = 'Please enter a subject.';
    if ($_POST['message']=='')
        $arrErrors['message'] = 'Please enter content in the message box.';
    if (count($arrErrors) == 0) {
        // If the error array is empty, there were no errors.
        // Insert form processing here.
    } else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror"><p>Please check the following and try again:</p><ol>';
        // Get each error and add it to the error string 
        // as a list item.
        foreach ($arrErrors as $error) {
            $strError .= "<li>$error</li>";
        }
        $strError .= '</ol></div>';
    }
}
?>

 

Form xhtml

 

<fieldset id="emailform">          

<p class="center"> If you want to contact me for anything regarding my site or any jobs that you need to be done. Please fill the form below and I will get back with you within 24 hours or less.</p> 
       
        <?php echo $strError; ?>
        
        <form method="post" action="contact.html">
        <!--
        For every form field, we do the following...
        
        Check to see if there?s an error message for this form field. If there is, 
        add the formerror class to the surrounding paragraph block. The formerror
        class contains the highlighted box.
        -->

        <p<?php if (!empty($arrErrors['firstname'])) echo ' class="formerrorl"'; ?>>
            
            <label for="firstname">* First Name:</label>
            
            <input name="firstname" type="text" id="firstname" size="30" value="<?php echo $_POST['firstname'] ?>">
            
        </p>
        
        <p<?php if (!empty($arrErrors['lastname'])) echo ' class="formerrorl"'; ?>>
            
            <label for="lastname">* Last Name:</label>
            
            <input name="lastname" type="text" id="lastname" size="30" value="<?php echo $_POST['lastname'] ?>">
            
        </p>
        
        <p<?php if (!empty($arrErrors['email'])) echo ' class="formerrorl"'; ?>>
        
            <label for="email">* Email Address:</label>
            
            <input name="email" type="text" id="email" size="30" value="<?php echo $_POST['email'] ?>">
            
        </p>
        
        <p>
        
            <label>Website URL:</label> 
            
            <input type="text" name="web" size="30" id="web" value="http://" />
            
        </p>
        
        <p<?php if (!empty($arrErrors['subject'])) echo ' class="formerrorl"'; ?>>
            
            <label for="subject">* Subject:</label>
            
            <input name="subject" type="text" id="subject" size="30" value="<?php echo $_POST['subject'] ?>">
            
        </p>
        
        <p<?php if (!empty($arrErrors['message'])) echo ' class="formerrorl"'; ?>>
            
            <label for="message">* Message:</label>
            
            <br />
            
            <textarea rows="10" name="message" cols="85" id="message" value="<?php echo $_POST['message'] ?>"></textarea>
            
        </p>
        
        <p>
            <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset" value="Reset">
        </p>
        
        </form>


	<div class="italic">* required</div>	

</fieldset>

 

 

Thanks in advance.

 

KashMoney

Link to comment
Share on other sites

I am not trying to bump up my post but  I did the part where when someone click submit it sents me an email but the  thing is php doesn't do any validation when I leave something blank on the form field.

 

Here is the part that I did;

 


<?php

        if(isset($_POST['submit'])) 
	{
        
            $to = "myemail@name.com"; 
            $subject = "Submission Form";
            $fname_field = $_POST['firstname'];
            $lname_field = $_POST['lastname'];
            $email_field = $_POST['email'];
            $web_field = $_POST['web'];
            $subject_field = $_POST['subject'];
            $message = $_POST['message'];
                     
            $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n";
        
            echo "<center>The message was sent successfully!</center>";
            mail($to, $subject, $body);
            
   	} 

else 
	{
        	echo "<center>Error Try Again!</center>";
        	}
        ?>

 

Any help would be appreciated!

 

Thanks,

 

KashMoney

 

 

Link to comment
Share on other sites

You should do the validation stuff after the button is pressed i.e.

 

<?php

        if(isset($_POST['submit'])) 
	{
// do your validation here 
                  if (error) { display error }
                  else { continue with other code...  }


?>

Link to comment
Share on other sites

n~ link=topic=170799.msg756271#msg756271 date=1197006354]

You should do the validation stuff after the button is pressed i.e.

 

<?php

        if(isset($_POST['submit'])) 
	{
// do your validation here 
                  if (error) { display error }
                  else { continue with other code...  }


?>

 

Not sure what you mean on the code part?

Link to comment
Share on other sites

see this... hope you get the idea

 

<?php
# after user presses submit button.....
if(isset($_POST['submit'])) 

{
        	// check for validation .... 
		if ($_POST['firstname']=='')
        	$arrErrors['firstname'] = 'Please provide your first name.';
   			if ($_POST['lastname']=='')
        	$arrErrors['lastname'] = 'Please provide your last name.';
		if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) 
		$arrErrors['email'] = 'A valid email address is required.';
		if ($_POST['subject']=='')
        	$arrErrors['subject'] = 'Please enter a subject.';
    		if ($_POST['message']=='')
        	$arrErrors['message'] = 'Please enter content in the message box.';
		// till here you did the validation 

		# now check if error is empty or not, if empty do the mail 
		if (count($arrErrors) == 0) // same as your code.... if errors are 0 process mail... 
		{
		$to = "myemail@name.com"; 
            $subject = "Submission Form";
            $fname_field = $_POST['firstname'];
            $lname_field = $_POST['lastname'];
            $email_field = $_POST['email'];
            $web_field = $_POST['web'];
            $subject_field = $_POST['subject'];
            $message = $_POST['message'];
                     
            $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n";
        
            echo "<center>The message was sent successfully!</center>";
            mail($to, $subject, $body);
		}
		else 
		{
		# there are some errors... display it 
		echo "ERRORS OCCURED...."; 
		}
            
} 

?>

Link to comment
Share on other sites

Try this now... php is case sensitive, so Submit and submit is different... you copy this all and see the changes...

<?php
$arrErrors = "";
# after user presses submit button.....
if(isset($_POST['submit'])) 

{
        	// check for validation .... 
		if ($_POST['firstname']=='')
        	$arrErrors= 'Please provide your first name.<BR>';
   			if ($_POST['lastname']=='')
        	$arrErrors.= 'Please provide your last name.<BR>';
		if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) 
		$arrErrors.= 'A valid email address is required.<BR>';
		if ($_POST['subject']=='')
        	$arrErrors.= 'Please enter a subject.<BR>';
    		if ($_POST['message']=='')
        	$arrErrors.= 'Please enter content in the message box.<BR>';
		// till here you did the validation 

		# now check if error is empty or not, if empty do the mail 
		if (empty($arrErrors)) // same as your code.... if errors are 0 process mail... 
		{
		$to = "myemail@name.com"; 
            $subject = "Submission Form";
            $fname_field = $_POST['firstname'];
            $lname_field = $_POST['lastname'];
            $email_field = $_POST['email'];
            $web_field = $_POST['web'];
            $subject_field = $_POST['subject'];
            $message = $_POST['message'];
                     
            $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n";
        
            echo "<center>The message was sent successfully!</center>";
            $sendmail = mail($to, $subject, $body);
		if (!$sendmail) { echo "Mail could not be sent";};
		}
		else 
		{
		# there are some errors... display it 
		echo "ERRORS OCCURED...."; 
		}
            
} 

?>
<fieldset id="emailform">          
<p class="center"> If you want to contact me for anything regarding my site or any jobs that you need to be done. Please fill the form below and I will get back with you within 24 hours or less.</p> 
<div style="color:#FF0000"><?php if (!empty($arrErrors)) {echo $arrErrors; } ?></div>

<form method="post" action="contact.php">
<!--
For every form field, we do the following...

Check to see if there?s an error message for this form field. If there is, 
add the formerror class to the surrounding paragraph block. The formerror
class contains the highlighted box.
-->
<p>
<label for="firstname">* First Name:</label>
<input name="firstname" type="text" id="firstname" size="30" value="<?php if (isset($_POST['firstname'])) { echo $_POST['firstname']; } ?>">
</p>
<p>
<label for="lastname">* Last Name:</label>
<input name="lastname" type="text" id="lastname" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['lastname']; } ?>">
</p>
<p>
<label for="email">* Email Address:</label>
<input name="email" type="text" id="email" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['email']; } ?>">
</p>
<p>
<label>Website URL:</label> 
<input type="text" name="web" size="30" id="web" value="http://" />
</p>
<p>
<label for="subject">* Subject:</label>
<input name="subject" type="text" id="subject" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['subject']; } ?>">
</p>
<p>
<label for="message">* Message:</label>
<br />
<textarea rows="10" name="message" cols="85" id="message" value="<?php if (isset($_POST['lastname'])) {echo $_POST['message']; } ?>"></textarea>
</p>
<p>
<input type="submit" name="submit" value="submit"> <input type="reset" name="Reset" value="Reset">
</p>
</form>
<div class="italic">* required</div>	
</fieldset>

Link to comment
Share on other sites

I see what you are trying to do, but I want it to go to another page with just the text saying sent successfully. When i do not put anything on the form I want it to highlight like a box in red then when you put something in then it goes away on submit. Not to be annoying. Not what I wanted done. I mean the idea of the stuff showing on top of the form of the missing stuff but the highlights are gone and the next page saying mail successfully gone is not in the code that you put out.

 

Please bare with me.

Link to comment
Share on other sites

Please copy / paste this and check.... Did you mean like this.. To redirect to another page with your custom message, make that page and change the name of the file in header ("Location:mailsent.php");

<?php
$arrErrors = array();
$my_css="";
$strError="";
# after user presses submit button.....
if(isset($_POST['submit'])) 

{
        	// check for validation .... 
		if ($_POST['firstname']=='') 
		$arrErrors['firstname']= 'Please provide your first name.<BR>';
		if ($_POST['lastname']=='')
        	$arrErrors['lastname']= 'Please provide your last name.<BR>';
		if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) 
		$arrErrors['email']= 'A valid email address is required.<BR>';
		if ($_POST['subject']=='')
        	$arrErrors['subject']= 'Please enter a subject.<BR>';
    		if ($_POST['message']=='')
        	$arrErrors['message']= 'Please enter content in the message box.<BR>';

		// till here you did the validation 


		$my_css1 = (empty($_POST['firstname'])) ? 'input_err' : 'input_normal';
		$my_css2 = (empty($_POST['lastname'])) ? 'input_err' : 'input_normal';
		$my_css3 = (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) ? 'input_err' : 'input_normal';
		$my_css4 = (empty($_POST['subject'])) ? 'input_err' : 'input_normal';
		$my_css5 = (empty($_POST['message'])) ? 'input_err' : 'input_normal';


		# now check if error is empty or not, if empty do the mail 
		if (count($arrErrors) == 0) // same as your code.... if errors are 0 process mail... 
		{

		$to = "myemail@name.com"; 
            $subject = "Submission Form";
            $fname_field = $_POST['firstname'];
            $lname_field = $_POST['lastname'];
            $email_field = $_POST['email'];
            $web_field = $_POST['web'];
            $subject_field = $_POST['subject'];
            $message = $_POST['message'];
                     
            $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n";
                   
            $sendmail = mail($to, $subject, $body);
		echo "<center>The message was sent successfully!</center>";	

			if (!$sendmail) 
			{ 
			echo "Mail could not be sent";
			}
			else
			{
			header ("Location:mailsent.php");
			exit;
			}
		}
		else 
		{
        	// The error array had something in it. There was an error.
        	// Start adding error text to an error string.

        	$strError = '<div class="formerror"><p>Please check the following and try again:</p><ol>';
        	// Get each error and add it to the error string 
        	// as a list item.
			foreach ($arrErrors as $error) 
			{
			$strError .= "<li>$error</li>";
			}
        	$strError .= '</ol></div>';
    		}


            
} 

?>
<style type="text/css">
<!--
.input_normal {
background-color: #ffffff;
}
.input_err {
background-color: #E49BA5;
border: 2px solid #FF0000;
}
-->
</style>




<fieldset id="emailform">          
<p class="center"> If you want to contact me for anything regarding my site or any jobs that you need to be done. Please fill the form below and I will get back with you within 24 hours or less.</p> 
<div style="color:#FF0000"><?php echo $strError; ?></div>

<form method="post" action="contact.php">
<!--
For every form field, we do the following...

Check to see if there?s an error message for this form field. If there is, 
add the formerror class to the surrounding paragraph block. The formerror
class contains the highlighted box.
-->
<p>
<label for="firstname">* First Name:</label>
<input name="firstname" type="text" id="firstname" size="30" value="<?php if (isset($_POST['firstname'])) { echo $_POST['firstname']; } ?>" class="<?php echo $my_css1; ?>" >
</p>
<p>
<label for="lastname">* Last Name:</label>
<input name="lastname" type="text" id="lastname" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['lastname']; } ?>" class="<?php echo $my_css2; ?>">
</p>
<p>
<label for="email">* Email Address:</label>
<input name="email" type="text" id="email" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['email']; } ?>" class="<?php echo $my_css3; ?>">
</p>
<p>
<label>Website URL:</label> 
<input type="text" name="web" size="30" id="web" value="http://" />
</p>
<p>
<label for="subject">* Subject:</label>
<input name="subject" type="text" id="subject" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['subject']; } ?>" class="<?php echo $my_css4; ?>">
</p>
<p>
<label for="message">* Message:</label>
<br />
<textarea rows="10" name="message" cols="85" id="message" class="<?php echo $my_css5; ?>"><?php if (isset($_POST['lastname'])) {echo $_POST['message']; } ?></textarea>
</p>
<p>
<input type="submit" name="submit" value="submit"> <input type="reset" name="Reset" value="Reset">
</p>
</form>
<div class="italic">* required</div>	
</fieldset>

Link to comment
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.