Jump to content

Php help?? Making fields required


DonelleJenae

Recommended Posts

I have a site I am building for a client and have code that I am using to make an emaiil field required.

 

Here is the code

<script type="text/javascript">

function validate_email(field,alerttxt)

{

with (field)

{

apos=value.indexOf("@")

dotpos=value.lastIndexOf(".")

if (apos<1||dotpos-apos<2)

  {alert(alerttxt);return false}

else {return true}

}

}function validate_form(thisform)

{

with (thisform)

{

if (validate_email(email,"Not a valid e-mail address!")==false)

  {email.focus();return false}

}

}

</script>

 

My variable in my php script is called email. The php part is working, it is a form mailer scipt. The only part that isn't working is the part where it says that the email is required. Do I also have to add something to the php to make it work. I have tried everything and it's not working. 

 

Help if you can, please

Link to comment
https://forums.phpfreaks.com/topic/96059-php-help-making-fields-required/
Share on other sites

Im new to php.

 

Here is my script for my form mailer

 

<?php

$property = $_POST['property'];

$cities = implode(', ', $_POST['cities']);

$other = $_POST['other'];

$min = $_POST['min'];

$max = $_POST['max'];

$feet = $_POST['feet'];

$type = $_POST['type'];

$rooms = $_POST['rooms'];

$bath = $_POST['bath'];

$comments = $_POST['comments'];

$email = $_POST['email'];

 

header( "Location: http://www.findourpad.com/thanks.htm" );

 

$to = "[email protected]";

$subject = ";Custom Home Search Results";

 

$eol="\r\n";

 

$headers = "From: ". $full_name ." <". $email .">".$eol;

$headers .= "Reply-To: ". $full_name." <". $email .">".$eol;

 

$message .= ";Property Type: ". $property . "\n";

$message .= ";Cities: ". $cities."\n";

$message .= ";Other Locations: ". $other . "\n";

$message .= ";Minimum Price: ". $min . "\n";

$message .= ";Maximum Price: ". $max . "\n";

$message .= ";Square Feet: ". $feet . "\n";

$message .= ";Home Type: ". $type . "\n";

$message .= ";Minimum Bedrooms: ". $rooms . "\n";

$message .= ";Minimum Bathrooms: ". $bath . "\n";

$message .= ";Additional Comments: ". $comments . "\n";

$message .= ";Email Address: ". $email . "\n";

 

foreach($_POST as $k => $v) {

        if ($v == '') continue; // skip empty fields

 

        if($htmlFormat) {

                $v = str_replace("\n","<br>\n",$v);

        }

        if (is_array($v)) {

                for ($z=0;$z<count($v);$z++) {

                        if($i) {

                                $msg .= "$bl0$k:$ld$v[$z]$el";

                        } else {

                                $msg .= "$bl1$k:$ld$v[$z]$el";

                        }

                        $i = !$i;

                }

        } else {

                if($i) {

                        $msg .= "$bl0$k:$ld$v$el";

                } else {

                        $msg .= "$bl1$k:$ld$v$el";

                }

                $i = !$i;

        }

}

 

if(mail("$to", "$subject", "$message", "$headers")){

 

echo "Mail Was Sent, Check Your Junk or Inbox.";

 

}else{

 

echo "There was an error delivering your message, please try again";

 

 

}

 

?>

 

Where and what would I put in the code to make the email address required. I am very new to php and this is my first script I ever wrote.

here is a javascript function that i use to validate and force an email address - you could do the same for any type of field. The name and id of the form element is 'email'.

The name of the form is form1. the last thing the function does is make a request to the php form handler thru the http ajax object created earlier on the page...not shown here.

hope this helps. i'm sure there are improvements to be made...

 

function validate_email(  )

{

  var  r = document.form1.email.value;

  var at = r.indexOf('@',0);

  var dot = r.lastIndexOf(".");

  var a_space = r.indexOf(" ");

  var len = r.length -1;

  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

 

  var myemail= document.getElementById("email");

 

    if ( a_space != -1  ) {

  alert ( "Please enter good email address, thanks! ( space)" );

  setTimeout(function(){myemail.focus()}, 10);

  //document.getElementById("email").focus(); 

  return false;

  }

 

  if ( at == -1  ) {

  alert ( "Please enter good email address, thanks! ( no @ sign)" );

  setTimeout(function(){myemail.focus()}, 10);

  return false;

  }

  if ( dot == -1  ) {

  alert ( "Please enter good email address, thanks! ( no dot)" );

  setTimeout(function(){myemail.focus()}, 10);

  return false;

  }

 

  if (r.length < 5){

  alert ( "Please enter good email address, thanks! (too short)" );

  setTimeout(function(){myemail.focus()}, 10);

  return false;

}

 

//alert (" dot is " + dot + " len is " + len );

  if ( dot == len ) {

  alert ( "Please enter good email address, thanks! (nothing after dot)" );

  setTimeout(function(){myemail.focus()}, 10);

  return false;

  }

  if ( (len - dot ) <=2 ) {

  alert ( "Please enter good email address, thanks! (hardly anything after dot)" );

  setTimeout(function(){myemail.focus()}, 10);

  return false;

 

 

  }

 

      if (!filter.test®) {

  alert('Required! correct email address');

setTimeout(function(){myemail.focus()}, 10);

return false;

}

 

var el = document.getElementById('general');

//el.style.backgroundColor = 'red';

el.style.display="block";

el = document.getElementById('email');

        sendRequestTextPost(el.name,el.value,0,'GENERAL');

 

}

// -->

</script>

 

 

 

yes, it is in an .html file.

the first part of the html file creates the ajax object and names the php handler of the form.

the form is introduced with setting the focus for the first element - the email element.

like this:

<body onload="form1.email.focus();">

<h3> Your information is important to us and privileged information. It  will be used for contacting you only.</h3>

<form name="form1" id="form1" action="<? $_SERVER[’PHP_SELF’] ?>"  method="post">

 

then the form has the input element like this:

   

  <table id="generaltable" bgcolor="#FF6633" width="93%" border="2" cellspacing="5">

    <tr>

      <td width="8%">*E-mail</td>

      <td width="23%">

      <input name="email" id="email" onblur="validate_email()" type="text"  size="25"

        /> 

      </td>

 

i reset the focus  back to the email element if the email is invalid. I use onblur because i want to capture the event when the cursor leaves the field and before anything else is done by the user.

 

in the html object in the first part of the page ( not shown here) there is this ajax section:

<script type="text/javascript">

<!--

function sendRequestTextPost(name, value,cf,room) {

 

 

 

var rnd = Math.random();

//if ( name == 'email' )

    //    alert (" name is \n" + name  + " value is:\n " + value );

 

try{

    http.open('POST',  'handle_form.php');

    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    http.onreadystatechange = handleResponseText;

http.send('name='+name+'&value='+value+'&cf='+cf+'&room='+room+'&rnd='+rnd);

 

}

catch(e){}

finally{}

}

// -->

</script>

 

the 'handle_form.php' program is a separate php program that analyzes the fields after validation and stores the data and makes computations based on it...

I suggest using this method as it is not hard to program, once you get past the initial setting up of

the infrastructure...the php program receives a lot of information for each form element, in this case.

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.