Jump to content

Gilbert

Members
  • Content count

    40
  • Joined

  • Last visited

  • Days Won

    1

Gilbert last won the day on April 25

Gilbert had the most liked content!

Community Reputation

1 Neutral

About Gilbert

  • Rank
    Newbie

Profile Information

  • Gender
    Male
  • Location
    Schenectady
  1. Gilbert

    sessionStorage vs Global Variables

    Thanx justsomeguy - I think I've got it now. I've got to read very carefully what is being referred to - the window or browser or page or site. Thanx
  2. Gilbert

    sessionStorage vs Global Variables

    I think I understand your point - The following excerpts are from thew3 JS tutorial under jsScope, but I think I read 'web page' as 'web site' in the first one, which would support your answer. But down further are two other excerpts which are confusing me. It says that HTML global variables belong to the window object and remain until the window is closed. The last line of the 3rd quote is what I was basing my hypothesis on. Please set me straight if I'm reading this wrong. Thanks for all your help. In the end, I have nothing against sessionStorage - I'm just trying to understand the scope thing (actually I use Listerine) (chuckle, chuckle) Global JavaScript Variables A variable declared outside a function, becomes GLOBAL. A global variable has global scope: All scripts and functions on a web page can access it. Global Variables in HTML With JavaScript, the global scope is the complete JavaScript environment. In HTML, the global scope is the window object. All global variables belong to the window object. The Lifetime of JavaScript Variables The lifetime of a JavaScript variable starts when it is declared. Local variables are deleted when the function is completed. In a web browser, global variables are deleted when you close the browser window (or tab), but remain available to new pages loaded into the same window.
  3. Hi, I tried a couple of searches and couldn't find a post that compared these 2 forms of storage. I was using sessionStorage in my script to remember information from a Db, but as I was reviewing variable properties and scope, I said to myself Why should I involve sessionStorage when I can just assign the info to a global variable which is accessable to the whole website and goes away when you leave the website, just like a session variable. Is this correct? Are the global variables not as reliable or something? What are the differences, similarities; and what can you recommend to me about this. Thanx a million!
  4. Gilbert

    php is concatenating 2 values in its return

    Thank you very much - great explanation! I have programmed in 'normal' languages for some time now, but I have to start thinking in abstracts more with asynchronous stuff going on. I appreciate all the help from good, experienced coders like yourself. Gil
  5. Gilbert

    php is concatenating 2 values in its return

    Thank you. I think I have it straight, but I just came up with another fly in my ointment. I can't figure what's wrong here - I get 'undefined' when I look at 'goodInput'. I have followed the w3 examples doing an xmlhttprequest specifying a callback function; usually w3 will put the responseText into a 'demo' element and assign it with innerHTML So I took it one step further and thought why can't you cut out the middleman and use a 'return' to send back the responseText and assign it to a variable. I am checking for valid input and am only going to get one response back so it seemed logical (to me). I have tested a similar function with a callback and it worked well, but with 4 more lines needed for the callback. Here's my code for the calling function and the xhr function; is it possible to do it this way? The userInput I enter is in the correct form and should return as is. // userInput is from a prompt input box and passed to this function // not worried about security here - just functionality function validateInput(userInput) { var sendMe = "userID=" + userInput; var goodInput = loadPHPwithReturn("checkUserInput.php", sendMe); return goodInput; // goodInput is either = to the input or 'BadEntry" which I deal with on return } //Load an XMLHttpRequest with Return of Response Text function loadPHPwithReturn(url, sendMe) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { return this.responseText; } }; xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.send(sendMe); }
  6. Gilbert

    php is concatenating 2 values in its return

    Absolutely correct - it works like a charm - Thank You very Much!! I've been a little hazy on which to use where. I know return is supposed to send back a value whereas echo sends back a string maybe with html in it. Do I have that correct? And can you add anything to that so I can get it straight in my head. Thanx again, Gil
  7. Hi all, I have a php file to check a users input and it seems pretty straight-forward as far as logic goes, but the variable seems to be concatenating the "badEntry" onto its return value. I've been over it and over it and can't seem to see what's wrong. I originally had $userInput as the only return variable, but when I started having trouble I added the $toReturn variable to hopefully make it clear what I was doing wrong. Didn't help. I enter 'sakde12345' and I get that back with 'BadEntry' concatenated to it - looks like this - sakde12345BadEntry. My callback function is supposed to see if the return value is equal to 'BadEntry' and of course it isn't because it somehow includes the original input in front of it. It's returning this for either 'good' or 'bad' input. Can anyone figure out how I've miscoded or what? I also had a question about the debugger in chrome when I was running the js that called this php in the xmlhttprequest. The debugger traced thru everthing OK, but when it came to the 'onreadystatechange', it went to the 'xhr.open' line, jumped back to the onreadystatechange ( which became 1) but then it jumped to the requestheader line, then the .send line, then it jumped back to the js function that called the xhr request, then it jumped all the way back to the button in html that submitted the input. That kind of surprised me to see that - is the program just gathering information to proceed or something like that? I know it's kind of a weird question, but I'd like to understand what is going on. Thanx for any help, and I appreciate your patience!!! <?php $toReturn = ""; $userInput = ""; $chars = []; $i = 0; if ($_SERVER["REQUEST_METHOD"] == "POST"){ $userInput = $_POST["userID"]; // Get variable passed to xmlhttp call $userInput = test_input($userInput); // Run it thru easy data cleaner $userInput = strtolower($userInput); // change all to lower case $toReturn = $userInput; $chars = str_split($userInput); // split input into single characters if (strlen($userInput) != 10) { // is the input exactly 10 chars long $toReturn = "BadEntry"; } else { if (substr($userInput,0,3) != "sak") { // are the 1st 3 letters 'sak' $toReturn = "BadEntry"; } else { if (is_not_Char($chars[3]) || is_not_Char($chars[4])) { // are the 4th & 5th chars letters - name initials $toReturn = "BadEntry"; } else { for ($i = 5; $i < 10; $i++){ if (is_not_Numb($chars[$i])) { // are the last 5 chars Numbers $toReturn = "BadEntry"; } } } } } } echo $toReturn; function is_not_Char($examp){ $charBool = false; if ($examp < chr(97) || $examp > chr(122)) { // is it a lowercase letter - ascii 97 - 122 $charBool = true; } echo $charBool; } function is_not_Numb($examp){ $charBool = false; if ($examp < chr(48) || $examp > chr(57)) { // is it a number - ascii 48 - 57 ( 0 - 9 ) $charBool = true; } echo $charBool; } function test_input($data) { // standard data cleansing tools $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); echo $data; } ?>
  8. Gilbert

    mysqli OOps or procedural AND .php file naming

    Thank you very much for the good help straightening out my confusions. In the questions about the ext being .php or .html I should have said my confusion was because w3 uses several examples with a lot of php intertwined with html, but now I see that they should have the .html ext and leave the php ext for purely php files. On that subject, is it normal for a website to have a lot of little php files which may be called from an xmlhttp request, or something, or could a php file have several user-defined functions and then they are called when needed? It would seem easier to remember the name of a function and harder to keep track of 10 or 12 individual files. Can you invoke a specific php function from javascript?
  9. Hi all, I have a couple of questions that popped up while studying php; really not questions, but I want to get some confirmation that my deductions are correct from what I've figured out. I was getting very confused over the differences in the object-oriented mysqli and the procedural mysqli and translating from one to the other. I think I like using OOps better The reference list on w3 only shows the procedural syntax so I wanted to make sure I was coming up with the correct OOps. Things like 'mysqli_query($conn,$sql)' becomes '$conn->query($sql)'. I could see that quite clearly in the tutorial, but some others get a little hazy like 'mysqli_free_result($result)' becomes '$result->free_result'. Do I have this correct? and is there a rule about changing from one to the other - like whatever the object of the function is goes first in the OOps syntax, or something. Now I noticed that when you check for a connection error the OOps syntax says '$conn->connect_err' while the procedural syntax is 'mysqli_connect_error()' with nothing in the parenthesies. Is that because the connection object hasn't actually been established yet? or some other mysterious reason. I also had a quick question about .php files in general. The w3 said that a file with php in it should end with .php, but when I do that the server won't execute it - so I change it to html and its OK. Now if I have a file with exclusively php code and I don't 'echo' any output, but only 'return' a value, do I still need to write the <!DOCTYPE> and other tags like <head>, etc. Or can I start the file with <?php and end it with ?>. Like I said , I think I have these things correctly in my mind, but if I'm not right I want to nip it in the bud and make sure I'm off on a good foundation. I've experimented with different ideas, but if it doesn't work, sometimes you can't figure out what you did wrong. Thank you so much for your input and answers!!
  10. Gilbert

    invoke phone keyboard on autofocus

    I really thought it would be nice to save a 'keystroke' or 'tap' - if you have to do it with another click event what is the sense; they might as well click the input box. Thanx for your answer. I hope that a new method might come out to make this possible - I don't see that it is an invasion of privacy just to have your keyboard pop up when your going to do it anyway when an input box comes up.....
  11. Hi again, I would like to see if there is a way to have the phone's keyboard popup as soon as a page with a form loads. I was hoping that using autofocus might bring up the keyboard, but you still have to tap the input box to get it to show, so what's the point of autofocus. I figured if you have a certain input box with the focus you naturally would want the keyboard to show. Is there any way to have the phone's keyboard popup when you enter a page? Thanx
  12. Gilbert

    passing variables in jQuery

    I think I just figured it out - I searched further and the example they give in the 'jQuery AJAX' methods section gives a better example than in the "tutorial". They use a variable there and I understand how to use it now. Be careful - I've got a variable and I know how to use it!! I hope this post will help someone else if they have the same question.
  13. Gilbert

    passing variables in jQuery

    trying to learn .post method in jQuery - is there a way to pass a variable to the code to use in the 'data' argument of the function. The 'try it yourself' example from w3schools has the data hard-coded in, but I would like to use a variable that I got from a form, or something to do the post. I tried a few things but to no avail. Here is the code from the example: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.post("demo_test_post.asp", { name: "Donald Duck", city: "Duckburg" }, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> </head> <body> <button>Send an HTTP POST request to a page and get the result back</button> </body> </html> I tried this but it didn't work. I tried with and without the braces. How can you pass a variable to a jQuery method? should I use a global variable. please be explicit. Thanx <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var myName = "Donald Duck"; var myCity = "Duckburg"; var myData = "{name:" + myName + ", city:" + myCity + "}"; $.post("demo_test_post.asp", myData, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> </head> <body> <button>Send an HTTP POST request to a page and get the result back</button> </body> </html>
  14. Gilbert

    using 'view source' in a browser

    Thank very much dsonesuk! You have answered and confirmed some of the hazy questions that developed in my brain while studying this.
  15. Gilbert

    using 'view source' in a browser

    Thank you so much dsonesuk; very good explanation! I had to read a few times b/4 it all sank in. My question came out of exploring a login/signin situation. I thought I could use javascript to process the userID, which I had restricted to 8 chars. I thought I would first check to see if it was 8 chars in length, but then I realized if I wrote - ' if (uID.length == 8) ' , then someone could see that using view source and they would know enough to enter 8 chars - etc, etc. I think I understand that a userID or password or form data should be sent directly and immediately to php via an AJAX call for processing. would you say that is a fair assumption? Anyway, do you know of an article or website that can explain what needs to be done to secure a login page - not too complicated, but secure enough? I didn't think w3schools did that good a job on it - I still feel knid of up in the air about it. Thanx for your help.....
×