21, November 2008

Implementing "Check empty fields ver. 1.01" - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: Implementing "Check empty fields ver. 1.01"
« previous next »
Pages: [1] 2 Print

Author Topic: Implementing "Check empty fields ver. 1.01"  (Read 2840 times)
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« on: Aug 08, 2006, 05:15:30 PM »

This is GREAT script and would seem to solve an ongoing problem I have w/ a textbox/checkbox email program for internal club use.

What I don't understand is how to implement this between my CONTACT page (information collection & submit) and my sender page (actually sending the emails.).

Thanks, --Larry
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Aug 08, 2006, 05:30:17 PM »

try this:

Code:
<?php
if (check_empty_fields()) {
  
header("Location: your_mail_script.php");
}


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #2 on: Aug 08, 2006, 11:46:46 PM »

UFF-DAH.  Thanks Olaf, but I'm dense here.

On "submit" the contact form "POSTS" information to the mailer.  All works well, but the email goes out whether or not data boxes are filled.

Session indicators must go berfore the headers, and the fucnction can go about anywhere, but I don't see how check_empty_fields()  is called or knows when to actually call the mailer.

This page (CONTACT.php) is password protected, only available to members of the car club.  It reads in member data from a mySQL file and allows checking memember names for whom to send emails.  I pass this ivormation to SENDER.php which uses the Swift Mailer to send stmp email.  The  (somewhat awkward) code follows:

--Larry

CONTACT.PHP
head>

<meta HTTP-EQUIV="Pragma" content="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="Mon, 04 Dec 1999 21:29:02 GMT">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

</head>

<form method="POST" action="sender.php">

<title>New Page 1</title>
<td> <h1 align="left"> Contact the Club </h1>


<body>


<div><b><bold>
 <label for="name">Your Name: </label></b></bold><br>
 <input type="text" name="name" id="name" size="35">
<font color ="red">
  Message will not transmit without a NAME entry
</div>
</font>

<div>
<br><b><bold>
<label for="subject">Subject: </label></b></bold><br>
 <input type="text" name="subject" id="subject" size ="39">
<font color ="red">
  Message will not transmit without a SUBJECT entry
</div>
</font>

<br><br>
<b>
Select Addressees: <p>

