Can't validate form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pmoore
Forum Newbie
Posts: 3
Joined: Tue Jun 03, 2008 6:46 pm

Can't validate form

Post by pmoore »

This started out as a tell a friend script to add one friend and I've added the variables $FriendName1, $FriendName2, $FriendEmail1, and $FriendEmail2. I'm close to modifying it to suit my needs (send to up to three friends) but when I submit the form it gives an error:

"- FriendName1 must contain a number"

I'm sure it will happen for FriendName2 as well, and I'm stuck. Here's the entire code:

Code: Select all

 
<?php
 
$FriendName = $_POST['FriendName'];
$FriendName1 = $_POST['FriendName1'];
$FriendName2 = $_POST['FriendName2'];
$FriendEmail = $_POST['FriendEmail'];
$FriendEmail1 = $_POST['FriendEmail1'];
$FriendEmail2 = $_POST['FriendEmail2'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$emailtext = $_POST['emailtext'];
$string = $_SERVER['QUERY_STRING'];
 
?>
<html><head>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
 
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->
</script>
</head>
<body>
<?php
if (!$FriendEmail) {
?>
<br>
<form name="form1" onsubmit="MM_validateForm('Name','','R','Email','','RisEmail','FriendName','','R','FriendEmail','','RisEmail','FriendName1','','val','FriendName2','FriendEmail1','','isEmail','FriendEmail2','','isEmail');return document.MM_returnValue" action="" method="POST">
<div align="center">
<table width="427" border="0" cellspacing="0" cellpadding="3">
  <tr> 
    <td width="69" align="right" valign="top">&nbsp;</td>
    <td width="5"><font face="Verdana" size="2">&nbsp;</font></td>
    <td width="113"><font face="Verdana" size="2">Name: </font> </td>
    <td width="216"> <font face="Verdana" size="2">E-mail:</font></td>
  </tr>
  <tr> 
    <td align="right" valign="top"><font face="Verdana" size="2"><b>You:</b></font></td>
    <td>&nbsp;</td>
    <td> 
      <font face="Verdana"> 
      <input type="text" name="Name" size="15"><font size="2"> </font></font>
    </td>
    <td> 
      <font face="Verdana"> 
      <input type="text" name="Email" size="20"><font size="2"> </font></font>
    </td>
  </tr>
  <tr> 
    <td align="right" valign="top"><font face="Verdana" size="2"><b>Friend:</b></font></td>
    <td>&nbsp;</td>
    <td> 
      <font face="Verdana"> 
      <input type="text" name="FriendName" size="15"><font size="2"> </font>
        </font>
    </td>
    <td> 
      <font face="Verdana"> 
      <input type="text" name="FriendEmail" size="20"><font size="2"> </font>
        </font>
    </td>
  </tr>
  <tr>
    <td align="right" valign="top"><font face="Verdana" size="2"><b>Friend:</b></font></td>
    <td>&nbsp;</td>
    <td>
      <font face="Verdana">
      <input type="text" name="FriendName1" size="15"><font size="2"> </font>
        </font>
    </td>
    <td>
      <font face="Verdana">
      <input type="text" name="FriendEmail1" size="20"><font size="2"> </font>
        </font>
    </td>
  </tr>
  <tr>
    <td align="right" valign="top"><font face="Verdana" size="2"><b>Friend:</b></font></td>
    <td>&nbsp;</td>
    <td>
      <font face="Verdana">
      <input type="text" name="FriendName2" size="15"><font size="2"> </font>
        </font>
    </td>
    <td>
      <font face="Verdana">
      <input type="text" name="FriendEmail2" size="20"><font size="2"> </font>
        </font>
    </td>
  </tr>
  <tr> 
    <td align="right" valign="top"><font face="Verdana" size="2"><b>Message:</b></font></td>
    <td>&nbsp;</td>
    <td colspan="2"> 
      <font face="Verdana"> 
      <textarea name="emailtext" cols="38" rows="6" style="font-family: Trebuchet MS; font-size: 10pt">
I thought you might be interested in this!
Here is the link to the page: <?php echo $string ?>
</textarea><font size="2"> </font></font>
    </td>
  </tr>
  <tr> 
    <td align="right">&nbsp;</td>
    <td><font face="Verdana" size="2">&nbsp; </font> </td>
    <td> 
      <font face="Verdana"> 
      <input type="submit" value="Send" name="Submit"><font size="2"> </font>
        </font>
    </td>
    <td>&nbsp;</td>
  </tr>
</table>
</div>
</form>
<?php
}
else {
if ($FriendEmail) { mail( $FriendEmail, "Message from $Name", "$FriendName,  \n\n".$emailtext ."\n\n$Name", "From: $Email");
if ($FriendEmail1) { mail( $FriendEmail1, "Message from $Name", "$FriendName1,  \n\n".$emailtext ."\n\n$Name", "From: $Email");
if ($FriendEmail2) { mail( $FriendEmail2, "Message from $Name", "$FriendName,  \n\n".$emailtext ."\n\n$Name", "From: $Email");
                    
  }
  }
   }
echo "<center><br><br><font face=Verdana size=2>Thank you <font color=red>$Name</font>. This page has been sent to <font color=red>$FriendName  $FriendName1  $FriendName2</font>. <br><br><a href='javascript&#058;window.close();'>Close this window</a></font><p>";
}
?>
<p>&nbsp;</p>
</body>
</html>
 
I'm pretty sure that this is the line giving me trouble:

Code: Select all

 
<form name="form1" onsubmit="MM_validateForm('Name','','R','Email','','RisEmail','FriendName','','R','FriendEmail','','RisEmail','FriendName1','','val','FriendName2','FriendEmail1','','isEmail','FriendEmail2','','isEmail');return document.MM_returnValue" action="" method="POST">
I'm just not sure what to do from here. I've been at it for a while. Searched for form validating, MM_validateForm, etc... and I'm just lost. I'm new to all this but try to do stuff myself.

I appreciate any help!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can't validate form

Post by califdon »

No, your "error" is not an error message at all, it is coming from the line in the code that sends that message:

Code: Select all

if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
You have probably copied this code from somewhere and then tried to modify it, but you didn't change the logic of the code that validates the data. I wouldn't attempt to rewrite the code that you have, because I don't know what it was intended to do and I don't have the time to make wild guesses.
pmoore
Forum Newbie
Posts: 3
Joined: Tue Jun 03, 2008 6:46 pm

Re: Can't validate form

Post by pmoore »

Thanks for the reply. It's a tell-a-friend script that was a free download. I'm just trying to add two more "friends" to the form so instead of referring one person, you can refer up to three.

I say "error" because after hitting submit, an alertbox says "The following error(s) occured".

Sorry if I wasted your time, I'm just stuck...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can't validate form

Post by califdon »

pmoore wrote:Thanks for the reply. It's a tell-a-friend script that was a free download. I'm just trying to add two more "friends" to the form so instead of referring one person, you can refer up to three.

I say "error" because after hitting submit, an alertbox says "The following error(s) occured".

Sorry if I wasted your time, I'm just stuck...
You didn't waste my time at all. I didn't intend to say that, only that I can't rewrite an entire script for you. As for "errors", that means that the system generates an error. An alert box is merely something that the programmer decides to put into the logic of the script. An error would not generate an alert box.

Your dilemma is a common one. You download a free script (absolutely nothing wrong with that), but then, as an inexperienced programmer, you try to modify it, something that can be treacherous sometimes even for a skilled programmer, with someone else's program. Programs are very picky, you can't just change one thing and expect it to continue working, depending on how it was written and what you want to change. If you happen to find someone with a lot of time on their hands, perhaps they will volunteer to undertake the rewrite. I just don't happen to have time to do it.
pmoore
Forum Newbie
Posts: 3
Joined: Tue Jun 03, 2008 6:46 pm

Re: Can't validate form

Post by pmoore »

You download a free script (absolutely nothing wrong with that), but then, as an inexperienced programmer, you try to modify it, something that can be treacherous sometimes even for a skilled programmer, with someone else's program.
Baptism by fire. I try to learn as much as I can on my own by getting my hands dirty. I wouldn't even think of asking anyone to rewrite a script for me. I guess I just assumed that I was missing something simple that an experienced eye could pick up. Everything works, except this alertbox "error", which I guess would mean that nothing works.

Anyway, I appreciate your response and have a lot of respect for the knowledge that you guys have.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can't validate form

Post by califdon »

pmoore wrote:
You download a free script (absolutely nothing wrong with that), but then, as an inexperienced programmer, you try to modify it, something that can be treacherous sometimes even for a skilled programmer, with someone else's program.
Baptism by fire. I try to learn as much as I can on my own by getting my hands dirty. I wouldn't even think of asking anyone to rewrite a script for me. I guess I just assumed that I was missing something simple that an experienced eye could pick up. Everything works, except this alertbox "error", which I guess would mean that nothing works.

Anyway, I appreciate your response and have a lot of respect for the knowledge that you guys have.
Would you believe, every one of us started with less knowledge about it than you already have! :D I'm biased, as a retired college instructor, but I strongly believe in formal instructional methods. If you want to get up to speed reasonably quickly, I highly recommend taking classes, perhaps at a local college. If that just isn't workable, the next best approach would be to study a good beginner's programming book, maybe something like "PHP for Dummies" (there's probably one, I'm not certain, but don't be put off by the title, they are generally very good). Some people can do well just by studying online tutorials (just Google using search terms like php basic tutorial, or even basic programming tutorial). The point is that programming is like a pyramid: if you don't learn the basic building blocks first, the more complex features will make little or no sense! The least efficient way to learn, in my opinion, is to start with a relatively complex program or script and try to modify it. The reason is that most useful scripts employ all sorts of complex logic and syntax. You may be able to replace the text display or change the color of the background, but anything that involves program logic is likely to do more to confuse you than to enlighten you, simply because there are a large number of other assumptions that you're unaware of.
Post Reply