herghost Posted October 26, 2010 Share Posted October 26, 2010 Evening fellow freaks I am having a major problem with what I think is quite a simple script, its basically just a ajax same page registration script that is throwing up some php errors. I am not sure if the error lies in the php or js, or both! Ill post the scripts and then explain the problem login.php <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function() { //if submit button is clicked $('#signin_submit').click(function () { //Get the data from all the fields var username = $('input[name=username]'); var email = $('input[name=email]'); var password = $('input[name=password]'); //Simple validation to make sure user entered something //If error found, add hightlight class to the text field if (username.val()=='') { name.addClass('hightlight'); return false; } else name.removeClass('hightlight'); if (email.val()=='') { email.addClass('hightlight'); return false; } else email.removeClass('hightlight'); if (password.val()=='') { comment.addClass('hightlight'); return false; } else password.removeClass('hightlight'); //organize the data properly var data = 'username=' + username.val() + '&email=' + email.val() + '&password=' + password.val(); //disabled all the text fields $('.text').attr('disabled','true'); //show the loading sign $('.loading').show(); //start the ajax $.ajax({ //this is the php file that processes the data and send mail url: "processreg.php", //GET method is used type: "GET", //pass the data data: data, //Do not cache the page cache: false, //success success: function (html) { //if process.php returned 1/true (send mail success) if (html==1) { //hide the form $('.form').fadeOut('slow'); //show the success message $('.done').fadeIn('slow'); //if process.php returned 0/false (send mail failed) } else alert('Sorry, unexpected error. Please try again later.'); } }); //cancel the submit button default behaviours return false; }); }); </script> <div id="logincontainer"> <div id="topnav" class="topnav"> <a href="login" class="login"><span>Register</span></a> <a href="login" class="signin"><span>Log In</span></a> </div> <fieldset id="signin_menu"> <form method="post" id="signin" action=""> <label for="username">Username</label> <input id="username" name="username" value="" title="username" tabindex="4" type="text"> <br> <label for="password">Password</label> <input id="password" name="password" value="" title="password" tabindex="5" type="password"> <p class="remember"> <input id="signin_submit" value="Sign in" tabindex="6" type="submit"> <input id="remember" name="remember_me" value="1" tabindex="7" type="checkbox"> <label for="remember">Remember me</label> </p> <p class="forgot"> <a href="#" id="resend_password_link">Forgot your password?</a> </p> <p class="forgot-username"> <A id=forgot_username_link title="If you remember your password, try logging in with your email" href="#">Forgot your username?</A> </p> </form> </fieldset> <fieldset id="login_menu"> <form method="post" id="login" action="processreg.php"> <label for="username">Username</label> <input id="username" name="username" title="username" type="text"> <br> <label for="email">Email</label> <input id="email" name="email" title="email" type="text"><br> <label for="password">Password</label> <input id="password" name="password" title="password" type="password"> <input id="submit" value="Register" type="submit"> </p> </form> </fieldset> <div class="loading"></div> <div class="done"> <b>Thank you for joining!</b> You can now login. </div> </div> <script type="text/javascript"> $(document).ready(function() { $(".signin").click(function(e) { e.preventDefault(); $("fieldset#signin_menu").toggle(); $(".signin").toggleClass("menu-open"); }); $("fieldset#signin_menu").mouseup(function() { return false }); $(document).mouseup(function(e) { if($(e.target).parent("a.signin").length==0) { $(".signin").removeClass("menu-open"); $("fieldset#signin_menu").hide(); } }); }); $(document).ready(function() { $(".login").click(function(e) { e.preventDefault(); $("fieldset#login_menu").toggle(); $(".login").toggleClass("menu-open"); }); $("fieldset#login_menu").mouseup(function() { return false }); $(document).mouseup(function(e) { if($(e.target).parent("a.login").length==0) { $(".login").removeClass("menu-open"); $("fieldset#login_menu").hide(); } }); }); </script> <script src="javascripts/jquery.tipsy.js" type="text/javascript"></script> <script type='text/javascript'> $(function() { $('#forgot_username_link').tipsy({gravity: 'w'}); }); </script> and processreg.php <?php include('connect.php'); //Retrieve form data. //GET - user submitted data using AJAX //POST - in case user does not support javascript, we'll use POST instead $username = ($_GET['username']) ? $_GET['username'] : $_POST['username']; $email = ($_GET['email']) ?$_GET['email'] : $_POST['email']; $password = ($_GET['password']) ?$_GET['password'] : $_POST['password']; //flag to indicate which method it uses. If POST set it to 1 if ($_POST) $post=1; //Simple server side validation for POST data, of course, you should validate the email if (!$username) $errors[count($errors)] = 'Please enter your name.'; if (!$email) $errors[count($errors)] = 'Please enter your email.'; if (!$password) $errors[count($errors)] = 'Please enter your required password.'; //if the errors array is empty, send the mail if (!$errors) { $newpassword= md5($password); $result="INSERT INTO userbase (username, email, password) VALUES ('$username','$email','$newpassword')"; //if POST was used, display the message straight away if ($_POST) { if (!mysql_query($result)) echo 'Thank you! We have received your message.'; else echo 'Sorry, unexpected error. Please try again later'; //else if GET was used, return the boolean value so that //ajax script can react accordingly //1 means success, 0 means failed } else { echo $result; } //if the errors array has values } else { //display the errors message for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>'; exit; } ?> The output errors Notice: Undefined index: username in C:\wamp\www\buy2earn\processreg.php on line 7 Notice: Undefined index: email in C:\wamp\www\buy2earn\processreg.php on line 8 Notice: Undefined index: password in C:\wamp\www\buy2earn\processreg.php on line 9 Notice: Undefined variable: errors in C:\wamp\www\buy2earn\processreg.php on line 21 Thank you! We have received your message. Now this is basic basic errors, which I know normally means I have named my form inputs different to the processing script, but in this case they are correct. I am using the second form in the login script which is this one: <form method="post" id="login" action="processreg.php"> <label for="username">Username</label> <input id="username" name="username" title="username" type="text"> <br> <label for="email">Email</label> <input id="email" name="email" title="email" type="text"><br> <label for="password">Password</label> <input id="password" name="password" title="password" type="password"> <input id="submit" value="Register" type="submit"> Can anyone explain what's the problem? My js skills are non existent so this came from my collection of saved rar files with no explanations and some very basic editing! Many Thanks Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 It means you're attempting to access the value of a variable or array element that doesn't exist. There's no logic in the script to see if the values exist before they are used. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted October 26, 2010 Share Posted October 26, 2010 Quickfix: Right at the top, initialize your variables: $username = ''; $email = ''; $password = ''; $errors = ''; I would definitely advise you use if statements where necessary. Quote Link to comment Share on other sites More sharing options...
herghost Posted October 26, 2010 Author Share Posted October 26, 2010 Thanks both, @Pikachu2000, Im sorry, im not quite sure what you mean? In the lines that are throwing up the error I am trying to define the variable from the form inputs/ @Anti-Moronic, Im sure that would work, but the problem still exists that the results are not coming from the form fields Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted October 26, 2010 Share Posted October 26, 2010 Sorry, big mistake! My post above is completely wrong. It is likely because you are not using isset within your if statment. ($_GET['username']) Should be.. (isset($_GET['username'])) ..and $errors should be initialized at the top. $errors = ''; Hope that helps. These are minor warnings, but worth fixing. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 Give this a try and see what happens. Let me know if you have any questions/problems. <?php include('connect.php'); //Retrieve form data. //GET - user submitted data using AJAX //POST - in case user does not support javascript, we'll use POST instead if( isset($_REQUEST['username']) ) { $username = ($_GET['username']) ? $_GET['username'] : $_POST['username']; } if( isset($_REQUEST['email']) ) { $email = ($_GET['email']) ?$_GET['email'] : $_POST['email']; } if( isset($_REQUEST['password']) ) { $password = ($_GET['password']) ?$_GET['password'] : $_POST['password']; } //flag to indicate which method it uses. If POST set it to 1 if ($_POST) { $post=1; } $errors = array(); //Simple server side validation for POST data, of course, you should validate the email if(!$username) { $errors[count($errors)] = 'Please enter your name.'; } if(!$email) { $errors[count($errors)] = 'Please enter your email.'; } if(!$password) { $errors[count($errors)] = 'Please enter your required password.'; } //if the errors array is empty, send the mail if (!$errors) { $newpassword= md5($password); $result = "INSERT INTO userbase (username, email, password) VALUES ('" . mysql_real_escape_string($username) . "', '" . mysql_real_escape_string($email) . "', '$newpassword')"; //if POST was used, display the message straight away if ($_POST) { if (!mysql_query($result)) { echo 'Thank you! We have received your message.'; } else { echo 'Sorry, unexpected error. Please try again later'; } //else if GET was used, return the boolean value so that //ajax script can react accordingly //1 means success, 0 means failed } else { echo $result; } //if the errors array has values } else { //display the errors message for ($i=0; $i<count($errors); $i++) { echo $errors[$i] . '<br/>'; } exit; } ?> EDIT: This probably isn't the best solution, but it should work for you. Quote Link to comment Share on other sites More sharing options...
herghost Posted October 26, 2010 Author Share Posted October 26, 2010 Thanks! That did what it was supposed to, however I cant get it to fire the ajax popup message on the login.php. I am going to store this for later and give it another go when I have read up a bit more on ajax Thanks again to all for their help Quote Link to comment 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.