Jump to content
Larry Ullman's Book Forums

rafaec

Members
  • Content Count

    9
  • Joined

  • Last visited

Community Reputation

0 Neutral

About rafaec

  • Rank
    Newbie
  1. I use Dreamweaver CS 5.5... I'll try uploading it to a hosting server and see what happens. Thank you for your help!
  2. Hi, The "unknown problem" alert is set by me to know there was a problem with responseText: if (ajax.responseText == 'VALID') { console.log('works'); alert('You are now logged in!'); } else if (ajax.responseText == 'INVALID') { console.log('failed'); alert('The submitted values do not match those on file!'); } else { alert ('Unknown problem') } I did comment each chunk of code, verified all the commands, even copy pasted from Larry's. I am now absolutely positive the problem is in this validation. For some reason, PHP returns 'VALID' but the var is set to 'VALID\r\n\r\n '. But weird enough, the NET tab in Firebug, shows on HTTP recieved the value VALID (no spaces or breaks). So I am sure, that the script is reading the right information. I am using XAMPP. Testing everything on localhost. Could that be the problem?
  3. Hi HartleySan, Thanks for your reply. I changed that before with no success. I actually tried using the login.js from Larry's website directly to rule out any typos... The console does not output "works" or "failed". Browser just alerts "Unknown problem". ??
  4. New info: When establishing a break point after alert('You are now logged in!'); ajax.responseText shows value of "VALID\r\n\r\n ". I really have no idea where is it getting the /r/n values from. PHP is basically if (...$_POST[...) // validate and set email and password to me@example.com and testpass { echo 'VALID'; } else { echo 'INVALID'; } no new lines no enters... ???
  5. Hi everyone, me again... I have broken my head over this one...no idea why it doesn't work. HTTP: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <form action="login.php" method="post" id="loginForm" novalidate> <fieldset> <legend> Login </legend> <div><label for="email"> Email adress </label> <input type="email" name="email" id="email" required /></div> <div><label for="password"> Password </label> <input type="password" name="password" id="password" required /> </div> <div><label for="submit"> Submit </label> <input type="submit" name="Login →" id="submit"></div> </fieldset> </form> <script src="resources/login.js"> </script> <script src="ajax.js"> </script> </body> </html> LOGIN.JS // Script 11.x - login.js #2 // Function called when the form is submitted. // Function validates the form data and returns a Boolean value. function validateForm() { 'use strict'; // Get references to the form elements: var email = document.getElementById('email'); var password = document.getElementById('password'); // Validate! if ( (email.value.length > 0) && (password.value.length > 0) ) { // Perform an Ajax request: var ajax = getXMLHttpRequestObject(); ajax.onreadystatechange = function() { console.log (ajax.readyState); if (ajax.readyState == 4) { // Check the status code: if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) { console.log(ajax.status); console.log(ajax.responseText); if (ajax.responseText == 'VALID') { console.log('works'); alert('You are now logged in!'); } else if (ajax.responseText == 'INVALID') { console.log('failed'); alert('The submitted values do not match those on file!'); } else { alert ('Unknown problem') } } else { document.getElementById('theForm').submit(); } } }; ajax.open('POST', 'resources/login.php', true); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var data = 'email=' + encodeURIComponent(email.value) + '&password=' + encodeURIComponent(password.value); ajax.send(data); return false; } else { alert('Please complete the form!'); return false; } } // End of validateForm() function. // Function called when the window has been loaded. // Function needs to add an event listener to the form. function init() { 'use strict'; // Confirm that document.getElementById() can be used: if (document && document.getElementById) { var loginForm = document.getElementById('loginForm'); loginForm.onsubmit = validateForm; } } // End of init() function. // Assign an event listener to the window's load event: window.onload = init; PHP is the same as the book. The weird thing is that when I put the valid email adresses for some reason Ajax.responseText returns 'VALID", however, it still shows the alert 'Unknown problem'. I made a console.log so I am sure it returns 'VALID' and also to for ajax status. Everything seems to be ok, however, I can't make it to display 'You are now logged in". Anyone can see where the error is? Thanks!
  6. I was running them in Firebug console...also tried it in Chrome console...same issue. But you are right, when you run the whole code like that it works. Anyone have any idea why do you get such an error in console???
  7. Hi everyone, So, I am doing the zip code validator on the text: var regexp = /^\d{5}(-\d{4})?$/; regexp.test (15265-5325) returns false var regexp = /^\d{5}(-\d{4})?$/; // same code regexp.test (15265-3325) returns true I have realized that unless the first number after the hyphen is less than 4, the expression will return false. I tried putting ^ inside parenthesis with first (). Fixes error. But if the first digit is 1 or 0 returns false. >>> var regexp = /(^\d{5})(-\d{4})?$/; regexp.test (85265-5325) true >>> var regexp = /(^\d{5})(-\d{4})?$/; regexp.test (15265-5325) false >>> var regexp = /(^\d{5})(-\d{4})?$/; regexp.test (05265-5325) false Anyone has any idea why is this happening??? var regexp = /^(\d{5})(-\d{4})?$/; -
  8. Hey Larry, Love your book btw. I have one question: On the "Pursue" item 3, on Chapter 9. It says to "rewrite customize.php so the script also applies the user's preferences." What do you mean by that? Could you post the solution here? Is the question telling you to make the "customize.php" page to set the css stylesheet to the cookie values once the cookie is set? In this case, can you comment if my code is correct? Please clarify Thanks! <?php //customize.php cookie handling if (isset($_GET['font_size'], $_GET['font_color'])) { //SEND THE COOKIES! setcookie ('font_size', $_GET['font_size'], time()+100000000, '/', '', 0); setcookie ('font_color',$_GET['font_color'], time()+100000000, '/', '', 0); //msg to be printed later: $msg = '<p>Your settings have been entered! Click , <a href="view_settings.php">here</a> to see it in action </p>'; } // end of submitted if ?> <!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=utf-8" /> </head> <body> <?php //if cookies set print msg if (isset($msg)) { print $msg; } ?> <style type="text/css"> body { <?php if (isset($_GET['font_size']) || isset($_GET['font_color'])) { print " font-size: " . htmlentities($_GET['font_size']) . ";\n"; print " color: #" . htmlentities($_GET['font_color']) . ";\n"; } ?> } </style> <p> Use this form to set preferences </p> <form action="customize.php" method="GET"> <select name="font_size"> <option value=""> Font Size </option> <option value="xx-small"> xx-small</option> <option value="x-small"> x-small</option> <option value="small">small </option> <option value="medium">medium </option> <option value="large">large </option> <option value="x-large">x-large </option> <option value="xx-large"> xx-large</option> </select> <select name="font_color"> <option value="">Font Color </option> <option value="999">Gray</option> <option value="0c0">Green</option> <option value="00f">Blue</option> <option value="c00">Red</option> <option value="000">Black</option> </select> <input type="submit" name="submit" value="Set My Preferences" /> </form> </body> </html>
×
×
  • Create New...