</b>
</div>

   
<?   
   include("dbinfo.inc.php");
   mysql_connect('localhost',$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      $query="SELECT * FROM rosterlist";
   $result=mysql_query($query);
   
   $num=mysql_numrows($result);

   mysql_close();
unset($email1); unset ($name);
$i=0;  //30
   while ($i < $num) {
                    $name=mysql_result($result,$i,"name");
         $memberno=mysql_result($result,$i,"memberno");
         $address1=mysql_result($result,$i,"address1");
         $address2=mysql_result($result,$i,"address2");
         $address3=mysql_result($result,$i,"address3");
         $phone=mysql_result($result,$i,"phone");
         $email1=mysql_result($result,$i,"email1");
         $email2=mysql_result($result,$i,"email2");
         $car1=mysql_result($result,$i,"car1");   //40
         $car2=mysql_result($result,$i,"car2");
         $car3=mysql_result($result,$i,"car3");
         $car4=mysql_result($result,$i,"car4");
         $car5=mysql_result($result,$i,"car5");
   
        
  if ($email1 <>"") {
      $emails = array($email1);            
echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";
            
 
foreach($emails as $email)
{

echo "<input type=\"checkbox\" value=\"{$email}\" name=\"emails[]\">{$name}</input><br /> \n";
}}
++$i;
   }
?>
<bold><b><p>
<label for="message">Message: </bold></b></label><br>
<textarea rows="15" name="message" cols="55" id="message"></textarea><br>

</div>


<input type="submit" value="SUBMIT" name="submit""



</form>

</body>

</html>



SENDER.PHP

<head>

<meta HTTP-EQUIV="Pragma" content="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="Mon, 04 Dec 1999 21:29:02 GMT">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

</head>

<?php

 //check submitted values

 
if (isset($_POST['emails']))
{
$name=$_POST['name'];
$subj=$_POST['subject'];
$mess=$_POST['message'];

 
// check the value
// if( strLen( $name or $subject or $message ) == 0) {


// ?>  <meta http-equiv="refresh" content="4; URL=http://localhost/contact.php"> <?
// exit();
// }

// there is something submitted.. value is OK



include("gmail.inc.php");

require('swiftmailer/Swift.php');
require('swiftmailer/Swift/Connection/SMTP.php');

$mailer = new Swift(new Swift_Connection_SMTP('smtp.gmail.com', SWIFT_SECURE_PORT, SWIFT_TLS));


//If anything goes wrong you can see what happened in the logs
if ($mailer->isConnected()) //Optional
{

   //You can call authenticate() anywhere before calling send()
   if ($mailer->authenticate($username, $password))
   {
   if(isset($_POST['submit']))

$emailadd=$_POST['emails'];

//Sends a simple email
               
      $mailer->send(
         $emailadd,
         $name,
         $subj,
         $mess .' ------ ' .'This message was originated by ' . $name);
         
       echo "Thank you " . $name . ". The email was sent. ";
   
   }
   else echo "Didn't authenticate to server";
   
   //Closes cleanly... works without this but it's not as polite.
   $mailer->close();

?>
  <br><br><br>
 <td align="center"><p><a href="members1.php">CLICK HERE TO PROCEED BACK TO THE MEMBERS PAGE</a></p>
<?
}
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
   Log: <pre>".print_r($mailer->transactions, 1)."</pre>";

?>


Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Aug 09, 2006, 12:40:53 AM »

The problem is that you post the form to ther mailer script, post the form to you contact.php and then the redirect will work.

Actually I use only one file...(most of the time)


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #4 on: Aug 09, 2006, 02:08:07 AM »

You wrote:  ...The problem is that you post the form to ther mailer script, post the form to you contact.php and then the redirect will work....  ??  I don't think you mean I should post back to CONTACT, it is the orginator.

I am posting my contact form to my sender which then calls the Swift mailer.  Are you saying that I should POST the contact form to a MTCHECK file (which contains the 'Check empty fields ver 1.01 script).  It will then either redisplay the incorrect form or call SENDER?

I appreciate your advice!

--Larry
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Aug 09, 2006, 02:11:43 AM »

You wrote:  ...The problem is that you post the form to ther mailer script, post the form to you contact.php and then the redirect will work....  ??  I don't think you mean I should post back to CONTACT, it is the orginator.

I am posting my contact form to my sender which then calls the Swift mailer.  Are you saying that I should POST the contact form to a MTCHECK file (which contains the 'Check empty fields ver 1.01 script).  It will then either redisplay the incorrect form or call SENDER?

I appreciate your advice!

--Larry
No, it can't because the data is not saved...

You have to use this form like before and you have to add the validation code into the mail script page:

if valid process mail code else redirect back to the mail script or showing some error...


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #6 on: Aug 09, 2006, 02:15:33 AM »

OK - I'll see if I can figure out where to imbed it.  Any suggestions you might have would not be considered

intrusive  Tongue

Thanks again.
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #7 on: Aug 09, 2006, 02:35:04 AM »

Well, I'm in trouble already.  I put the script at the VERY top of CONTACT and receive this session warning:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\MBMGCC.org\contact.php:2) in C:\MBMGCC.org\contact.php on line 3

SESSION START is line 2
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #8 on: Aug 09, 2006, 02:37:50 AM »

I called for help too soon.  There was a blank line above the initial <?PHP.

thanks
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #9 on: Aug 09, 2006, 02:54:39 AM »

Olaf, I've included the script at the top of my CONTACT from, but of course, nohting changes if I leave an input blank.

Could you give me a quick explanation of the last few lines of the script & which of these I change for my application?

Thanks!

if (isset($_POST['submit'])) check_empty_fields();
// and the form field like:
// echo <input type=\"text" name=\"_name\"";
// if (isset($_POST['_name'])) echo " value=\"".$_POST['_name']."\"";
// if (!$_SESSION['_name']) echo " class=\"invalid\">";
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #10 on: Aug 09, 2006, 08:58:28 AM »

Olaf, I've included the script at the top of my CONTACT from, but of course, nohting changes if I leave an input blank.

Could you give me a quick explanation of the last few lines of the script & which of these I change for my application?

Thanks!

if (isset($_POST['submit'])) check_empty_fields();
// and the form field like:
// echo <input type=\"text" name=\"_name\"";
// if (isset($_POST['_name'])) echo " value=\"".$_POST['_name']."\"";
// if (!$_SESSION['_name']) echo " class=\"invalid\">";


since you have to post the form to the mail script you validate the values inside you mailscript


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #11 on: Aug 09, 2006, 07:55:12 PM »

Perhaps I am begining to understand........

In the 3  lines, the function is checking the "name" entry, if valid it will post to the mail script. --Yes?

Then I would make equivalent entries for the other entries I am checking such as the senders name, the checkboxes and the message?

If there is a blank entry the function redisplays the page for correction, if all is good the mail script does it's stuff.  --Yes?
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #12 on: Aug 09, 2006, 09:31:13 PM »

and just when I think I'm getting it .....

I removed the  comment line
           // and the form field like:

and removed the comment strokes from the remaining three lines,

but then get a PHP error:  Parse error: syntax error, unexpected '<' in C:\MBMGCC.org\contact.php on line 43

looking at the input statement it appears to be HTML/PHP conflict, but the line is definately within the <?php ?> boundaries

--Larry
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Aug 09, 2006, 10:37:09 PM »

and just when I think I'm getting it .....

I removed the  comment line
           // and the form field like:

and removed the comment strokes from the remaining three lines,

but then get a PHP error:  Parse error: syntax error, unexpected '<' in C:\MBMGCC.org\contact.php on line 43

looking at the input statement it appears to be HTML/PHP conflict, but the line is definately within the <?php ?> boundaries

--Larry


what is code at line 43?


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #14 on: Aug 10, 2006, 12:15:21 AM »

 The error line is:

echo <input type=\"text" name=\"_name\"";

It is the first line of the commented section after   // and the form field like:
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #15 on: Aug 10, 2006, 12:47:41 AM »

the example code is missing some double quotes:

echo "<input type=\"text" name=\"_name\"";

maybe you should start here: http://www.php.net/echo Visit through proxy


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #16 on: Aug 10, 2006, 12:51:02 AM »

Thanks  -- it appears that there is also a   \  missing after "text  before the "
« Last Edit: Aug 10, 2006, 01:06:57 AM by LarryS »
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #17 on: Aug 10, 2006, 01:48:09 AM »

One last question (I hope), Olaf,

I now get this error:
Notice: Undefined variable: _SESSION in C:\MBMGCC.org\contact.php on line 110 class="invalid">

at this code line:
if (!$_SESSION['_name']) echo " class=\"invalid\">";

I haven't changed it from the example.  Using the PHP.net resource you gave me above, it looks OK. 

Except for a  change of the field name to agree w/ my code, the function is as original.
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6435
39434 credits
Members referred : 374


It's time to use PHP5!


« Reply #18 on: Aug 10, 2006, 09:09:07 AM »

you need a session_start() at the top of the page.


Last blog : Just a better Internet portal provided by Google
I love Pokemon
*
Posts: 14
88 credits
Members referred : 0


« Reply #19 on: Aug 10, 2006, 05:51:21 PM »

It is there. 

Here is the code down to the error producing line at the bottom

Code:
<?php 
session_start
(); 

unset(
$_SESSION); 
$req_fields = array("_name"=>"name""field"=>"subject""check"=>"checkbox"); 
$msg "&nbsp;"

function 
check_empty_fields($method "post") { 
global 
$req_fields$msg
$use_method = ($method == "post") ? $_POST $_GET
$errors ""
$count_empty 0
foreach (
$req_fields as $key => $val) { 
$field_name = (array_key_exists($key$req_fields)) ? $req_fields[$key] : $key
if (!
array_key_exists($key$use_method) || empty($use_method[$key])) { 
$_SESSION[$key] = true
$errors .= "|".$field_name
$count_empty++; 
} else { 
$_SESSION[$key] = false


if (
$count_empty == 0) { 
return 
true
} else { 
$msg "The following (required) fields are empty:"
$msg_parts explode("|"ltrim($errors"|")); 
$num_parts count($msg_parts); 
$msg .= "<br><b>"
for (
$i 0$i $num_parts$i++) { 
$msg .= $msg_parts[$i]; 
if (
$i <= $num_parts 2) { 
$msg .= ($i == $num_parts 2) ? " &amp; " ", "


$msg .= "</b>\n"
return 
false


if (isset(
$_POST['submit'])) check_empty_fields(); 
?>


<html><!-- #BeginTemplate "/templates/main.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title>Mount Baker MG Car Club Official Web Site</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">

<table width="700">
  <tr>
    <td width="170" height="123" align="center"><a href="index.html"><img src="images/mbmgcc-logo.gif" width="120" height="123" border="0"></a></td>
    <td width="2">&nbsp;</td>
    <td width="6">&nbsp;</td>
    <td width="2">&nbsp;</td>
    <td> <h1>Mount Baker MG Car Club<br>
        <i><font size="3">The Official Web Site</font></i></h1></td>
  </tr>
  <tr>
    <td width="170" valign="top">&nbsp;</td>
    <td width="2" valign="top">
      <p>&nbsp;</p></td>
    <td width="6" valign="top">&nbsp;</td>
    <td width="2" height="8" valign="top">&nbsp;</td>
    <td width="599" valign="top">&nbsp;</td>
  </tr>
  <tr>
    <td width="170" align="center" valign="top">
      <p><a href="index.html">Home</a></p>
      <table width="100%" border="0">
   </tr>     
      </table>
      <p><br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
      </p>
      <p>&nbsp;</p></td>
    <td width="2" valign="top">&nbsp;</td>
    <td width="6" valign="top" bgcolor="#669900">&nbsp;</td>
    <td width="2" valign="top">&nbsp;</td>
    <td valign="top"><!-- #BeginEditable "Body2" -->
<head>

<meta HTTP-EQUIV="Pragma" content="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="Mon, 04 Dec 1999 21:29:02 GMT">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

</head>

<form method="POST" action="sender.php">

<title>New Page 1</title>
<td> <h1 align="left"> Contact the Club </h1>


<body>


<div><b><bold>
Your Name:<br>

<?
echo "<input type=\"text\" name=\"_name\" size = \"35\"";
if (isset($_POST['_name'])) echo " value=\"".$_POST['_name']."\"";
if (!$_SESSION['_name']) echo " class=\"invalid\">";
?>

« Last Edit: Aug 10, 2006, 05:53:29 PM by Nikolas »
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3585
Tags : php tutorials mysql cookies email Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: Implementing "Check empty fields ver. 1.01"
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 21, 2008, 06:22:56 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 37.714
Total Topics: 7.646
Total Members: 4.390
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: panetolikos

30 Guests, 3 Users online :

20 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.