drunkelf Posted October 30, 2012 Share Posted October 30, 2012 WHen I add this bit of code I get an error $name = trim($_POST['name']); if (empty($name)) { $error['name'] = 'Please enter your name'; } Notice: Undefined index: name in C:\htdocs\phpdw\contact.php on line 24 it is suppose to check whether the form for name is empty or not.The text field has a name and id of name.here is the rest of the code most of it irrevelant.Can somebody help me with this? <?php if (array_key_exists('ewComments', $_POST)) { // mail processing script // initialize variables $to = 'heunesc@gmail.com'; // use your own email address $subject = 'Feedback from East-West Seasons'; // build the message $message = 'From: '.$_POST['name']."\n\n"; $message .= 'Email: '.$_POST['email']."\n\n"; $message .= 'Comments: '.$_POST['message']; // send the email // remove escape characters from POST array mail($to, $subject, $message); } // remove escape characters from POST array if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); } $name = trim($_POST['name']); if (empty($name)) { $error['name'] = 'Please enter your name'; } ?> <!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" /> <title>east west seasons</title> <?php /*include('style_rules.php');*/ ?> <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_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> <div id="wrapper"> <div id="titlebar"><img src="" alt="east west season" name="bluebellstop" width="738" height="32" id="bluebellstop" style="background-color: #00CC99" /></div> <div id="Main content"> <div id="nav"> <ul> <li id="thispage"><a href="index.php">Home</a></li> <li><a href="index.php">News</a></li> <li><a href="index.php">BLog</a></li> <li><a href="index.php">Gallery</a></li> <li><a href="index.php">Contact</a></li> </ul> </div> <h1>send us your comments</h1> <p>Duis aute irure dolor ut labore et dolore magna aliqua. Quis nostrud exercitation sunt in culpa lorem ipsum dolor sit amet. Excepteur sint occaecat ullamco laboris nisi ut labore et dolore magna aliqua. In reprehenderit in voluptate quis nostrud exercitation qui officia deserunt. </p><form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="Contact form" id="Contact form" onsubmit="MM_validateForm('name','','R','email','','NisEmail','message','','R');MM_validateForm('name','','R','email','','RisEmail','message','','R');return document.MM_returnValue"> <p> <label for="name">name:</label> <br /> <input type="text" name="name" id="name" /> </p> <p> <label for="email">email</label> <br /> <input type="text" name="email" id="email" /> </p> <p> <label for="message">message<br /> </label> <textarea name="message" cols="60" rows="6" id="message"></textarea> </p> <p> <input name="ewcomments" type="submit" id="ew comments" value="send comments" /> </p> </form> <p> </p> <div id="footer"> <?php include('copyright.php'); ?> </div> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/ Share on other sites More sharing options...
ManiacDan Posted October 30, 2012 Share Posted October 30, 2012 You need to check to see if the variable is set first: if ( isset($_POST['name']) && !empty($_POST['name']) ) Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388764 Share on other sites More sharing options...
Psycho Posted October 30, 2012 Share Posted October 30, 2012 You are trying to referencing an array index that doesn't exist. I assume this is happening when you load the page before you submit the form. That is a warning that should be suppressed in a production environment but is good to have on in a development environment. However, I typically use something such as below for any fields submitted in a form to work around such errors. $name = isset($_POST['name']) ? trim($_POST['name']) : ''; Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388765 Share on other sites More sharing options...
drunkelf Posted October 30, 2012 Author Share Posted October 30, 2012 (edited) You are trying to referencing an array index that doesn't exist. I assume this is happening when you load the page before you submit the form. That is a warning that should be suppressed in a production environment but is good to have on in a development environment. However, I typically use something such as below for any fields submitted in a form to work around such errors. $name = isset($_POST['name']) ? trim($_POST['name']) : ''; You are trying to referencing an array index that doesn't exist. I assume this is happening when you load the page before you submit the form. That is a warning that should be suppressed in a production environment but is good to have on in a development environment. However, I typically use something such as below for any fields submitted in a form to work around such errors. $name = isset($_POST['name']) ? trim($_POST['name']) : ''; OK i think I understand what you mean so where do I type in the code you gave me.IF you can tell me where I should put the code I think I will understand better Edited October 30, 2012 by drunkelf Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388786 Share on other sites More sharing options...
Jessica Posted October 30, 2012 Share Posted October 30, 2012 Before you try to use that variable. Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388793 Share on other sites More sharing options...
Psycho Posted October 30, 2012 Share Posted October 30, 2012 Change this $name = trim($_POST['name']); To the code I provided Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388821 Share on other sites More sharing options...
ManiacDan Posted October 30, 2012 Share Posted October 30, 2012 See how his code matches your code nearly exactly, but with some extra stuff? That's where. Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388823 Share on other sites More sharing options...
drunkelf Posted October 30, 2012 Author Share Posted October 30, 2012 Error is gone!I would like to understand what is happening here but right now it only needs to work. thank you Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388825 Share on other sites More sharing options...
ManiacDan Posted October 30, 2012 Share Posted October 30, 2012 Like Psycho said: You were trying to reference an array index which does not exist. That was the reason you were getting the error. The code he and I provided checks to see if the variable is set BEFORE it's used. Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388826 Share on other sites More sharing options...
Psycho Posted October 30, 2012 Share Posted October 30, 2012 Error is gone!I would like to understand what is happening here but right now it only needs to work. $name = isset($_POST['name']) ? trim($_POST['name']) : ''; That is what is called a ternary operator, which many people do not understand. It is also referred to as shorthand notation for an if/else which isn't completely correct. So, let me write it out another way that would be easier to explain. That line does the exact same thing as this: if isset($_POST['name']) { $name = trim($_POST['name']); } else { $name = ''; } So, it check if $_POST['name'] is set. If so, it sets $name to the trimmed value of that variable. If it is not set, then it sets $name to an empty string. The key here is that you can do an isset() check on an unset variable without triggering the error you were encountering previously. Quote Link to comment https://forums.phpfreaks.com/topic/270078-please-help-with-php-code/#findComment-1388835 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.