pappakaka Posted February 9, 2011 Share Posted February 9, 2011 Ok soon i can't take this any more, there IS nothing wrong with this code! I've checked it, i have double checked it, tripple checked and then checked it again bit for bit and I can't find anything that suggest i should get these messages: Notice: Undefined index: birth_month in on line 30 Notice: Undefined index: birth_day in [php file location] on line 31 Notice: Undefined index: birth_year in [php file location] on line 32 Notice: Undefined index: iagree in [php file location] on line 33[/b] [size=14pt]I have one file with the HTML form named "register.php" and another file with the PHP code to process the form named "class.register.php". I don't know if it matters but i have to solve this to be able to move on with my website so i'll try to bring you all the facts and info i have..[/size] [size=14pt]Here is the HTML file/code (register.php):[/size] [code]<?php session_start(); if(isset($_POST['register'])) { include_once('classes/class.register.php'); $register = new Register(); if($register->process()) echo "Successfully Signed Up!"; else $register->show_errors(); } $token = $_SESSION['token'] = md5(uniqid(mt_rand(),true)); ?> <!doctype html> <html> <head> <link rel="stylesheet" type="text/css" href="css/main.css"> <link rel="stylesheet" type="text/css" href="css/register.css"> <script type="text/javascript" src="js/passwordmeter.js"></script> </head> <body> <div class="center"> <!-- PHP --> <?php require("html/menu.inc"); ?> <?php require("html/layout.inc"); ?> <?php require("html/login.inc"); ?> <!-- PHP --> <div class="register"> <header>Sign Up Now!</header> <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"> <ul> <li> <label for="username">* Username: </label><br /> <input name="username" autofocus="on" class="rusernamefield" type="text" required="required"></input> </li> <li> <label for="first_name">* First Name: </label><br /> <input name="first_name" class="rfirstnamefield" type="text" required="required"></input> </li> <li> <label for="last_name">Last Name: </label><br /> <input name="last_name" class="rlastnamefield" type="text"></input> </li> <li> <label for="password">* Password: </label><br /> <input name="password" class="rpasswordfield" onkeyup='password_strength(this.value)' minlength="6" maxlength="30" type="password" required="required"></input> </li> <div id="password_strength_border"> <div id="password_strength" class="strength0"></div> </div> <li> <label for="email">* Email Address: </label><br /> <input name="email" class="remail" type="email" required="required" placeholder="[email protected]"></input> </li> <li> <label for="confemail">* Confirm Email Address: </label><br /> <input name="confemail" class="rconfirmemail" type="email" required="required" placeholder="[email protected]"></input> </li> <li> <label for="gender">* Gender: </label><br /> <select name="gender" required="required"> <option value="Man" selected="selected">Man</option> <option value="Woman">Woman</option> </select> </li> <li> <label for="birth_month">* Birth Day: </label><br /> <select name="birth_month" required="required"> <option disabled="disabled" selected="selected">Month</option> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="birth_day" required="required"> <option disabled="disabled" selected="selected">Day</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <select name="birth_year" required="required"> <option disabled="disabled" selected="selected">Year</option> <option value="2011">2011</option> <option value="2010">2010</option> <option value="2009">2009</option> <option value="2008">2008</option> <option value="2007">2007</option> <option value="2006">2006</option> <option value="2005">2005</option> <option value="2004">2004</option> <option value="2003">2003</option> <option value="2002">2002</option> <option value="2001">2001</option> <option value="2000">2000</option> <option value="1999">1999</option> <option value="1998">1998</option> <option value="1997">1997</option> <option value="1996">1996</option> <option value="1995">1995</option> <option value="1994">1994</option> <option value="1993">1993</option> <option value="1992">1992</option> <option value="1991">1991</option> <option value="1990">1990</option> <option value="1989">1989</option> <option value="1988">1988</option> <option value="1987">1987</option> <option value="1986">1986</option> <option value="1985">1985</option> <option value="1984">1984</option> <option value="1983">1983</option> <option value="1982">1982</option> <option value="1981">1981</option> <option value="1980">1980</option> <option value="1979">1979</option> <option value="1978">1978</option> <option value="1977">1977</option> <option value="1976">1976</option> <option value="1975">1975</option> <option value="1974">1974</option> <option value="1973">1973</option> <option value="1972">1972</option> <option value="1971">1971</option> <option value="1970">1970</option> <option value="1969">1969</option> <option value="1968">1968</option> <option value="1967">1967</option> <option value="1966">1966</option> <option value="1965">1965</option> <option value="1964">1964</option> <option value="1963">1963</option> <option value="1962">1962</option> <option value="1961">1961</option> <option value="1960">1960</option> <option value="1959">1959</option> <option value="1958">1958</option> <option value="1957">1957</option> <option value="1956">1956</option> <option value="1955">1955</option> <option value="1954">1954</option> <option value="1953">1953</option> <option value="1952">1952</option> <option value="1951">1951</option> <option value="1950">1950</option> <option value="1949">1949</option> <option value="1948">1948</option> <option value="1947">1947</option> <option value="1946">1946</option> <option value="1945">1945</option> <option value="1944">1944</option> <option value="1943">1943</option> <option value="1942">1942</option> <option value="1941">1941</option> <option value="1940">1940</option> <option value="1939">1939</option> <option value="1938">1938</option> <option value="1937">1937</option> <option value="1936">1936</option> <option value="1935">1935</option> <option value="1934">1934</option> <option value="1933">1933</option> <option value="1932">1932</option> <option value="1931">1931</option> <option value="1930">1930</option> <option value="1929">1929</option> <option value="1928">1928</option> <option value="1927">1927</option> <option value="1926">1926</option> <option value="1925">1925</option> <option value="1924">1924</option> <option value="1923">1923</option> <option value="1922">1922</option> <option value="1921">1921</option> <option value="1920">1920</option> <option value="1919">1919</option> <option value="1918">1918</option> <option value="1917">1917</option> <option value="1916">1916</option> <option value="1915">1915</option> <option value="1914">1914</option> <option value="1913">1913</option> <option value="1912">1912</option> <option value="1911">1911</option> <option value="1910">1910</option> <option value="1909">1909</option> <option value="1908">1908</option> <option value="1907">1907</option> <option value="1906">1906</option> <option value="1905">1905</option> <option value="1904">1904</option> <option value="1903">1903</option> <option value="1902">1902</option> <option value="1901">1901</option> <option value="1900">1900</option> </select> </li> <li> <label for="iagree" class="iagreetext">* I Agree to the <a href="#">Privacy Policy</a> and <a href="#">Terms of Use</a></label> <input name="iagree" type="checkbox" required="required" class="iagreebox"></input> </li> <input name="register" class="registerbutton" type="submit" value="Sign Up"></input> <p class="fieldsmarked">Fields marked with an (*) is required</p> <input type="hidden" name="token" value="<?php echo $token;?>"/> </ul> </form> </div> </div> </body> </html>[/code] [size=14pt]And here is the PHP file/code (class.register.php):[/size] [code=php:0]<?php class Register { private $username; private $first_name; private $last_name; private $password; private $passmd5; private $email; private $gender; private $birth_month; private $birth_day; private $birth_year; private $iagree; private $errors; private $token; public function __construct() { $this->errors = array(); $this->username = $this->filter($_POST['username']); $this->first_name = $this->filter($_POST['first_name']); $this->last_name = $this->filter($_POST['last_name']); $this->password = $this->filter($_POST['password']); $this->email = $this->filter($_POST['email']); $this->gender = $this->filter($_POST['gender']); $this->birth_month = $this->filter($_POST['birth_month']); $this->birth_day = $this->filter($_POST['birth_day']); $this->birth_year = $this->filter($_POST['birth_year']); $this->iagree = $this->filter($_POST['iagree']); $this->token = $_POST['token']; $this->passmd5 = md5($this->password); } public function process() { if($this->valid_token() && $this->valid_data()) $this->register(); return count($this->errors)? 0 : 1; } public function filter($var) { return preg_replace('/[^a-zA-Z0-9@.]/','',$var); } public function register() { mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("membership") or die (mysql_error()); $sql = "INSERT INTO users(username,password,first_name,last_name,email,gender,birth_month,birth_day,birth_year) VALUES ('{$this->username}','{$this->passmd5}','{$this->first_name}','{$this->last_name}','{$this->email}','{$this->gender}','{$this->birth_month}','{$this->birth_day}','{$this->birth_year}')"; mysql_query($sql) or die(mysql_error()); if(mysql_affected_rows()< 1) $this->errors[] = "<div class=errormessages>Could Not Process Form</div>"; } public function user_exists() { mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("membership") or die (mysql_error()); $data = mysql_query("SELECT username FROM users WHERE username = '{$this->username}'"); return mysql_num_rows($data) > 0 ? 1 : 0; } public function show_errors() { foreach($this->errors as $key=>$value) echo "<div class=errormessages> $value </div> <br />"; } public function valid_data() { if ($this->user_exists()){ $this->errors[] = 'The username is already taken, choose another one!'; } if (empty($this->username)){ $this->errors[] = 'You must enter a username!'; } if (empty($this->first_name)){ $this->errors[] = 'You must enter your first name'; } if (empty($this->password)){ $this->errors[] = 'You must enter a password!'; } elseif (strlen($this->password) < 6){ $this->errors[] = 'Your password must be longer than 6 characters!'; } if (empty($this->email)){ $this->errors[] = 'You must enter an email address!'; } elseif (!preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/',$this->email)){ $this->errors[] = 'Your password can only contain these characters: a-z, A-Z and 0-9!'; } if (empty($this->confemail)){ $this->errors[] = 'You need to confirm your email address!'; } elseif ($this->confemail != $this->email){ $this->errors[] = 'The email addresses you entered did not match!'; } if (empty($this->birth_month)){ $this->errors[] = 'Select which month you were born!'; } if (empty($this->birth_day)){ $this->errors[] = 'Select which day you were born!'; } if (empty($this->birth_year)){ $this->errors[] = 'Select which year you were born!'; } if (empty($this->iagree)){ $this->errors[] = 'You must agree to the <a href="#">Privacy Policy</a> and <a href="#">Terms of Use to sign up!'; } return count($this->errors)? 0 : 1; } public function valid_token() { if(!isset($_SESSION['token']) || $this->token != $_SESSION['token']) $this->errors[] = "<div class=errormessages>Invalid Submission</div"; return count($this->errors)? 0 : 1; } } ?>[/code] I really need help with this.. So if you have 10 min or so to scroll through my code and see if you can see any direct faults i would really appreciate it! Cause I can't find anything wrong with it! [size=14pt][i]Note: I tried to solve it by changing the top part of the PHP code from:[/i][/size] [code=php:0]$this->birth_month = $this->filter($_POST['birth_month']); $this->birth_day = $this->filter($_POST['birth_day']); $this->birth_year = $this->filter($_POST['birth_year']); $this->iagree = $this->filter($_POST['iagree']);[/code] [size=14pt][i]To:[/i][/size] [code=php:0]$this->birth_month = $this->filter($_POST['birth_month'] = ""); $this->birth_day = $this->filter($_POST['birth_day'] = ""); $this->birth_year = $this->filter($_POST['birth_year'] = ""); $this->iagree = $this->filter($_POST['iagree'] = "");[/code] [size=14pt][i]That did take away the messages but instead whenever i for example chose a date in the dropdown menu it would always return false..[/i][/size] Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/ Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 Before $register = new Register(); try print_r($_POST) to see all your posted variables. From now on, anytime you want to access an array value, check if it's set first. if(isset($_POST['token'])){ ..code here. ETC. } Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172054 Share on other sites More sharing options...
pappakaka Posted February 9, 2011 Author Share Posted February 9, 2011 Ok I tried to change the code from: <?php session_start(); if(isset($_POST['register'])) { include_once('classes/class.register.php'); $register = new Register(); if($register->process()) echo "Successfully Signed Up!"; else $register->show_errors(); } $token = $_SESSION['token'] = md5(uniqid(mt_rand(),true)); ?> To: <?php session_start(); if(isset($_POST['token'])) { include_once('classes/class.register.php'); print_r($_POST) $register = new Register(); if($register->process()) echo "Successfully Signed Up!"; else $register->show_errors(); } $token = $_SESSION['token'] = md5(uniqid(mt_rand(),true)); ?> But then i just get this error message, i don't know how to fix that: Parse error: syntax error, unexpected T_VARIABLE in on line 10[/b] Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172079 Share on other sites More sharing options...
BlueSkyIS Posted February 9, 2011 Share Posted February 9, 2011 missing semi-colon after print_r($_POST) Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172081 Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 But then i just get this error message, i don't know how to fix that: Parse error: syntax error, unexpected T_VARIABLE in on line 10[/b] Whenever you get an error, look at the line it's referencing, and the ones before and after it. Check for common problems like typos. Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172084 Share on other sites More sharing options...
BlueSkyIS Posted February 9, 2011 Share Posted February 9, 2011 do you get those errors on post? using your code, i only get one notice when iagree isn't checked: Notice: Undefined index: iagree in on line 33 What editor are you using to update your files? Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172085 Share on other sites More sharing options...
pappakaka Posted February 9, 2011 Author Share Posted February 9, 2011 I'm sorry my bad, didn't look hard enough! Well then i get this at the top of my webpage, what does it mean really? Array ( [username] => [first_name] => [last_name] => [password] => => [confemail] => [gender] => Man [register] => Sign Up [token] => 69317acbf5cf9a58fa61490250f9e06c ) do you get those errors on post? using your code, i only get one notice when iagree isn't checked: Notice: Undefined index: iagree in on line 33 What editor are you using to update your files? I get the "Notice" errors when i hit the sign up button. I use Notepad++! Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172087 Share on other sites More sharing options...
BlueSkyIS Posted February 9, 2011 Share Posted February 9, 2011 Well then i get this at the top of my webpage, what does it mean really? Array ( [username] => [first_name] => [last_name] => [password] => => [confemail] => [gender] => Man [register] => Sign Up [token] => 69317acbf5cf9a58fa61490250f9e06c ) it shows that all of those POST variables are empty except for gender, register and token. pretty weird, as your code seems to work fine here. Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172089 Share on other sites More sharing options...
BlueSkyIS Posted February 9, 2011 Share Posted February 9, 2011 what browser are you using? i wonder of all those extraneous tags within the html is confusing it... autofocus="on" required="required" OH, problem spotted: <input name="confemail" class="rconfirmemail" type="email"> type should be text, not email. Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172090 Share on other sites More sharing options...
pappakaka Posted February 10, 2011 Author Share Posted February 10, 2011 I use the latest version of Chrome. All of the fields are empty because i didn't type anything in them, I've forgotten to mention that if i choose something in the date field or klick "I Agree bla bla.." i can sign up successfully as you say you can if you try. But it's only when i don't choose anything that the errors appear. How could i forget to write that.. I want to fix it cause i don't want any error messages to show up if a person signs up at my website and types something wrong or forget to check a checkbox! Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172096 Share on other sites More sharing options...
Jessica Posted February 10, 2011 Share Posted February 10, 2011 From now on, anytime you want to access an array value, check if it's set first. if(isset($_POST['token'])){ ..code here. ETC. } Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172099 Share on other sites More sharing options...
BlueSkyIS Posted February 10, 2011 Share Posted February 10, 2011 yes, exactly. i already typed this, so here is an example: if (isset($_POST['username'])) { $this->username = $this->filter($_POST['username']); } else { $this->username = ''; } DOH, and already posted once above by Jesirose. i wish I paid more attention to what's already been posted. Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172101 Share on other sites More sharing options...
Pikachu2000 Posted February 10, 2011 Share Posted February 10, 2011 I use the latest version of Chrome. All of the fields are empty because i didn't type anything in them, I've forgotten to mention that if i choose something in the date field or klick "I Agree bla bla.." i can sign up successfully as you say you can if you try. But it's only when i don't choose anything that the errors appear. How could i forget to write that.. I want to fix it cause i don't want any error messages to show up if a person signs up at my website and types something wrong or forget to check a checkbox! That's the purpose of field validation, which you should also be doing. Error reporting should not be enabled on a live production site. Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172179 Share on other sites More sharing options...
pappakaka Posted February 10, 2011 Author Share Posted February 10, 2011 Ok thanks everyone for the help, i really appreciate it! I used what jesirose and BlueSkyIS said and it helpt me to understand a little more about the problem but still that wasn't the solution. Infact I have no idea what solved it? Cause I spent the whole yesterday evening trying to solve it and finally i gave up too try agin in the morning.. And now when i click on "Sign Up" in my registration form, i don't get any error messages anymore so I can't say what the problem was. Anyway, the problem is gone! Thanks again all! Also: That's the purpose of field validation, which you should also be doing. Error reporting should not be enabled on a live production site. I know this, but how do turn error reporting off? Quote Link to comment https://forums.phpfreaks.com/topic/227216-notice-undefined-index-problem-wont-go-away/#findComment-1172277 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.