PDA

View Full Version : Require fields???



jasonhardwick
September 1st, 2006, 03:48 PM
I have a php "join the mailing list" form that I need to make the fields required, How can I do this??? I don’t know that much about php and am trying to grasp these basics so if anyone out there can help me out or point me in the right direction it would be greatly appreciated.

Here is my page Code

<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.style4 {color: #FFFFFF; font-weight: bold; }
-->
</style>
<script 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_valida teForm.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>
<table width="300" height="400" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
<tr>
<td colspan="3">
<img src="images/join_mailing_list_01.jpg" width="300" height="16" alt=""></td>
</tr>
<tr>
<td rowspan="2">
<img src="images/join_mailing_list_02.jpg" width="14" height="370" alt=""></td>
<td>
<img src="images/join_mailing_list_03.jpg" width="271" height="97" alt=""></td>
<td rowspan="2">
<img src="images/join_mailing_list_04.jpg" width="15" height="370" alt=""></td>
</tr>
<tr>
<td height="273" valign="middle" background="images/join_mailing_list_05.jpg"><form id="form1" name="form1" method="post" action="">
<p class="style1"><span class="style4">Name:
</span><br />
<input name="Name" type="text" id="Name" size="32" />
</p>
<p class="style1"><span class="style4">E-Mail:
</span><br />
<input name="Email" type="text" id="Email" value="" size="32" />
</p>
<p class="style1"><span class="style4">Confirm E-Mail:</span><br />
<input name="Confirm_Email" type="text" id="Confirm_Email" size="32" />
</p>
<p>
<span class="style1">
<input name="Submit" type="submit" onclick="MM_validateForm('Name','','R','Email','','RisEmail ','Confirm_Email','','RisEmail');return document.MM_returnValue" value="Submit" />
</span> </p>
</form></td>
</tr>
<tr>
<td colspan="3">
<img src="images/join_mailing_list_06.jpg" width="300" height="14" alt=""></td>
</tr>
</table>
<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Confirm_Email = $_POST['Confirm_Email'];

$link = mysql_connect( '', '', '' ) or die( mysql_error() );
mysql_select_db( '', $link ) or die( mysql_error() );

$result = mysql_query( "INSERT INTO Join_the_Mailing_List (Name, Email, Confirm_Email) VALUES ( '$Name','$Email','$Confirm_Email')" ) or die( mysql_error() );
?>

GrzKax
September 1st, 2006, 03:51 PM
First you might wan't to try using the:

[ PHP] and [ /PHP] tags or the [ HTML] and [ /HTML]

bwh2
September 1st, 2006, 08:52 PM
here's a good place to start:


<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Confirm_Email = $_POST['Confirm_Email'];

/* here are the checks */

if( $Name == '' ||
$Email == '' ||
$Confirm_Email == ''
) {
echo 'All fields are required';
die;
}

/* end checks */

/* if statements require == to test if things are equal. this if doesn't make sense. */
if( $Name = '' ||
$link = mysql_connect( '', '', '' ) or die( mysql_error() );
mysql_select_db( '', $link ) or die( mysql_error() );

$result = mysql_query( "INSERT INTO Join_the_Mailing_List (Name, Email, Confirm_Email) VALUES ( '$Name','$Email','$Confirm_Email')" ) or die( mysql_error() );
?>
as GrzKax mentioned, it's nice to use the
the code tags as well.

@GrzKax: you can use the [noparse] tags to show the [php] tags without using those spaces.

chrisclick
September 1st, 2006, 10:21 PM
and
Cleaning it up for GrzKax