Hello DANIWEB!

Haven't posted before... but find this site a good info resource... but I'm pulling my hair out with my php register code - don't know what I'm doing wrong... as the code i've written doesn't send on the site i'm working on...


Here's the code

<?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','university','course','message');
	$required = array ('name','email','university','course','message');	 
	
	$your_email = "miguel@test.co.uk";
	$email_subject = "Web Registration: ".$_POST['subject'];
	$email_content = "New Registration:\n";
	
	foreach($values as $key => $value){
	  if(in_array($value,$required)){
		if ($key != 'subject' && $key != 'company') {
		  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!';
	}
}
?>

I keep getting the message "PLEASE FILL IN REQUIRED FIELDS" But all the five fields have been filled in correctly.

When I amend the line

$values = array ('name','email','university','course','message');
	$required = array ('name','email','university','course','message');

to be

$values = array ('name','email','message');
	$required = array ('name','email','message');

It sends fine... but I'm missing the content from the 'University' and 'Course' fields....


Can anyone advise what the devil I'm doing wrong....


Many thanks

Miguel

This code is wrong:

if ($key != 'subject' && $key != 'company') {
  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}

because $key will be an integer (so it will always evaluate to true), but that wouldn't explain your problem. Do you perhaps have a typo in your form ? It would be helpful if you could post that html too.

This code is wrong:

if ($key != 'subject' && $key != 'company') {
  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}

because $key will be an integer (so it will always evaluate to true), but that wouldn't explain your problem. Do you perhaps have a typo in your form ? It would be helpful if you could post that html too.

Hi Pritaeas,


Thanks for replying to my post...


This is the code from the register.html that goes with the register.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Student Saver | Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<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(),
			company: $('#university').val(),
			subject: $('#course').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>
<link rel="icon" href="http://www.thestudentsaver.co.uk/favicon.ico" type="image/x-icon" /> 
<link rel="shortcut icon" href="http://www.thestudentsaver.co.uk/favicon.ico" type="image/x-icon" />
<body>
<div class="main">
  <div class="header">
    <div class="logo"><img src="images/tss_logo.png" width="500" height="85" alt="logo" longdesc="images/tss_logo.png" /></div>
    </div>
    <div class="menu">
      <ul>
        <li><a href="index2.html"><span>Home</span></a></li>
        <li><a href="offers.html"><span>Offers</span></a></li>
        <li><a href="your.html"><span>Your Uni</span></a></li>
        <li><a href="about.html"><span>About Us</span></a></li>
        <li><a href="register.html"><span>Register</span></a></li>
      </ul>
    </div>
    <div class="clr"></div>
  </div>
  <div class="slider_bg">
    <div class="slider_t">
      <div class="slider_b small">
        <div class="title_text">
          <p>&nbsp;</p>
          <h2>Register for our monthly offers</h2>
        </div>
      </div>
    </div>
  </div>
  <div class="body">
    <div class="left">
      <div class="box no_margin">
        <div class="box_t">
          <div class="box_b">
            <h2>Benefits of registering</h2>
            <p class="spec">Feel free to contact me or please fill up below in the following details and I will be in touch shortly.</p>
            <p><strong>Address:</strong> address info here<br />
              <strong>Telephone:</strong> telephone number here<br />
              <strong>Fax:</strong> fax number here<br />
            </p>
          </div>
        </div>
      </div>
      <div class="clr"></div>
    </div>
    <div class="big_center no_margin">
      <h2>Register your details</h2>
      <p class="spec">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eget bibendum tellus. Nunc vel imperdiet tellus. Mauris ornare aliquam urna, accumsan bibendum eros auctor ac.</p>
      <p>Suspendisse adipiscing rhoncus massa, sit amet sollicitudin quam vulputate non. In non turpis nisl. Curabitur purus mi, pharetra vitae viverra et, mattis sit amet nunc. <br />
      Quisque enim ipsum, convallis sit amet molestie in, placerat vel urna. Praesent congue auctor elit, nec pretium ipsum volutpat vitae. Vivamus eget ipsum sit amet ipsum tincidunt fermentum.    </p>
      <form action="register.php" method="post" id="contactform">
            <ol>
              <li>
                <label for="name">First Name*</label>
                <input id="name" name="name" class="text" />
              </li>
              <li>
                <label for="email">Your email*</label>
                <input id="email" name="email" class="text" />
              </li>
              <li>
                <label for="university">University*</label>
                <input id="university" name="university" class="text" />
              </li>
              <li>
                <label for="course">Course*</label>
                <input id="course" name="course" class="text" />
              </li>
              <li>
                <label for="message">Message*</label>
                <textarea id="message" name="message" rows="6" cols="50"></textarea>
              </li>
              <li class="buttons">
                <input name="imageField" type="image" id="imageField" src="images/send.gif" align="left" />
              </li>
          </ol>
        </form>
    </div>
    <div class="clr"></div>
  </div>
</div>
<div class="footer">
  <div class="FBG">
    <div class="footer_resize">
      <div class="clr"></div>
    </div>
  </div>
  <div class="FBG">
    <div class="footer_resize">
      <p class="leftt">© Copyright thestudentsaver.co.uk. All Rights Reserved <br />
        <a href="index2.html">Home</a> | <a href="contact.html">Contact</a></p>
      <p class="rightt"><a href="http://www.typografico.co.uk"> Design by Typo Grafico</a></p>
      <div class="clr"></div>
    </div>
  </div>
</div>
</body>
</html>

I've checked through my code and can't find anything that screams 'its wrong' but then I'm not a total coder... still learning a lot here...

ANy help/advice is greatly appreciated....

Miguel

Try this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Student Saver | Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
// <![CDATA[
$(function(){
	$("#imageField").click(function(){
	var n = $("#name").val();
	var e = $('#email').val();
	var u = $("#university").val();
	var c = $("#course").val();
	var m = $("#message").val();
	var dataString = 'name=' + n +'&email=' + e + '&university=' + u + '&course=' + c + '&message=' + m;
	if(n=="" || e == "" || u=="" || c == "" || m == ""){
		alert("Fill out All fields with ( * ) Sign..!!");
	}else{
		$.ajax({
		type: "POST",
		url: "./register.php",
		data: dataString,
		cache: false,
		success: function(html){
		 alert("Registration Completed");
			
		}
					});
	}return false;
	});
});

</script>
</head>
<link rel="icon" href="http://www.thestudentsaver.co.uk/favicon.ico" type="image/x-icon" /> 
<link rel="shortcut icon" href="http://www.thestudentsaver.co.uk/favicon.ico" type="image/x-icon" />
<body>
<div class="main">
  <div class="header">
    <div class="logo"><img src="images/tss_logo.png" width="500" height="85" alt="logo" longdesc="images/tss_logo.png" /></div>
    </div>
    <div class="menu">
      <ul>
        <li><a href="index2.html"><span>Home</span></a></li>
        <li><a href="offers.html"><span>Offers</span></a></li>
        <li><a href="your.html"><span>Your Uni</span></a></li>
        <li><a href="about.html"><span>About Us</span></a></li>
        <li><a href="register.html"><span>Register</span></a></li>
      </ul>
    </div>
    <div class="clr"></div>
  </div>
  <div class="slider_bg">
    <div class="slider_t">
      <div class="slider_b small">
        <div class="title_text">
          <p>&nbsp;</p>
          <h2>Register for our monthly offers</h2>
        </div>
      </div>
    </div>
  </div>
  <div class="body">
    <div class="left">
      <div class="box no_margin">
        <div class="box_t">
          <div class="box_b">
            <h2>Benefits of registering</h2>
            <p class="spec">Feel free to contact me or please fill up below in the following details and I will be in touch shortly.</p>
            <p><strong>Address:</strong> address info here<br />
              <strong>Telephone:</strong> telephone number here<br />
              <strong>Fax:</strong> fax number here<br />
            </p>
          </div>
        </div>
      </div>
      <div class="clr"></div>
    </div>
    <div class="big_center no_margin">
      <h2>Register your details</h2>
      <p class="spec">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eget bibendum tellus. Nunc vel imperdiet tellus. Mauris ornare aliquam urna, accumsan bibendum eros auctor ac.</p>
      <p>Suspendisse adipiscing rhoncus massa, sit amet sollicitudin quam vulputate non. In non turpis nisl. Curabitur purus mi, pharetra vitae viverra et, mattis sit amet nunc. <br />
      Quisque enim ipsum, convallis sit amet molestie in, placerat vel urna. Praesent congue auctor elit, nec pretium ipsum volutpat vitae. Vivamus eget ipsum sit amet ipsum tincidunt fermentum.    </p>
      <form action="register.php" method="post" id="contactform">
            <ol>
              <li>
                <label for="name">First Name*</label>
                <input id="name" name="name" class="text" />
              </li>
              <li>
                <label for="email">Your email*</label>
                <input id="email" name="email" class="text" />
              </li>
              <li>
                <label for="university">University*</label>
                <input id="university" name="university" class="text" />
              </li>
              <li>
                <label for="course">Course*</label>
                <input id="course" name="course" class="text" />
              </li>
              <li>
                <label for="message">Message*</label>
                <textarea id="message" name="message" rows="6" cols="50"></textarea>
              </li>
              <li class="buttons">
                <input name="imageField" type="image" id="imageField" src="images/send.gif" align="left" />
              </li>
          </ol>
        </form>
    </div>
    <div class="clr"></div>
  </div>
</div>
<div class="footer">
  <div class="FBG">
    <div class="footer_resize">
      <div class="clr"></div>
    </div>
  </div>
  <div class="FBG">
    <div class="footer_resize">
      <p class="leftt">© Copyright thestudentsaver.co.uk. All Rights Reserved <br />
        <a href="index2.html">Home</a> | <a href="contact.html">Contact</a></p>
      <p class="rightt"><a href="http://www.typografico.co.uk"> Design by Typo Grafico</a></p>
      <div class="clr"></div>
    </div>
  </div>
</div>
</body>
</html>

shishtawitch thanks!!! It worked... ok, you'll probably guess that I'm a complete novice with code... quick Q. How do I get the page to return to the site home page ?? after the 'message sent' message has appeared....

Thanks in advance

Ah, nevermind. My apologies.

No. The $key in this case will contain the array index.
PHP assumes

$values = array ('name','email','university','course','message');

to be this:

$values = array (0 => 'name', 1 => 'email', 2 => 'university', 3 => 'course', 4 => 'message');

So $key would be 0 and $value would be 'name' and so on.

commented: Yep, Thats right. I realised it a little late :) +5

you need to add the following code after

alert("Registration Completed");

so add

window.location = 'http://www.yoursite.com';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.