jmevz Posted April 30, 2011 Share Posted April 30, 2011 Hi There, I'm a NEWBY PHP and am following this tutorial "http://youtu.be/U1jFhFM4l8o". I've copied his code almost exactly but with my credentials as I have designed my database a little differently, but I don't see why his page loads fine yet for all of my $variable lines I get this error: "Notice: Undefined index: submit in C:\xampp\htdocs\indiemunity\register.php on line 7". I'm presuming I need to open the connection to the database on each page accessing it and close it at the end, which is why I have the include where the tutorial doesn't. I just don't get this? Am I declaring the variables incorrectly? Possibly due to different version of PHP? I'm running XAMPP/PHPMyAdmin/SQL. MY CODE BELOW ------------------------------------------------------------------------------------------------------------------------------- <?php include_once 'dbconnect.php'; echo "<h1>Register</h1>"; // Form Data $submit = $_POST['submit']; $useremail = $_POST['useremail']; $username = $_POST['username']; $userpassword = $_POST['userpassword']; $userpasswordcheck = $_POST['userpasswordcheck']; ?> <html> <form action='register.php' method='POST'> <table> <tr> <td> Email: </td> <td> <input type='text' name='useremail'> </td> </tr> <tr> <td> Username: </td> <td> <input type='text' name='username'> </td> </tr> <tr> <td> Password: </td> <td> <input type='password' name='userpassword'> </td> </tr> <tr> <td> Repeat: </td> <td> <input type='password' name='userpasswordcheck'> </td> </tr> </table> <p> <input type='submit' name='submit' value='Register'> </form> </html> ------------------------------------------------------------------------------------------------------------------------------- THE ERROR ------------------------------------------------------------------------------------------------------------------------------- Notice: Undefined index: submit in C:\xampp\htdocs\indiemunity\register.php on line 7 Notice: Undefined index: useremail in C:\xampp\htdocs\indiemunity\register.php on line 8 Notice: Undefined index: username in C:\xampp\htdocs\indiemunity\register.php on line 9 Notice: Undefined index: userpassword in C:\xampp\htdocs\indiemunity\register.php on line 10 Notice: Undefined index: userpasswordcheck in C:\xampp\htdocs\indiemunity\register.php on line 11 ------------------------------------------------------------------------------------------------------------------------------- Hope you can help. I'm sure it's something simple. Thanks in advance, Jeff [email protected] - Interested in helping me further as well? Email me Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/ Share on other sites More sharing options...
hyster Posted April 30, 2011 Share Posted April 30, 2011 Undefined index means u have variables that are not set. there empty. u are calling the vars at the top of the page before they are entered into a form and submitted. so if u use logic u have to call them AFTER submission is this 1 page or more? typicaly a form is the 1st page and on submission the data gets sent to anothe processing page, allthough u can do it on 1 page. to get around the undifine index error u need the isset function <?php if (isset($_GET['email'])){ code that contains the variable u want. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208569 Share on other sites More sharing options...
murli800 Posted April 30, 2011 Share Posted April 30, 2011 hi...look ur line "<form action='register.php' method='POST'>".......in this u notice action=register.php dis means that all the data inserted by the user and then contaned in the variables is forwarded to register.php and you are trying to receive on the same page...if you want to eliminate these error..u just cut section mentioned below from the code and paste it at the begging of register.php // Form Data $submit = $_POST['submit']; $useremail = $_POST['useremail']; $username = $_POST['username']; $userpassword = $_POST['userpassword']; $userpasswordcheck = $_POST['userpasswordcheck']; Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208570 Share on other sites More sharing options...
jmevz Posted April 30, 2011 Author Share Posted April 30, 2011 @hyster: Logic! Now that makes sense, I'm going to go and try something, I'll come back to you Although I think you've solved it Ta, Jeff Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208572 Share on other sites More sharing options...
jmevz Posted April 30, 2011 Author Share Posted April 30, 2011 @hyster: There is the only 1 register page, I'd rather not create another page for processing the form, would rather do it all in one. So, I presume I have to use the; if (isset($_GET['email'])) for every field of the form. So, this reads: If there is data in the form, set email to value and then post? Regards, Jeff Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208573 Share on other sites More sharing options...
jmevz Posted April 30, 2011 Author Share Posted April 30, 2011 @murli800, Thanks for your help. But I'd rather do it all on one page, removing that code from this page would force a second processing page, from what I am aware? Anyway. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208575 Share on other sites More sharing options...
hyster Posted April 30, 2011 Share Posted April 30, 2011 u have to use if (isset($_GET['email'])) for each variable if (isset($_post['email'])) if (isset($_post['password'])) and so forth Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208576 Share on other sites More sharing options...
hyster Posted April 30, 2011 Share Posted April 30, 2011 sorry i didnt read ur post correctly. if i understand the isset function correctly it means. if the variable is empty then ignore it. Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208578 Share on other sites More sharing options...
jmevz Posted April 30, 2011 Author Share Posted April 30, 2011 @hyster: No, I think you were right anyway, so I've done this: --------------------------------------------------------------------------------------------------------------------- // Get form data and assign to Variable. if (isset($_GET['submit'])){ $submit = $_POST['submit']; } if (isset($_GET['useremail'])){ $useremail = $_POST['useremail']; } if (isset($_GET['username'])){ $username = $_POST['username']; } if (isset($_GET['userpassword'])){ $userpassword = $_POST['userpassword']; } if (isset($_GET['userpasswordcheck'])){ $userpasswordcheck = $_POST['userpasswordcheck']; } --------------------------------------------------------------------------------------------------------------------- Now, am I correct in saying that the above is, in plain English: If form field contains value, get that value and assign to variable. Now that that variable is assigned, I can then use the INSERT INTO function to insert those variables into my database? I'm a bit worried about the passwordcheck option though and how to render that as their is no actual field in my database for that. Regards, Jeff Thanks Heaps! Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208579 Share on other sites More sharing options...
hyster Posted April 30, 2011 Share Posted April 30, 2011 i forgot to change the _get to _post hope that makes sense <?php if (isset($_post['submit'])){ $submit = $_POST['submit']; if (isset($_post['useremail'])){ $useremail = $_POST['useremail']; if (isset($_post['username'])){ $username = $_POST['username']; if (isset($_post['userpassword'])){ $userpassword = $_POST['userpassword']; if (isset($_post['userpasswordcheck'])){ $userpasswordcheck = $_POST['userpasswordcheck']; //////////////// sql code and other code that contains the variables goes here //////////////// } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208581 Share on other sites More sharing options...
hyster Posted April 30, 2011 Share Posted April 30, 2011 will look something like this <?php if (isset($submit])){ $submit = $_POST['submit']; if (isset($useremail])){ $useremail = $useremail]; if (isset($username])){ $username = $_POST['username']; if (isset($userpassword])){ $userpassword = $_POST['userpassword']; if (isset($userpasswordcheck)){ $userpasswordcheck = $_POST['userpasswordcheck']; // check passwords match if ($userpassword = $userpasswordchech){ $sql="INSERT INTO $tbl_name (submit, useremail, username, userpassword) values ('$submit', '$useremail', '$username', '$userpassword')"; $result=mysql_query($sql); // check the sql statment worked ok if($result){ echo "Update Successful"; } else { echo "ERROR"; } } }else{ echo "your password do not match" } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208585 Share on other sites More sharing options...
jmevz Posted April 30, 2011 Author Share Posted April 30, 2011 @hyster: Hey, thanks for the code example, you're a legend. I ended up creating insert.php and adding my INSERT SQL to that, which work, but without the password check. I feel creating the second insert file pointless really, because when am I ever going to use that same insert statement somewhere else? Pointless... I will give your method below a shot. Thanks heaps, will let you know how I go Regards, Jeff Quote Link to comment https://forums.phpfreaks.com/topic/235168-notice-undefined-index-submit/#findComment-1208588 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.