Two errors everybody knows and hates and I can't get rid of them

Discussion in 'PHP' started by hanzi, Aug 15, 2008.

  1. #1
    Hi:confused:

    My first attempt at PHP and these two errors are driving me up the wall, can somebody please help me out here. I don't know if clearing up the errors will make the rest of the code work but I have these other functions that are supposed to check that only certain characters are entered and make the fields required before sending the email.

    the errors: 1.header missing
    2.headers already sent

    Thank you for any help:

    <?php
    
    if ($_GET['action'] == "send") {
    	if (!$smtp_sock = fsockopen("mail.mentornet.co.za", 25)) {
        		die ("Couldn't open a connection to the mail server! \n");
        	}
    	else {
    	 		$id = check_input($_POST['id'], "Enter your id");
    	 		$name = check_input($_POST['name'], "Enter your name");
    	 		$surname = check_input($_POST['surname'], "Enter your surname");
    	 		$title = check_input($_POST['title']);
    	 		$organisation = check_input($_POST['organisation']);
    	 		$postAdress = check_input($_POST['postal']);
    	 		$contactNumber1 = check_input($_POST['contact1'], "Enter at least one number");
    	 		$contactNumber2 = check_input($_POST['contact2']);
    	 		$email = check_input($_POST['email'], "Enter your email");
    	 		$us = check_input($_POST['us'], "You didn't choose any courses");
    	 		$choose = check_input($_POST['choose']);
    	 		$numberS = check_input($_POST["numberS"]);
    	 		$postal = check_input($_POST["postal"]);
    	 		$code = check_input($_POST["code"], "please enter security code");
    	 		$headers = "comments@mentornet.co.za" . "none" . "none";
    	 		$text = $id."\n".$name."\n".$surname."\n".$title."\n".$organisation."\n".$postAdress."\n".$contactNumber1."\n".$contactNumber2."\n".$email."\n".$us."\n".$choose."\n".$numberS."\n".$postal;
    	 		
    	 		mail("tech@mentornet.co.za", "Course Registration", "$text", "$headers");
            	header('Location:registrationComplete.html');
    	}
    if (strtolower($_POST['code']) != 'reg332') 
    			{
    				die('Wrong access code');
    			}
    }
    if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$id))
    			{
        			die("Please enter numbers only for ID");
    			}
    if (preg_match("/\D/",$contactNumber1))
    			{
        			die("Please enter numbers only for Contact Number");
    			}
    if (preg_match("/\D/",$contactNumber2))
    			{
        			die("Please enter numbers only for Contact Number");
    			}
    if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
    			{
        			die("wrong email");
    			}
    ?>
    <html>
    	<head>
    		<title>Mentornet Online - Register for Courses</title>
    		<link rel="stylesheet" type="text/css" href="files/mstyle.css" />
    	</head>
    	<body>
    	<div id="main">
    		<img src="files/images/header.png" alt="MENTORNET" border="0" id="header" />
    
    		<table border="0" id="quick">
    			<tr>
    				<td><p><a href="index.html">Home</a></p></td>
    				<td><p><a href="contact.html">Contact Us</a></p></td>
    				<td><p><a href="calendar.html">Calendar</a></p></td>
    				<td><p><a href="comments.php">Comments</a></p></td>
    			</tr>
    		</table>
    
    		<table border="0" id="tabs">
    			<tr>
    				<td><p><a href="etd.html">ETD</a></p></td>
    				<td><p><a href="services.html">Training Services</a></p></td>
    				<td><p><a href="conference.html">Conference Facilities</a></p></td>
    				<td><p><a href="community.html">Community</a></p></td>
    				<td><p><a href="bee.html">BEE</a></p></td>
    				<td><p class="on"><a href="registerCourse.html">Register</a></p></td>
    			</tr>
    		</table>
    
    		<img src="files/images/topFin.png" alt="" border="0" id="top" />
    		
    		<div id="body">
    			
    			
    			<center>
    			<br  /><br  />
    			
    			<h1>Registration of Courses</h1>
    			
    			<div id="content">
    				
    				<h2>Register the courses you are interested in here</h2>
    				
    				<h5>Please fill in the form to register for our courses. The fields 
    				marked with a red asterix are required</h5>
    				
    				<form name="registerCourse" action="registerCourse.php?action=send" method="post">
        			<table border="0" class="mtable">
    		    		
    	            	<tr>
    	            		<td>Title:</td>
    	            		<td class="left1"><select name="title">
    											<option>Mr.</option>
    											<option>Mrs.</option>
    											<option>Ms.</option>
    											<option>Dr.</option>
    											<option>Prof.</option>
    											<option>Other</option>
    										</select></td>
    	            	</tr>
    					<tr>
    	                	<td>Name:</td>
    	                	<td class="left">&nbsp;&nbsp;<input type="text" name="name" size="30"><a style="color:#ff0000">*</a></td>
    	            	</tr>
    	            	<tr>
    	                	<td>Surname:</td>
    	                	<td class="left">&nbsp;&nbsp;<input type="text" name="surname" size="30"><a style="color:#ff0000">*</a></td>
    	            	</tr>
    	            	<tr>
    	                	<td>ID/Passport Number:</td>
    	                	<td class="left">&nbsp;&nbsp;<input type="text" name="id" size="12"><a style="color:#ff0000">*</a></td>
    	            	</tr>
    	            	<tr>
    	            		<td>Postal Address:</td>
    	            		<td class="left">&nbsp;&nbsp;<textarea rows="7" cols="30" name="postal"></textarea></td>
    	            	</tr>
    	            	<tr>
    	            		<td>Individual or Group (choose one):</td>
    	            		<td class="left">&nbsp;&nbsp;<input type="radio" name="choose" value="individual"><a style="color:#ff0000">*</a><a>Individual</a>
    	            		<input type="radio" name="choose" value="group"><a style="color:#ff0000">*</a><a>Group</a></td>
    	            	</tr>
    					<tr>
    						<td>If you chose to register as a Group, how many students will be attending?</td>
    						<td class="left1"><select>
    											<option>2</option>
    											<option>3</option>
    											<option>4</option>
    											<option>5</option>
    											<option>6</option>
    											<option>7</option>
    											<option>8</option>
    											<option>9</option>
    											<option>10</option>
    											<option>11</option>
    											<option>12</option>
    											<option>13</option>
    											<option>14</option>
    											<option>15</option>
    											<option>16</option>
    											<option>17</option>
    											<option>18</option>
    											<option>19</option>
    											<option>20</option>
    											<option>21</option>
    											<option>22</option>
    											<option>23</option>
    											<option>24</option>
    											<option>25</option>
    											<option>More than 25</option>
    											</select><name="numberS"><a style="color:#ff0000">*</a></td>
    					</tr>	
    	            	<tr>
    	            		<td>Organisation:</td>
    	            		<td class="left">&nbsp;&nbsp;<input type="text"  name="organisation" size="30"></td>
    	            	</tr>
    					<tr>
    						<td>Contact Number 1:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="contact1" size="10"><a style="color:#ff0000">*</a></td>
    					</tr>
    					<tr>
    						<td>Contact Number 2:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="contact2" size="10"></td>
    					</tr>
    					<tr>
    						<td>Email:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="email" size="30"><a style="color:#ff0000">*</a></td>
    					</tr>
    					<tr>
    						<td>Unit Standard Numbers:</td>
    						<td class="left">&nbsp;&nbsp;<textarea rows="7" cols="30" name="us"></textarea><a style="color:#ff0000">*</a></td>
    					</tr>
    					<tr>
    						<td>Access code:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="code" /><br />
    							<a>Please enter <b>reg332</b> above.</a></td>
    					</tr>				
    	            	<tr>
    						<td align="center"><input type="submit" value="Submit Registration" />
                    		<input type="reset" value="Reset" /></td>
                		</tr>
    				</table>
    				
    				<br />	
    				
    				
    			
    			<br /><br />
    			</div>
    			</center>
    			<br  />
    			
    		</div>
    		
    		<img src="files/images/bottomFin.png" alt="" border="0" id="bottom" />
    		
    		<br />
    		<center>
    		<img src="/cgi-sys/Count.cgi?df=mentorne.dat|display=Counter|ft=2|md=6|frgb=40;120;80|dd=B">
    		</center>
    	</div>
    	</body>
    </html>
    <?php
    
    
    function check_input($data, $problem='')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        if ($problem && strlen($data) == 0)
        {
            die($problem);
        }
        return $data;
    
    }
    
    
    ?>
    PHP:

     
    hanzi, Aug 15, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    For your answer look at the manual from PHP, MAIL!

    www.php.net/mail then you see that your headers are WRONG! ;)

    and please remove the " from ever $variabel that u use, you don't need the " there ;)

    for example

    mail($email, $subject, $content, $headers);

    Enjoy! :)
     
    EricBruggema, Aug 15, 2008 IP
  3. hanzi

    hanzi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the speedy reply.

    Will go check that out now.
     
    hanzi, Aug 15, 2008 IP
  4. hanzi

    hanzi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well I've come to this, I followed the example you gave me as best I could. I don't get any errors but my form doesn't display. It jumps right to the error page I made for when the security code isn't entered. If I put the security code inside the if statement it displays the form but nothing happens when I click the submit button, it just reloads the page

    Any Ideas?

    <?php
    if ($action == "send") {
    	if (!$smtp_sock = fsockopen("mail.mentornet.co.za", 25)) {
        		die ("Couldn't open a connection to the mail server! \n");
        	}
    	else {
    			$to = 'tech@mentornet.co.za';
    			$subject = 'Online Course Registration';
    	 		$id = check_input($_POST['id']);
    	 		$name = check_input($_POST['name']);
    	 		$surname = check_input($_POST['surname']);
    	 		$title = check_input($_POST['title']);
    	 		$organisation = check_input($_POST['organisation']);
    	 		$postAdress = check_input($_POST['postal']);
    	 		$contactNumber1 = check_input($_POST['contact1']);
    	 		$contactNumber2 = check_input($_POST['contact2']);
    	 		$email = check_input($_POST['email']);
    	 		$us = check_input($_POST['us']);
    	 		$choose = check_input($_POST['choose']);
    	 		$numberS = check_input($_POST["numberS"]);
    	 		$postal = check_input($_POST["postal"]);
    	 		$code = check_input($_POST["code"]);
    	 		$message = ($id."\n".$name."\n".$surname."\n".$title."\n".$organisation."\n".$postAdress."\n".$contactNumber1."\n".$contactNumber2."\n".$email."\n".$us."\n".$choose."\n".$numberS."\n".$postal);
    	 		$headers = 'registration@mentornet.co.za' . "none" . "none";
    	 		$headers = ('Location:registrationComplete.html');
    			mail($to, $subject, $message, $headers);
    		}
    }
    if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$id))
    			{
        			die("Please enter numbers only for ID");
    			}
    if (preg_match("/\D/",$contactNumber1))
    			{
        			die("Please enter numbers only for Contact Number");
    			}
    if (preg_match("/\D/",$contactNumber2))
    			{
        			die("Please enter numbers only for Contact Number");
    			}
    if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
    			{
        			die("invalid email address");
    			}
    if (strtolower($_POST['code']) != 'reg332') 
    			{
    				die(include('errorCode.html'));
    			}
    ?>
    <html>
    	<head>
    		<title>Mentornet Online - Register for Courses</title>
    		<link rel="stylesheet" type="text/css" href="files/mstyle.css" />
    	</head>
    	<body>
    	<div id="main">
    		<img src="files/images/header.png" alt="MENTORNET" border="0" id="header" />
    
    		<table border="0" id="quick">
    			<tr>
    				<td><p><a href="index.html">Home</a></p></td>
    				<td><p><a href="contact.html">Contact Us</a></p></td>
    				<td><p><a href="calendar.html">Calendar</a></p></td>
    				<td><p><a href="comments.php">Comments</a></p></td>
    			</tr>
    		</table>
    
    		<table border="0" id="tabs">
    			<tr>
    				<td><p><a href="etd.html">ETD</a></p></td>
    				<td><p><a href="services.html">Training Services</a></p></td>
    				<td><p><a href="conference.html">Conference Facilities</a></p></td>
    				<td><p><a href="community.html">Community</a></p></td>
    				<td><p><a href="bee.html">BEE</a></p></td>
    				<td><p class="on"><a href="registerCourse.html">Register</a></p></td>
    			</tr>
    		</table>
    
    		<img src="files/images/topFin.png" alt="" border="0" id="top" />
    		
    		<div id="body">
    			
    			
    			<center>
    			<br  /><br  />
    			
    			<h1>Registration of Courses</h1>
    			
    			<div id="content">
    				
    				<h2>Register the courses you are interested in here</h2>
    				
    				<h5>Please fill in the form to register for our courses. The fields 
    				marked with a red asterix are required</h5>
    				
    				<form name="Register Course" action="registerCourse.php?action=send" method="post">
        			<table border="0" class="mtable">
    		    		
    	            	<tr>
    	            		<td>Title:</td>
    	            		<td class="left1"><select name="title">
    											<option>Mr.</option>
    											<option>Mrs.</option>
    											<option>Ms.</option>
    											<option>Dr.</option>
    											<option>Prof.</option>
    											<option>Other</option>
    										</select></td>
    	            	</tr>
    					<tr>
    	                	<td>Name:</td>
    	                	<td class="left">&nbsp;&nbsp;<input type="text" name="name" size="30"><a style="color:#ff0000">*</a></td>
    	            	</tr>
    	            	<tr>
    	                	<td>Surname:</td>
    	                	<td class="left">&nbsp;&nbsp;<input type="text" name="surname" size="30"><a style="color:#ff0000">*</a></td>
    	            	</tr>
    	            	<tr>
    	                	<td>ID/Passport Number:</td>
    	                	<td class="left">&nbsp;&nbsp;<input type="text" name="id" size="12"><a style="color:#ff0000">*</a></td>
    	            	</tr>
    	            	<tr>
    	            		<td>Postal Address:</td>
    	            		<td class="left">&nbsp;&nbsp;<textarea rows="7" cols="30" name="postal"></textarea></td>
    	            	</tr>
    	            	<tr>
    	            		<td>Individual or Group (choose one):</td>
    	            		<td class="left">&nbsp;&nbsp;<input type="radio" name="choose" value="individual"><a style="color:#ff0000">*</a><a>Individual</a>
    	            		<input type="radio" name="choose" value="group"><a style="color:#ff0000">*</a><a>Group</a></td>
    	            	</tr>
    					<tr>
    						<td>If you chose to register as a Group, how many students will be attending?</td>
    						<td class="left1"><select>
    											<option>2</option>
    											<option>3</option>
    											<option>4</option>
    											<option>5</option>
    											<option>6</option>
    											<option>7</option>
    											<option>8</option>
    											<option>9</option>
    											<option>10</option>
    											<option>11</option>
    											<option>12</option>
    											<option>13</option>
    											<option>14</option>
    											<option>15</option>
    											<option>16</option>
    											<option>17</option>
    											<option>18</option>
    											<option>19</option>
    											<option>20</option>
    											<option>21</option>
    											<option>22</option>
    											<option>23</option>
    											<option>24</option>
    											<option>25</option>
    											<option>More than 25</option>
    											</select><name="numberS"><a style="color:#ff0000">*</a></td>
    					</tr>	
    	            	<tr>
    	            		<td>Organisation:</td>
    	            		<td class="left">&nbsp;&nbsp;<input type="text"  name="organisation" size="30"></td>
    	            	</tr>
    					<tr>
    						<td>Contact Number 1:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="contact1" size="10"><a style="color:#ff0000">*</a></td>
    					</tr>
    					<tr>
    						<td>Contact Number 2:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="contact2" size="10"></td>
    					</tr>
    					<tr>
    						<td>Email:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="email" size="30"><a style="color:#ff0000">*</a></td>
    					</tr>
    					<tr>
    						<td>Unit Standard Numbers:</td>
    						<td class="left">&nbsp;&nbsp;<textarea rows="7" cols="30" name="us"></textarea><a style="color:#ff0000">*</a></td>
    					</tr>
    					<tr>
    						<td>Access code:</td>
    						<td class="left">&nbsp;&nbsp;<input type="text" name="code" /><br />
    							<a>Please enter <b>reg332</b> above.</a></td>
    					</tr>				
    	            	<tr>
    						<td align="center"><input type="submit" value="Submit Registration" />
                    		<input type="reset" value="Reset" /></td>
                		</tr>
    				</table>
    				
    				<br />	
    				
    				
    			
    			<br /><br />
    			</div>
    			</center>
    			<br  />
    			
    		</div>
    		
    		<img src="files/images/bottomFin.png" alt="" border="0" id="bottom" />
    		
    		<br />
    		<center>
    		<img src="/cgi-sys/Count.cgi?df=mentorne.dat|display=Counter|ft=2|md=6|frgb=40;120;80|dd=B">
    		</center>
    	</div>
    	</body>
    </html>
    PHP:
     
    hanzi, Aug 18, 2008 IP