Jump to content

Recommended Posts

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
	$error.="Invalid email address entered";
	$errors=1;
}
if($errors==1) echo $error;
else{
	$values = array ('name','email','message');
	$required = array('name','email','message');
	 
	$your_email = "jhondoe@yahoo.com";
	$email_subject = "Allstate new message: ".$_POST['subject'];
	$email_content = "new message:\n";
	
	foreach($values as $key => $value){
	  if(in_array($value,$required)){
		if ($key != 'subject' ) {
		  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
		}
		$email_content .= $value.': '.$_POST[$value]."\n";
	  }
	}
	 
	if(@mail($your_email,$email_subject,$email_content)) {
		echo 'Message sent!'; 
	} else {
		echo 'ERROR!';
	}
}
?>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
	$('#contactform').submit(function(){				  
		var action = $(this).attr('action');
		$.post(action, { 
			name: $('#name').val(),
			email: $('#email').val(),
			subject: $('#subject').val(),
			message: $('#message').val()
		},
			function(data){
				$('#contactform #submit').attr('disabled','');
				$('.response').remove();
				$('#contactform').before('<p class="response">'+data+'</p>');
				$('.response').slideDown();
				if(data=='Message sent!') $('#contactform').slideUp();
			}
		); 
		return false;
	});
});
// ]]>
</script>
</head>

<body>
<table width="250"><tr><td>At Allstateprint.com we're all about honesty.
 Don't hold back, we can take it. It's the only way 
we'll get better.</td></tr>

<tr><td><form action="contact.php" method="post" id="contactform">
        <ol>
          <li>
            <label for="name">Name <span class="red">*</span></label>
            <input id="name" name="name" class="text" />
          </li>
          <li>
            <label for="email">Your email <span class="red">*</span></label>
            <input id="email" name="email" class="text" />
          </li>
         
          <li>
            <label for="subject">Subject</label>
            <input id="subject" name="subject" class="text" />
          </li>
          <li>
            <label for="message">Message <span class="red">*</span></label>
            <textarea id="message" name="message" rows="6" cols="50"></textarea>
          </li>
          <li class="buttons">
            <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
            <div class="clr"></div>
          </li>
        </ol>
      </form></td></tr></table>

This script is sending info to my e-mail box, how can I change to have the message field no mandatory, now you have to fill the message box in the form in order for the form to send the message to my email box, here is the form, and here is the PHP file

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/292563-how-to-alter-mandatory-fields-in-a-form/
Share on other sites

you have to validate that the $_POST['message'] field has what you want.  i have the script exit() if there is an empty message value (nothing or zero) but you likely want to handle it better than this.

<?php

if(!$_POST) exit;

$email = $_POST['email'];
$message = $email = $_POST['message'];

if( empty($message) )
{ exit('You need to provide a message!'); }


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','message');
    $required = array('name','email','message');
    
    $your_email = "jhondoe@yahoo.com";
    $email_subject = "Allstate new message: ".$_POST['subject'];
    $email_content = "new message:\n";
    
    foreach($values as $key => $value){
     if(in_array($value,$required)){
        if ($key != 'subject' ) {
         if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
     }
    }
    
    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!';
    } else {
        echo 'ERROR!';
    }
}
?>
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.