Jump to content

spence911

Members
  • Posts

    38
  • Joined

  • Last visited

Everything posted by spence911

  1. I can't find an explicit answer on this anywhere, but when you create a child class from an abstract class must you use ALL of the methods that are inside the abstract class in the new child class? Or are these methods just available to the child class to pick and choose?
  2. Just figured this out. Its a constant that ensures that the data returned is an integer(TINYINT, MEDIUMINT, BIGINT...) so that PDO can pass the correct data type to MySQL. PDO will also escape any quotes and other characters thereby preventing SQL injection.
  3. What's up guys! Can somebody please explain the meaning of PDO::PARAM_INT in the following statement: $pdoStmt->bindValue(":num", $num, PDO::PARAM_INT); I have already created a PDO Statement object called $pdoStmt. This line will bind the value of $num to the placeholder called :num. I know that PDO::PARAM_INT is a Predefined Constant but what does it do in this case? Thanx
  4. Thanks for the workaround. So if I understand correctly, the value of links is undefined because once the for loop has finished executing, whatever new value the for loop has created for var links is thrown out and replaced by the original value which is var links = document.links; correct?
  5. Whats up guys. I've created two links in an html document. The problem is the JavaScript below gives me a value of undefined after running the for loop. I know this has something to do with closures, which I don't have much experience with so I can't figure out why the value of links is undefined. JavaScript: window.onload = function getLinks(){ var links = document.links; for(var i = 0, count = links.length; i < count; i++){ links[i].onclick = function(){ alert(links[i].href); return false; }; } }; html: <body> <p><a href="linkOne.html" id="link" target="linkOne">Link B</a></p> <p><a href="linkTwo.html" id="link" target="linkTwo">Link A</a></p> <script src="js/handleLinks.js"></script> </body> </html>
  6. Thanks a lot. That I know. But why does removing the catch block affect the very last line: document.write( "Back to safety." ); That last line will only execute if the catch block is not omitted out.
  7. Hi guys! I would really appreciate it if somebody could explain to me why in the code below the last line "document.write( "Back to safety." );" will only execute if the catch block is not commented out. In other words, the script below will output "Finally!". But if the catch block is not commented out, the output is "Caught your error:someFunction is not definedFinally!Back to safety." <head> <title>Title Time! Yeah</title> <script type="text/javascript"> try { // Throw an error so the catch is triggered. nonExistentFunction(); } /*catch( error ){ // Catch the error. document.write( "Caught your error:", error.message ); } */ finally { document.write( "Finally!" ); } // We got past our try/catch. document.write( "Back to safety." ); </script> </head> <body> </body> </html>
  8. Thanks denno020. I created a <p> container with the id of output and added this at the top of the function: document.getElementById('output').innerHtml = '';. But I'm still getting an error messages on every click instead of just one. html: <body> <div><label for="firstName">First Name</label> <input type="text" name="firstName" id="firstName" value="First Name"/></div> <input type="button" name="btn" id="btn" value="Submit"/> <p id="output"></p> <script src="js/validayt.js"></script> </body> </html> js: window.onload = function(){ 'use strict'; document.getElementById('btn').onclick = function(){ document.getElementById('output').innerHtml = ''; if(document.getElementById('firstName').value == document.getElementById('firstName').defaultValue){ var msg = document.createTextNode('Incorrect information.'); document.getElementById('output').appendChild(msg); } }; };
  9. I'm using JavaScript to validate a form field. Basically, if the form is submitted and the default value of the field is not altered, a text node with an error message will be generated. Problem is if the submit button is clicked over and over, the error message will appear just as many times as the button is clicked, ie. Incorrect information.Incorrect information.Incorrect information.Incorrect information. How do I make the generated message appear only on the first button submit. I tried this but it JavaScript: window.onload = function(){ 'use strict'; var errorMsg = 'Incorrect information.'; document.getElementById('btn').onclick = function(){ if(document.getElementById('btn').value == document.getElementById('btn').defaultValue){ var msg = document.createTextNode(msg); document.getElementById('btn').parentNode.appendChild(msg); /* This if statement didn't work if(msg){ document.getElementById('btn').parentNode.replaceChild(msg); }*/ } }; }; html: <body> <form action="#" method="POST" id="theForm"> <div><input type="text" name="firstName" id="firstName" value="First Name"/></div> <input type="button" name="btn" id="btn" value="Submit Info"/> </form> <script src="js/validate.js"></script> </body> </html>
  10. Since the form's action is login.php, I'm guessing return true; could replace document.getElementById('theForm').submit(); I dont know if I'm right on this one. If it helps, this is what the html looks like: <body> <!-- login.html --> <form action="login.php" method="post" id="loginForm"> <fieldset> <legend>Login</legend> <div><label for="email">Email Address</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"></label><input type="submit" value="Login →" id="submit"></div> </fieldset> </form> <script src="login.js"></script> </body>
  11. My bad (noob moment), I just realized that its a precaution for if the AJAX call fails; the form submission will still go through. But would we get the same effect if we used return true; instead of document.getElementById('theForm').submit();
  12. This is a script that validates a user's email and password. Everything is super clear but I can't figure out the purpose of document.getElementById('theForm').submit(); As far as I know, it's a method that submits the form to the script referenced inside the forms html action tag eg. <form action="login.php"> (just in case ajax fails for whatever reason). But I can't find any information to validate this. 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() { if (ajax.readyState == 4) { // Check the status code: if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) { if (ajax.responseText == 'VALID') { alert('You are now logged in!'); } else { alert('The submitted values do not match those on file!'); } } 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;
  13. Thanks gristoi, if I'm coorect the native (or default behavior) of the first link is to open "popupB.html". This is what the html looks like btw: <body> <p><a href="popupB.html" id="link" target="PopUp">B Link</a> (Will open in a new window.)</p> <p><a href="popupA.html" id="link" target="PopUp">A Link</a> (Will open in a new window.)</p> <script src="js/popups.js"></script> </body> </html> Why do we still need a return false after the function has completed and the popup window is open?
  14. I've never understood the application of return true and return false after function calls in JavaScript. Could somebody please explain their meaning. This is a very simple script which will create a popup window for each links in an html document. function createPopup(e) { 'use strict'; // Get the event object: if (typeof e == 'undefined') var e = window.event; // Get the event target: var target = e.target || e.srcElement; // Create the window: var popup = window.open(target.href, 'PopUp', 'height=100,width=100,top=100,left=100,location=no,resizable=yes,scrollbars=yes'); // Give the window focus if it's open: if ( (popup !== null) && !popup.closed) { popup.focus(); return false; // Prevent the default behavior. } else { // Allow the default behavior. return true; } } // End of createPopup() function. // Establish functionality on window load: window.onload = function() { 'use strict'; // Add the click handler to each link: for (var i = 0, count = document.links.length; i < count; i++) { document.links[i].onclick = createPopup; } // End of for loop. }; // End of onload function. The only part of the script that doesn't make sense here is the return true/false. Why do we use return true or return false after calling a JavaScript function?
  15. Hello mates, I'm a newbie to JavaScript. Normally when I assign an HTML element to a variable created in JavaScript I place the element id in quotes like this: var output = document.getElementById('output'); But there is something new that I've come across: var output = document.getElementById(elementId); elementId is an undeclared parameter and it is without quotes. 'output' in the first snippet refers to <p id="output"></p> in the HTML code. What is surprising to me is that var output = document.getElementById(elementId); works just the same even though there is no HTML element declared with the name elementId. To put this into context, the code is in 2 files; today.html and today.js. today.html: <html> <head> <title>Today</title> </head> <body> <p id="output"></p> <script src="today.js"></script> </body> </html> today.js: function setText(elementId, message) { 'use strict'; if ( (typeof elementId == 'string') && (typeof message == 'string') ) { // Get a reference to the paragraph: var output = document.getElementById(elementId); // Update the innerText or textContent property of the paragraph: if (output.textContent !== undefined) { output.textContent = message; } else { output.innerText = message; } } // End of main IF. } function init() { 'use strict'; var today = new Date(); var message = 'Right now it is ' + today.toLocaleDateString(); message += ' at ' + today.getHours() + ':' + today.getMinutes(); // Update the page: setText('output', message); } // End of init() function. window.onload = init;
  16. I'm a newbie to JavaScript and I'm trying to wrap my head around anonymous functions. I understand how to create one but what is not clear is the purpose. Why make a function anonymous when it does the same job perfectly without being anonymous?
  17. I would really appreciate some assistance on how I can print out the values of object1, object2 and object3 from the code down below. Using document.write(object1); for example prints out [object Object] instead of the actual values. <script type="text/javascript"> var object1 = {value: 10}; var object2 = object1; var object3 = {value: 10}; </script> If it helps my browser is Google chrome.Thanks.
  18. Crystal clear now. Thanks Cracka Memba.
  19. I'm a beginner currently studying Object Oriented Programming. The code in question is quite long so I will just share the snippet that I can't understand. private $productlist = array(); public function addproduct($product){ $this->productlist[] = $product; } public function stockup(){ foreach($this->productlist as $product){ $product->addstock(100); } I'm sure that function addproduct adds product objects to the product list array. What does the foreach loop: foreach($this->productlist as $product) in the stockup function do? Does it take those values and store them back in $product?
  20. This is a very simple HTML table. There is no CSS stylesheet. <table cell spacing = "0" border = "5" style = "width: 20 em; border: 1px solid #567;"> <tr> <th>Sequence #</th> <th>Value</th> </tr> <tr> <td>F<sub>0</sub></td> <td>0</td> </tr> <tr class = "alt"> <td>F<sub>1</sub></td> <td>1</td> </tr> </table>
  21. I know the line below adds a table row but what is the significance of "alt"? <tr class="alt">
  22. Thanks CyberRobot. I completely overlooked the fact that I should run the function in order to get the value of $num. I added this after closing the function: multipli(); echo $num; and now it echoes the value of $num.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.