Jump to content

Gilbert

Members
  • Posts

    63
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by Gilbert

  1. 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!
  2. 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
  3. 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); }
  4. 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
  5. 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; } ?>
  6. 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?
  7. 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!!
  8. 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.....
  9. 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
  10. 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.
  11. 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>
  12. Thank very much dsonesuk! You have answered and confirmed some of the hazy questions that developed in my brain while studying this.
  13. 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.....
  14. When you right click and click 'view source' in a browser, do you see ALL the code that is used for that page, including separate .css or .js files that might be in a separate folder? Or do you see only the html file and any <style> or <script> tags that are included; I imagine if you could see all the content, they would have to compile it all together somehow. Just curious - I've learner a lot from looking at the source code from many websites But it makes sense to me not to reveal ALL of your secrets online. Thanx After I posted this I was reading the w3school php tutorial and I have another question. I've heard it said that javascript is a browser language and php is a server language. In many of the examples they have embedded php right into the html code, like this - Welcome <?php echo $_GET["name"]; ?><br> Your email address is: <?php echo $_GET["email"]; ?> My question is this; does the browser make a server request every time there is a php tag - is there some way the php CAN run on the browser. I'm trying to learn logistics of what goes where and the why's and wherefores of it all. Very confusing when starting out. I just thought I should have posted this in the php section, but it is kind of a general question. Thanx again.
  15. I hope there is a simple solution to this. I want to use localStorage to remember a user, but if it's not there I want to open a form so the user can enter the info in the input box and press Submit. Then I would put that info into the localStorage for next time. I can get the form to open and display fine, but the code just continues to execute after I open the form ( display set to "block"). How can I get the code to stop and wait for the input to come in before continuing with the next line of code? I have looked at the <dialog> tag with '.showmodal' which would work I guess, but it says it is not supported in Internet Explorer or FireFox; I want it to work in all browsers. I have tried a while loop to wait until the 'submit' button is pushed, but the whole thing bombs out when I put it in and the modal form doesn't even show. If I assign a value to the input , and insert the line "userID = document.getElementById("enterUserID").value;" just b/4 the return statement, I get the value I assigned. I appreciate any help explaining it to me.... I have included my code if it helps. <!DOCTYPE html> <html> <head> <title>Schenectady Kettles</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- <link --> <style> #logInModal {display:none;z-index:3;width:80%;height:200px;margin:auto; color:purple;background-color:lemonchiffon;text-align:center; font-size:8vw;font-weight:bold;} </style> </head> <body> <h3>Testing for Log In</h3> <div> <button id="logInBtn" type="button" onclick="logInClicked()">Log In</button><br> </div> <!-- Log In Modal --> <div id="logInModal"> <div> Enter User ID<br> <input type="text" id="enterUserID" placeholder="User ID" autofocus> <!-- Check input !! --> <button type="button" id="getIDBtn">Submit</button> </div> </div> <p id="demo"></p> <script> function logInClicked() { var uID = "hello"; if (typeof(Storage) !== "undefined") { if (localStorage.userID) { uID = localStorage.userID; } else { uID = getUserID(); document.getElementById("demo").innerHTML = uID; } } } function getUserID() { var userID = "123"; document.getElementById("logInModal").style.display = "block"; document.getElementById("getIDBtn").onclick = function() {userID = document.getElementById("enterUserID").value;}; return userID; } </script> </body> </html>
  16. I'm baffled by this - I am trying to master for loops and I seem to have run into a snag. I looked up w3's nested for loops and got an example ( you can find it @ javascript statements and click on the 'for' link - at the bottom of the page) and it works great and I understand it . Then I tried to inject an 'if' statement to interrupt it on a condition, but I got some unexpected results and I can't see what is wrong. The logic seems correct - maybe someone can help me. The 1st listing is directly from the example and in the second listing I injected a 2 line if statement - I got the results I=o, j = 2 993 994 and there it stopped. I also changed the values of i & j, but I don't see why that matters. Thanks for any help! <!DOCTYPE html> <html> <body> <p>In this example, we have two loops. Because there is one loop that is inside the other, we call this a nested loop. The first loop is often called an "outer loop", and the second is often called the "inner loop" because it is inside the first, outer loop.</p> <p>The outer loop executes first, and for each time the outer loop is executed, the inner loop will also execute.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = ""; var i, j; for (i = 0; i < 3; i++) { text += "<br>" + "i = " + i + ", j = "; for (j = 10; j < 15; j++) { document.getElementById("demo").innerHTML = text += j + " "; } } } </script> </body> </html> <!DOCTYPE html> <html> <body> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = ""; var i, j; for (i = 0; i < 5; i++) { text += "<br>" + "i = " + i + ", j = "; for (j = 2; j < 5; j++) { document.getElementById("demo").innerHTML = text += j + " "; if (i = j) { text += 99; } } } } </script> </body> </html>
  17. Hey justsomeguy, thanx for the response. I guess it was just wishful thinking - I see now that the $_SESSION vars are probably kept on the server and the js vars are put in the browser. I'm learning a lot and really appreciate all the help from all you guys!! Gil
  18. That kinda sucks. Sometimes I get busy and forget that I asked a question and then 5 or 6 days later I'll check and see a reply. Anything that can be done to get notifications whenever someone replies to your post?? Thanx, Gil
  19. I'm not sure where to post this, but I hope a mod can help me. I am not being notifies of replies to my posts. I turned on the icon for 'Notify me of replies; I have checked my profile and my email is correct; I have checked my junk email folder and they are not there. Can someone help me figure out what the problem is? Thanks, Gil
  20. Thanks for the input, Ingolme. I had just started reading about local storage and had come to the same conclusion, that it would be easier to use. I am experimenting with it now to make sure I understand the whole concept. I was learning about session vars also and I wanted to confirm that the Session variables you set with php can be used by javascript using setItem & getItem, and if they're set with js you can retrieve them with $_SESSION. I haven't started experimenting with them yet, but it appears they are the same variable, just accessed differently. Do I have that concept correct. I would use that concept to retrieve data from a database table and store it in a $_SESSION with php, and then use sessionStorage.getitem to set vars in js and vice versa. Thanks for helping, Gil
  21. O.I.C. I got it. I have another question about cookies since I've been trying to find the best way to 'get' and 'check' them. I see some pretty complicated examples of cookies and what a program would have to do to decipher them, but if you are setting the cookies yourself, and supposedly no one else can deliver a cookie with your website, shouldn't you be able to make it as simple as you want. Of course I realize I'm dreaming about a perfect world, but is the coding & decoding of the cookies string really necessary? I've seen code where it starts with 'document.cookie' and works just fine. Can somebody show me the simplest and easiest way to set & get just a singal name-value pair on a simple website? Thanx, I'm getting frustrated @#$%^&*(), Gil
  22. Hey justsomeguy, thanks. But I really am just starting out - can you give me a boilerplate of what the directory path would look like and to what it is referring. I'm guessing it would look like this: "www.mywebsite.com/nameofsomefolder/nameofanotherfolder/" Am I close? I was wondering how that 'path="/"' fit into the scheme of things. But if I leave out the path definition will the cookies be available to the whole site to use on whatever page and in whatever js or php I want to write? Thanx, Gil
  23. I'm just starting to use cookies for personalizing my website and I need a little clarification about the DOM. When a cookie is sent along with the request for a website, does it become available to the whole site (like every page you might load) or just to a specific page included in your website. I am planning on using the setcookie, getcookie and checkcookie functions from w3 - can I put them in any page of the website or do I have to use them from the initial page of my site? Any guidance would be appreciated.
  24. Thank you, dsonesuk and ingolme, for some reason I'm not getting alerted when replies comes in - I've even checked my spam & junk folders. Thanks for your replies - helped a lot in my understanding - I do remember seeing some code where the action reference was to the same page- I think it was $_SELF? The code is great - I used something very similar on the w3 site. It solidified it in my brain to see the thought process. Thanks a lot, Gil
×
×
  • Create New...