Jump to content
Larry Ullman's Book Forums

danconia

Members
  • Content Count

    10
  • Joined

  • Last visited

Community Reputation

0 Neutral

About danconia

  • Rank
    Newbie
  1. Edit: Nevermind do not worry about this thread. Apparently my action attribute for my login form should have been "index.php?p=login". When I give correct login info, it works correctly so I just need to fix the conditional for when the login info is incorrect. Okay so I tried setting up a modular website with the standard header + left sidebar, content, and right sidebar + footer. I also want to include the ability to register / login and put up a "Home | Login" or "Home | Settings" links up at the to of the header depending on whether someone is logged in or not. Now, for some reason when someone logs in successfully and the $_SESSION array is supposed to take in the data from the users table and then head to index.php, it seems that the $_SESSION array is not being set at all... it is empty. I am trying to figure out exactly what is going wrong here. Here is part of my login page: if(mysqli_num_rows($user_check_result) == 1) { //start of valid single match $_SESSION = mysqli_fetch_array($user_check_result, MYSQLI_ASSOC); //update most recent log in $last_logged_query = "UPDATE users SET last_logged_in = NOW() WHERE user_id = {$_SESSION['user_id']} LIMIT 1"; $last_logged_result = dbc_query ($dbc, $last_logged_query); if(mysqli_affected_rows($dbc) != 1) { //if one row was not affected $notes['last_logged_in_failure'] = "There was an error recording the login."; }//end of one row not being affected mysqli_free_result($user_check_result); if(isset($last_logged_result)) mysqli_free_result($last_logged_result); mysqli_close($dbc); $exit_url = BASE_URL . '/index.php'; header("Location: $exit_url"); exit(); } Also, since the else clause after this if clause is not being executed, and the page is being redirected to index.php I assume that the $_SESSION variable is being set. For some reason the redirect seems to be losing the $_SESSION array when it goes over to index.php. I even had to separate the login.php script into a login.inc.calc.php script to be executed befor ethe header.php file and a login.inc.out.php to be executed after the header.php file since a redirect has to occur before HTML output. A rar file of everything (except images) from my site (only 11kb because I just started it) is located here: http://ipredict.danconia.us/ipredict.danconia.us.rar This whole modular thing is making things a bit confusing and I'm wondering whether it's really worth it... if it might not be worth it to go back to the non-modular way of doing things. On the other hand I don't want to back down from a good challenge. Any help would be appreciated. Thank you! Edit: Also, for the record the first real line in index.php is a require('./includes/config.inc.php'); and that config file's first line is start_session(); so I just don't get why that wouldn't be occurring: /* * index.php (homepage) for http://iPredict.danconia.us * Script created by Kylan Hurt * http://kylan.danconia.us */ require('./includes/config.inc.php'); $errors = array(); $notes = array();
  2. Found two of the problems. First of all one of my encodeURIComponent functions is misspelled encoreURIComponent, and I am missing the concatenation operator on that last encodeURIComponent.There still seems to be some other issues so I'm digging in deeper for that. JSLint / JSHint have been a help and I will utilize them in the future.
  3. So I have been writing my own code (ie not from the book) and am trying to use Ajax to do a star rating. My problems it that I keep on getting the following error: Uncaught SyntaxError: Unexpected token ( I have pointed out which line below is giving me the error but I am having problems finding out where the core of my problem is.. I have had problems before with defining functions and calling them. Why am I getting this error? I just want to call the newStarRating function at this point with the given parameters. Any help would be appreciated! For the record I realize there may be multiple problems with my code as I am a beginner. U.addEvent(U.$('rating_star_1'), 'mouseover', function(){ newStarRating(1, "yellow_full"); } ); U.addEvent(U.$('rating_star_2'), 'mouseover', function(){ newStarRating(2, "yellow_full"); } ); U.addEvent(U.$('rating_star_3'), 'mouseover', function(){ newStarRating(3, "yellow_full"); } ); U.addEvent(U.$('rating_star_4'), 'mouseover', function(){ newStarRating(4, "yellow_full"); } ); U.addEvent(U.$('rating_star_5'), 'mouseover', function(){ newStarRating(5, "yellow_full"); } ); U.addEvent(U.$('rating_star_6'), 'mouseover', function(){ newStarRating(6, "yellow_full"); } ); U.addEvent(U.$('rating_star_7'), 'mouseover', function(){ newStarRating(7, "yellow_full"); } ); U.addEvent(U.$('rating_star_8'), 'mouseover', function(){ newStarRating(8, "yellow_full"); } ); U.addEvent(U.$('rating_star_9'), 'mouseover', function(){ newStarRating(9, "yellow_full"); } ); U.addEvent(U.$('rating_star_10'), 'mouseover', function(){ newStarRating(10, "yellow_full"); } ); <?php if (mysqli_num_rows($already_reviewed_query_result) == 1) { echo "U.addEvent(U.$('star_rating_div'), 'mouseout', function(){ newStarRating(" . $current_user_review_rows['review_score'] .", 'blue'); } );"; } else { echo "U.addEvent(U.$('star_rating_div'), 'mouseout', function(){ newStarRating(0, 'yellow_full'); } );"; echo "/* \$already_reviewed_query is:*" . $already_reviewed_query . "*/"; } mysqli_close($dbc); ?> function newStarRating(starNumber, color) { 'use strict'; var i = null; for (var i = 1; i <= 10; i++) { document.getElementById('rating_star_' + i).src='../images/white_star_17px.png'; } //end of star color reset var j = null; for (var j = 1; j <= starNumber; j++) { document.getElementById('rating_star_' + j).src='../images/' + color + '_star_17px.png'; } } function starSubmitFxn(starNumber) { 'use strict'; //Get a reference (get element by ID) to the entered username value: var ajax = getXMLHttpRequestObject(); var currentUsername = '<?php echo $current_username ?>'; var currentEntityId = '<?php echo $entity_id ?>'; ajax.onreadystatechange = function() { if (ajax.readyState == 4){ //Check the status code: if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) { for ( var r = 1; r <= 10; r++){ if (ajax.responseText == r) { newStarRating(r, "blue");//end of newStarRating call } //end of response text check } //end of loop } //end of Ajax connectivity check } //end of Ajax ready state }; //end of function for execution on ready state change error line-> var data = 'currentUsername=' + encodeURIComponent(currentUsername) + '&currentEntityId =' + encodeURIComponent(currentEntityId) + '&starNumber =' encoreURIComponent(starNumber); ajax.open('POST', 'resources/star_submit.php?' + data, true); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); ajax.send(data); }//End of verifyUsername() function
  4. Yeah I have made several changes to the PHP file and then finally tried using just the URL for the PHP script and appending the variables to the end. It seems that, among other things, my mysqli_escape_string function didn't include the first parameter ($dbc). Once I fixed this and entered in the URL it gave me back the simple "VALID" text response. <?php require_once ($_SERVER["DOCUMENT_ROOT"] . '/includes/config.inc.php'); // if (isset ($_GET['enteredUsername'])) { require_once (MYSQL); // Connect to the db. $trimmed_username = trim($_GET['enteredUsername']); $submitted_username = mysqli_real_escape_string($dbc, $trimmed_username); } $check_username_query = "SELECT username FROM users WHERE username = '$submitted_username'"; $r = mysqli_query($dbc, $check_username_query); if (mysqli_num_rows($r) == 0) { echo 'VALID'; } else { echo 'INVALID'; } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ ?> For the Javascript I used the below code and it appears that when I tried to set the enteredUsername variable it didn't work or something because later on it was not be encoded into the URI. function verifyUsername() { 'use strict'; //Get a reference (get element by ID) to the entered username value: var ajax = getXMLHttpRequestObject(); [b] var enteredUsername = document.getElementById('username');[/b] var usernameSpan = U.$('usernameSpan'); // Validate the first name: if (/[a-zA-Z0-9._-]{1,25}$/i.test(enteredUsername.value)) { //tested true in console if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = ""; } else {usernameSpan.innerText = "";} //Begin the AJAX request ajax.onreadystatechange = function() { if (ajax.readyState == 4){ //Check the status code: if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) { if (ajax.responseText == 'VALID') { if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username available"; } else {usernameSpan.innerText = "Username available";} } else { if (ajax.responseText == 'INVALID'){ if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username not available"; } else {usernameSpan.innerText = "Username not available";} } } } } }; [b]var data = 'enteredUsername=' + encodeURIComponent(enteredUsername.value);[/b] ajax.open('GET', 'resources/verifyusername.php?' + data, true); //ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); ajax.send(data); } else { if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username must be 25 characters or less, contain only \n\ numbers, letters, '.', '_', and '-'. "; } else {usernameSpan.innerText = "Username must be 25 characters or less, contain only \n\ numbers, letters, '.', '_', and '-'."; } } } //End of verifyUsername() function So I decided to change the encodeURIComponent paranthetical to document.getElementById("username").value, and it worked! I am still not completely certain why the original enteredUsername.value didn't work but I'll have to look into it a bit more.
  5. Thank you HartleySan, I am still digging through and it looks like the issue is probably with AJAX sending the GET request. I finally figured out how to use the developer's console in Chrome and these are the errors that I am getting: An error occurred in script '/home/kylhur/danconia.us/resources/verifyusername.php' on line 11: Undefined variable: submitted_username An error occurred in script '/home/kylhur/danconia.us/resources/verifyusername.php' on line 12: Undefined variable: dbc An error occurred in script '/home/kylhur/danconia.us/resources/verifyusername.php' on line 12: mysqli_query() expects parameter 1 to be mysqli, null given An error occurred in script '/home/kylhur/danconia.us/resources/verifyusername.php' on line 15: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given I specifically noticed the following: [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) So I gotta look closer at my request object I believe. Then once I get that working I can debug the PHP and MySQL code.
  6. Yeah I've started making changes according to your guys' suggestions. I've chopped out the unnecessary code a bit to try to isolate the problem but am still having issues. Unfortunately all of the tutorials that I see online use JQuery and I'm not sure I want to go through delving too deep into that right now since Ajax itself is giving me a hassle.
  7. Okay so I just finished reading the Javascript book (including Ch 11) and I am pushing forward trying to get AJAX to work on my prototype website for the first time. Specifically I want to have my registration page use AJAX to notify the user of whether their desired username is available in our system or not. I am familiar with HTML / CSS so I don't think the issue is in there, and am familiar enough with PHP and MySQL but not necessarily how PHP interacts with Javascript via AJAX. Here is my code, emphasis on the Javascript portion (and PHP portion) because those are the parts that I am most unfamiliar with: <tr> <td class="form_left"><label for="username">Username:</label></td> <td class="form_right"><input type="text" id="username" name="username" size="25" maxlength="25" value="<?php if (isset($_POST['username'])) {echo $_POST['username'];} ?>" onkeyup="verifyUsername()" /><span id="usernameSpan" style="color:red;">*</span></td> </tr> ... <script src="/js/register.js"></script> <script src="/js/ajax.js"></script> Here is my PHP code: require_once ($_SERVER["DOCUMENT_ROOT"] . '/includes/config.inc.php');[/background][/size][/font][/color] [color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3][background=rgb(245, 245, 245)]// if (isset ($_POST['enteredUsername'])) { $submitted_username = $_POST['enteredUsername']; require_once (MYSQL); // Connect to the db. } $check_username_query = "SELECT username FROM users WHERE username = $submitted_username"; $r = mysqli_query ($dbc, $check_username_query) ; if (mysqli_num_rows($r) == 0) { echo 'VALID'; } else { echo 'INVALID'; } And here is my Javascript code: function verifyUsername() { 'use strict'; //Get a reference (get element by ID) to the entered username value: var ajax = getXMLHttpRequestObject(); var enteredUsername = U.$('username'); var usernameSpan = U.$('usernameSpan'); // Validate the first name: if (/[a-zA-Z0-9._-]{1,25}$/i.test(username.value)) { //tested true in console if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = ""; } else {usernameSpan.innerText = "";} //Begin the AJAX request ajax.onreadystatechange = function() { if (ajax.readyState == 4){ //Check the status code: if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) { if (ajax.responseText == 'VALID') { if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username available"; } else {usernameSpan.innerText = "Username available";} } else { if (ajax.responseText == 'INVALID'){ if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username not available"; } else {usernameSpan.innerText = "Username not available";} } } } } }; ajax.open('POST', 'resources/verifyusername.php', true); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var data = 'enteredUsername=' + encodeURIComponent(username.value); ajax.send(data); } else { if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username must be 25 characters or less, contain only \n\ numbers, letters, '.', '_', and '-'. "; } else {usernameSpan.innerText = "Username must be 25 characters or less, contain only \n\ numbers, letters, '.', '_', and '-'."; } } } //End of verifyUsername() function As of right now when I enter a username it changes the asterisk to a blank spot. This means that my PERL regular expression is at least passing. If I can at least narrow down the error to whether it's the PHP or Javascript code causing the issue then that will make it much easier to eventually find and fix the problem. Any help would be greatly appreciated! Once I get this AJAX thing down the first time it will make all subsequent attempts much easier because I will have a template to base my attempts off of. I have been dabbling in the Developer's Console in Chrome which at least shows verifyusername.php being initiated by register.js but not much more info.
  8. Okay that makes perfect sense. Thank you for connecting those two points. I am not getting an error yet because I have not implemented the code yet. I am lifting much of it to insert into my own website and have not tested the script yet. I have some experience with form handling but not file uploading so the next 1-2 weeks working on this will be fairly rocky. Thanks a ton for the help!
  9. Okay I have manually created an 'uploads' folder but what I'm trying to get at is that we don't use the move_uploaded_file function before we test if it was actually done. It's like testing that a variable was set before actually setting the variable. At what stage or line during the script does the file get uploaded to the "uploads" folder. And is that folder supposed to be the temporary or permanent folder? The lines I showed are testing a CONDITIONAL of whether or not the file was moved, right? That line (with the 'if') isn't actually moving the file. Do you at least understand what I'm trying to ask?
  10. I am attempting to add a feature to my website where users are able to upload images for companies, and when looking at the code for 10.3 I noticed something that confused be on line 28 & 29: if (move_uploaded_file ($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name'] }")) { echo '<p><em>The file has been uploaded!</em></p>'; What confuses me about this line is that, up until this point it does not appear that the file has actually been moved to the permanent directory. How can we expect this conditional to ever come back TRUE (since it is a conditional...) if we've never established the permanent directory. It is never established at the end (the actual HTML form) nor the code before the conditionals (ie the HTML header area). Are these two lines supposed to both move the temp file AND act as a conditional? Or have we established this somewhere in the PHP.ini file? Because the PHP.ini file has upload_tmp_dir but not the permanent directory we want it to transfer the temp files to. Am I missing something here? Is this an error?
×
×
  • Create New...