elmas156 Posted March 2, 2011 Share Posted March 2, 2011 I have been trying to figure this out for hours and I can't see what's wrong. I'm getting this fatal error: Fatal error: Call to undefined function form() in /home/content/29/6879529/html/contact.php on line 5 Here's my form code: <html> <form name="contact" method="POST" action="contact.php" onsubmit="return validateContactForm()"> <input class="textbox" name="fname" type="text" /> <input type="submit" name="submit" value="Send Message" /> </form> </html> Here's my php code, which is at the top of the same page the form is on: <?php if (isset($_POST['submit'])) { // If the form has been submitted. $fname = form($_POST['fname']); //This is line 5 echo $fname; } ?> Any ideas on what could be causing this error? I've used this format MANY times in the past and I've never had a problem until now. I'm baffled! Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/ Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 Where is the function form() defined? Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181666 Share on other sites More sharing options...
elmas156 Posted March 2, 2011 Author Share Posted March 2, 2011 That's the thing... I'm not using "function form()". The only place on my page that you see anything about the form is where I've specified... it's just a little simplified. Here, I'll include the code for the entire page. Entire page code: <?php if (isset($_POST['submit'])) { // If the form has been submitted. $fname = form($_POST['fname']); $lname = form($_POST['lname']); $email = form($_POST['email']); $position = form($_POST['position']); $district = form($_POST['district']); $message = form($_POST['message']); $sendto = "[email protected]"; $emailsubject = "New Contact"; $emailmessage = "<html> <body> <table width='400' border='1' cellspacing='0' cellpadding='0'> <tr> <td width='150' align='left' valign='top'> Name: </td> <td width='250' align='right' valign='top'> $fname $lname </td> </tr> <tr> <td width='150' align='left' valign='top'> Email: </td> <td width='250' align='right' valign='top'> $email </td> </tr> <tr> <td width='150' align='left' valign='top'> Position/Title: </td> <td width='250' align='right' valign='top'> $position </td> </tr> <tr> <td width='150' align='left' valign='top'> District: </td> <td width='250' align='right' valign='top'> $district </td> </tr> <tr> <td width='150' align='left' valign='top'> Message: </td> <td width='250' align='right' valign='top'> </td> </tr> </table> <table width='400' border='0' cellspacing='0' cellpadding='0'> <tr> <td width='400' align='left' valign='top'> $message </td> </tr> </table> </body> </html>"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: $fname $lname' . "\r\n"; // Tells the mail server who the email is from $fromaddress = '-f $email'; // Mail it mail($sendto, $emailsubject, $emailmessage, $headers, $fromaddress); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="shortcut icon" href="favicon.ico"> <link href="style.css" rel="stylesheet" type="text/css" /> <script language="JavaScript"> function validateContactForm() { var fname=document.forms["contact"]["fname"].value if (fname==null || fname=="") { alert("Please enter your first name."); return false; } var lname=document.forms["contact"]["lname"].value if (lname==null || lname=="") { alert("Please enter your last name."); return false; } var email=document.forms["contact"]["email"].value var atpos=email.indexOf("@"); var dotpos=email.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) { alert("Please enter a valid e-mail address."); return false; } var position=document.forms["contact"]["position"].value if (position==null || position=="") { alert("Please enter your title or position you hold within the school distict you are affiliated with."); return false; } var district=document.forms["contact"]["district"].value if (district==null || district=="") { alert("Please enter the name of the school distict in which you are affiliated with."); return false; } var message=document.forms["contact"]["message"].value if (message==null || message=="") { alert("Please type your message before submitting the contact form."); return false; } } </script> <title>Contact Us</title> </head> <?php if (isset($_POST['submit'])) { echo "<body onLoad=\"alert('test')\">"; } else { echo "<body>"; } ?> <div id="container"> <div id="header"> <div id="logo_w1">Welcome<span class="sm1">SM</span></div> <div id="logo_w2">Anonymous Communication System</div> <div id="header_text"> <p>"Helping kids to safely communicate with the comfort of anonymity."</p> <a href="requestdemo.php">Request a free demo of our system.</a> </div> <ul> <li><a href="index.html">Home</a></li> <li><a href="about.html">About Us</a></li> <li><a href="service.html">Our Service</a></li> <li><a href="founder.html">Meet the Founder</a></li> <li><a href="news.html">News & Press</a></li> <li><a href="contact.php">Contact Us</a></li> </ul> </div> <div id="content"> <div id="left"> <h1>Contact Us...</h1> <p>Please provide the following information:<br /> <em>Required*</em></p> <form name="contact" method="POST" action="contact.php" onsubmit="return validateContactForm()"> <table width="470" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="170" align="left" valign="top"> First Name*:<br /><img src="images/nothing.gif" height="5" /> </td> <td width="300" align="right" valign="top"> <input class="textbox" name="fname" type="text" /><br /><img src="images/nothing.gif" height="5" /> </td> </tr> <tr> <td width="150" align="left" valign="top"> Last Name*:<br /><img src="images/nothing.gif" height="5" /> </td> <td width="300" align="right" valign="top"> <input class="textbox" name="lname" type="text" /><br /><img src="images/nothing.gif" height="5" /> </td> </tr> <tr> <td width="150" align="left" valign="top"> Email*:<br /><img src="images/nothing.gif" height="5" /> </td> <td width="300" align="right" valign="top"> <input class="textbox" name="email" type="text" /><br /><img src="images/nothing.gif" height="5" /> </td> </tr> <tr> <td width="170" align="left" valign="top"> Position/Title*:<br /><img src="images/nothing.gif" height="5" /> </td> <td width="300" align="right" valign="top"> <input class="textbox" name="position" type="text" /><br /><img src="images/nothing.gif" height="5" /> </td> </tr> <tr> <td width="150" align="left" valign="top"> School District*:<br /><img src="images/nothing.gif" height="5" /> </td> <td width="300" align="right" valign="top"> <input class="textbox" name="district" type="text" /><br /><img src="images/nothing.gif" height="5" /> </td> </tr> <tr> <td width="150" align="left" valign="top"> Phone:<br /><img src="images/nothing.gif" height="5" /> </td> <td width="300" align="right" valign="top"> <input class="textbox" name="phone" type="text" /><br /><img src="images/nothing.gif" height="5" /> </td> </tr> </table> </div> <div id="right"> <h3>Other Contact Methods...</h3> <p><strong>Contact us via email:</strong><br /> [email protected]</p> <p><strong>Or via telephone:</strong><br /> Phone1<br /> Phone2</p> <p> </p><p> </p> </div> <div id="whole"> Message*:<br /> <textarea class="textarea" name="message"></textarea><br /><img src="images/nothing.gif" height="5" /><br /> <input type="submit" name="submit" value="Send Message" /> </form> </div> <div id="footerline"></div> </div> <div id="footer"></div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181669 Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 That's the thing... I'm not using "function form()". Exactly right. Your executing a function called 'form', yet it's not defined anywhere. In this piece of code..... $message = form($_POST['message']); what exactly do you expect form() to do? It's not a php function. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181674 Share on other sites More sharing options...
elmas156 Posted March 2, 2011 Author Share Posted March 2, 2011 This line: $message = form($_POST['message']); should set the value of whatever was entered in the "message" field of the form to be the php variable $message. If this is not the correct way to do this, I have been doing it incorrectly all along... but it has been working perfectly until now. What changes do you suggest I make? Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181677 Share on other sites More sharing options...
Twistedweb123 Posted March 2, 2011 Share Posted March 2, 2011 it is showing an error, as you are calling an undefined function form() form() is not a inbuilt php function, so if you wish to use it, you must define what you want it to do. I'm guessing, that you may want it to sanitize your inputs, stripslashes etc? If not, to fix, simply remove form( & ) from around the post variables. If you are trying to sanitize, tell me and ill show you an example function to do so Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181678 Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 should set the value of whatever was entered in the "message" field of the form to be the php variable $message. If this is not the correct way to do this, I have been doing it incorrectly all along... but it has been working perfectly until now. You only need ..... $message = $_POST['message']; To set $message to your forms value. The only possible way your code was previously working was because you had the form() function defined somewhere. It's likely some validation or cleansing function you got from somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181680 Share on other sites More sharing options...
elmas156 Posted March 2, 2011 Author Share Posted March 2, 2011 OK, thanks for your help. What do you mean by sanitizing my inputs? I did get this code snippet from some code in a tutorial on phpfreaks, but I'm not sure how or why it has worked for me for so long. I have done about 3 php sites and it has worked on all of them until this one... and I'm only using php on one page of this site, which is the contact page. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181685 Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 What do you mean by sanitizing my inputs? User inputted data should not be just blindly inserted into a database, it needs to be validated and sanitized first. I have done about 3 php sites and it has worked on all of them until this one Without form() being defined, this code would ALWAYS generate a fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181687 Share on other sites More sharing options...
Twistedweb123 Posted March 2, 2011 Share Posted March 2, 2011 To make a very quick fix, place this code ABOVE the form process function form($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return $str; } This is a quick function (that ive named form) that will clean your input. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181689 Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 To make a very quick fix, place this code ABOVE the form process function form($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return $str; } This is a quick function (that ive named form) that will clean your input. If your using Mysql, you will then also (at least) need to run your data through mysql_real_escape_string after using the above function. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181701 Share on other sites More sharing options...
Twistedweb123 Posted March 2, 2011 Share Posted March 2, 2011 Yeah, i personally do, but his form is just to email and no database is involved Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181707 Share on other sites More sharing options...
elmas156 Posted March 2, 2011 Author Share Posted March 2, 2011 Oh yes, now I see what's been going on... I've always used MySQL in other sites that I've done, and I've always (unknowingly ;-) used a sanitizing function, again from a tutorial that I went through on phpfreaks. So in the past, it was already being taken care of. Man, I learn something new with php every day! Thanks for your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/229338-form-error/#findComment-1181774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.