Jump to content
Larry Ullman's Book Forums

sonal

Members
  • Posts

    121
  • Joined

  • Last visited

  • Days Won

    2

Everything posted by sonal

  1. I'm having hard time to understand the topic, "referencing the event" on page no. 290. I'm reading it now for 4th time and experimenting with the code given. I understand that the "target" point to the HTML element that triggered the event. So, we have used target.nodeName property to get it's reference. If the target is a button, and I want to know the name of the button. I thought I can use the "name" property here like this: var btName = target.nodeName.name //but this gives, "undefined".. Why can't I use it like that?
  2. I have 3 buttons on my html document. I'm writing following code to get their reference. But the buttonArray length shows 0 all the time. What am I doing wrong? window.onload = function() { 'use strict'; var buttonArray = document.getElementsByTagName('button'); //to reference all buttons on the document. console.log(buttonArray.length); for(var i = 0, count = buttonArray.length; i<count; i++) { buttonArray.onclick = clickButton; }
  3. Ok, got it. Create an another (anonymous) function assigning an event which can take a function with argument. Thanks
  4. I understand that ch. 6,7, 8 are most important basics to understand. I'm getting confused as I read over them. 1. event listener and event handler are used form same thing? Please correct me if I am wrong. I get it that a function written to do something for an event is referred as either event listener or event handler. 2. Can I assign a function with argument for event handling? Like, I have to create a JavaScript game of 2 players. I want to disable/enable player's button to give each one a turn. I'm doubtful about the logic, but copying the code to show what I want to ask. How to handle such functions? or what am I missing? function dice() { 'use strict'; var diceno = Math.floor(Math.random()*6 + 1); document.getElementById("dice").innerHTML = diceno; } function toggleButton(bt) { 'use strict'; dice(); b.disabled = true; if(b.value == 'b2') { //here b.value does not give the name or id of button, as button is a input, and it's value property isundefined. console.log(b.value); document.dcForm1.b1.disabled = false; } else { console.log("b1"); document.dcForm1.b2.disabled = false; } } window.onload = function() { 'use strict'; document.getElementById('b1').onclick = toggleButton; //This thing does not work, as it requires a parameter. document.getElementById('b2').onclick = toggleButton; }
  5. <p>I changed above code as per following: </p> <p> </p> <div>$memids = array();</div> <div> $memnames = array();</div> <div> </div> <div> //Fetch and print all records:</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span> while($row = mysqli_fetch_row($r))</div> <div> {</div> <div> </div> <div>// list out member names in a horizontal row:</div> <div>echo '<tr class="rowA">'; </div> <div> echo '<td><a href="addbook9.php?id='. $row[1] .'&nm='. $row[2] .'"> '. ucfirst($row[2]) .'</a></td>';</div> <div> array_push($memids, $row[1]); //add each id & name in another array.</div> <div> array_push($memnames, $row[2]);</div> <div> </div> <div> $_SESSION['members'] = array('mid' => $memids, 'mname'=> $memnames);</div> <div> </div> <div> echo '<td><a href="view_books.php?id='. $row[1] .'&nm='. $row[2] .'">' . $row[3] . '</td>';</div> <div> echo '</tr>';</div> <div> } //End of while loop.</div> <div> </div> <div>echo '</table>';</div> <div> </div> <div>And it shows following result:</div> <div> </div> <div>Array ( [user_id] => 5 [fname] => SONAL [lname] => MACWAN [lid] => 1 [members] => Array ( [mid] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => 11 [5] => 12 ) [mname] => Array ( [0] => Stavan [1] => Ethan [2] => sonal [3] => daxesh [4] => Rupal [5] => Kavi ) ) )</div> <div> </div> <div>So, I am more confused now. How will I access each id-name pair? Is this the right way to add array of array into session?</div>
  6. $_SESSION['something'] = array('mid' => 'value', 'mname' => 'value', 'mlastname' => 'value'); I got above code working. But then I realize, I need to store array of second array. Each user has multiple members. I tried to run following code, but it stores only the last "mid, mname" value from while loop? How to add array of array in already started session? $smid = 0; // to give different index type value //Fetch and print all records: while($row = mysqli_fetch_row($r)) // from query run to fetch members under a user { // list out member names in a horizontal row: echo '<tr class="rowA">'; echo '<td><a href="addbook9.php?id='. $row[1] .'&nm='. $row[2] .'"> '. ucfirst($row[2]) .'</a></td>'; //don't want to send id and name in url //$mname = $row[1]; $mid = 'mid' . $smid; // this will give value like mid0, mid1.... $mname = 'mname' . $smid; $_SESSION['members'] = array($mid => $row[1], $mname=>$row[2]); //want to store id, name in session to use later $smid++; echo '<td><a href="view_books.php?id='. $row[1] .'&nm='. $row[2] .'">' . $row[3] . '</td>'; echo '</tr>'; } //End of while loop. echo '</table>';
  7. Larry has explained about write and writeln methods in Ch. 9, on page 334. I'm just copying those words here. "Still there is one appropriate situation for write() or writeln(): to dynamically include extra resources under certain conditions. For example, ads and third-party scripts........" @Larry, After reading first part of chapter 9, I decided to read the book completely as my first priority, because it is the best resource and I need to grasp those info there before I try to complete my course assignments.
  8. When I used "document.write(document.forms.name);" in my code earlier, it was only printing the first form from the document, leaving every other form in dark. Then I come to know this "write" function wipes out values after page loads. So I used "console.log " method to print what I need to know.
  9. I just got it write. I should use console.log instead of document.write. And it seems the code I pasted above has all gone unformatted.
  10. I understand your point. As we get reference to all links in a document, we can get reference to other objects in the document. So I'm trying to get all forms in a doc. But following code gives just first form, and then says document.forms is undefined. function getForms() { //document.write("in fun"); totalForms = document.getElementsByTagName('form'); formsLength = document.forms.length; for(i=0; i<formslength; i++)="" {="" document.write(document.forms.name);="" }="" document.write("formslength="" :="" "="" +="" formslength);="" }<="" code=""> </formslength;>
  11. I surely agree what you say. You show me how to handle all and any links in the document. The problem was I was just thinking on links when I had to give some thought on the other function of putting text. But, I got it now and come up with following code, which works as my exercise wants. I'm just pasting the putText function. function putText() { msg1 = "clap clap"; msg2 = "sour puss"; if(this.id == 'yes') { document.forms[0].ftxt.value = msg1; } else { document.forms[0].ftxt.value = msg2; } } //End of functin putText(). Thanks a lot for helping.
  12. Thanks again. I did understand what you said and did exactly as your suggestion. But I have to output two different values at the same text input for two different links. With your code too I can get only one output. <input type="text" id="ftxt" name="ftxt" value="Are you happy?"> </form> <a href="#">YEs</a> <a href="#">No</a> <script type="text/Javascript"> var aLinks = document.getElementsByTagName('a'); for(var i=0, aLinksLen = aLinks.length; i<aLinksLen; i++) { aLinks.onmouseover = putText; } function putText() { msg1 = "clap clap"; msg2 = "sour puss"; document.forms[0].ftxt.value = msg1; document.forms[0].ftxt.value = msg2; //this line overwrites first msg. If I don't keep it here, then it updates only first msg. }
  13. Yes, may be this is related to Referencing the event. Thanks for putting out all in code. But my text input box updates different variable values, and not what is there in this.innerHTML. My problem is like : If link1 is mouseover then update text input with message1 or if link2 is mouseover then update same text input with message2. In checkbox example we can know which checkbox is checked by looking at "checkbox.checked" value. But how do I get hold of which link is mouseovered at this point?
  14. I have a form with one text input, with an initial value of "Are you happy?" That document have two links, named "YES" and "NO". I need to add onMouseover events to these links that can change form's text input value: When user moves over "YES" the text input shows "clap clap".. When user moves mouse over "NO", the text input shows "so sad". (May be this is not a good example, but ...) I followed examples of chapter 8. And come up with following codes: Including only parts of the code: I have modified utilities.js file for the following function. Rest two functions are same as in the book's example. setText: function(id, message) { 'use strict'; if((typeof id == 'string') && (typeof message == 'string')) { var output = this.$(id); if(!output) return false; if(output.value !== undefined) { output.value = message; } else { output.value = message; } return true; } //End of main IF }, //End of setText() fn. Other js file, to call eventhandler and updatetext function: function updateText() { 'use strict'; var msg1 = "Clap Calp!!"; var msg2 = "Sour puss!"; U.setText('ftxt', msg1); //ftxt is the form's text input id from html file. U.setText('ftxt', msg2); } function init() { 'use strict'; U.addEvent(U.$('link1'), 'mouseover', updateText); //link1 is the link id from html file. U.addEvent(U.$('link2'), 'mouseover', updateText); } //End of init() fn. U.addEvent(window, 'load', init); These both files are included in html file. When I run the file, it sets "Sour puss" value once the mouse over event occurs on any one of the link. After that, the text input value is not updated at all.
  15. May be I'm doing something wrong. But when I add 'something', like this it gives error: undefined index "something".. It doesn't simply add that new array name, as session's new element..
  16. I tried to do as HartleySan suggested. But, as I'm trying more and more with SESSION, it turns out that SESSION is an associate array, but special kind of. It needs special handling. I understand this now, but still I'm unable to figure out(or search online) how to make/use a SESSION variable for this kind of work: My login page assigns values to SESSION like this : $_SESSION['user_id']="9999"; $_SESSION['lname']="kkkkk" . And after some activity I want to store another array of information for this user. I want a SESSION like this: $_SESSION("user_id", "name", array("mid", mname, mlastname))... How to add that array? Or how to start a SESSION like this so it can take in array later on?
  17. I read the first chapter of the book in one sitting. It was a easy reading, as if I was reading a nice story. Still I got the main points about those important DOCTYPE and impressive HTML5 right into my head. I was not giving any thought so far about HTML tags and DOCTYPE since now, but now I know them right. That's what I like about Larry's writing. Thank you, Larry. And one more thing, so far I have been able to keep the glossy, red book away from my toddler Son. The book looks so impressive and expensive! It's a good read...
  18. I was sending header with encoding type of utf8 in header file. And that was the reason, of "headers already sent .." error. I found following info on php.net.. and it solved my problem. when the encoding of your script file needs to send some headers just after your script starts to execute, this happens mostly with the scripts using normal utf8 encoding. To overcome the issue, use utf8(without BOM) encoding provided by notepad++ and most modern editors. Using utf8 encoding and cookie based sessions, will result in headers already sent by error.
  19. I am trying to write a contact form. To do so, I have parted form in contactform.html and posted it to email.php. I'm using modularizing concept to make the site. There is a configuration file, header and footer files and modules. header file includes encoding using header() and starts session. I'm including a link to contactform.html in index.php. Clicking that link loads contactform.html nicely. Once that form is filled and submitted, it shows error "headers already sent - output started at .. email.php. (email is sent out correctly.) I have not included configer and header,footer file in contactform.html. I have included configer and header,footer file in email.php. Now I know, that we cannot include session start and header function, two times like this. Header file has actually started the session already as the error says. But, if I omit header and footer file from email.php, I lose all the formatting. And if I keep those files, it gives error of headers already sent. The script runs well on my local machine. And the above problem comes on the live site. contactfrom.html is loading nicely in content area with all the formats, but email.php is not loading into content area. I think this is the problem. But how to solve it?
  20. Safari books online has following link to preview. Seems like only first paras are shown on most of the pages. But clicking on Chapter 1 link from left sidebar shows good content. I have also ordered the book, and waiting eagerly. oh, well, this link seems to be broken. I have bookmarked it in my browser, so I can see it. But seems like pasting it here does not work. However it was there on Safari books preview, few weeks back. (I would like to delete my post, but can't undo now). Sorry....
  21. I'm trying to work with php Sessions. My login script assigns some values to the session array. Now, after user logs in and goes further, I want to add other variables in that session. How can I add other values to an already started session? Is it possible?
  22. Thank you very much, Dark Prince and Antonio for your replies. However, as php.net documentation, mysqli_affected_rows function only works with queries which update a table INSERT, UPDATE, or DELETE query.. In order to get the number of rows from a SELECT query, usemysqli_stmt_num_rows() instead. http://php.net/manual/en/mysqli-stmt.affected-rows.php I worked on mysqli_stmt_num_rows: Changed my code to remove //fetch results: mysqli_stmt_fetch($stmt4) so, my script is working as I intended it do. Still there may be better ways to do what I'm asking. Well, thanks again..
  23. I am searching around for many days to work with SELECT prepared statements. I can't figure out, how to handle the situation when the SELECT statement in a scenario does not return any result. For example, $q4 = "SELECT user_id, book_id from user_book_trn where user_id = {$_SESSION['user_id']} and book_id= ?"; //It is possible that this query returns 0 results. In this case, mysli_prepare stmt does not work (or returns FALSE). In simple querys, we used to check result with mysqli_num_rows(query result). Is there anything like that here? If the query runs right, then I want to display results, or else I need to insert a record in a table. //Prepare the statement: if($stmt4 = mysqli_prepare($dbc, $q4)) { echo "Working for stmt4"; //bind variable to query's placeholder: mysqli_stmt_bind_param($stmt4,'i', $mid); //Execute query: mysqli_stmt_execute($stmt4); //Bind resutls: mysqli_stmt_bind_result($stmt4, $uid, $bkid); //fetch results: mysqli_stmt_fetch($stmt4); mysqli_stmt_store_result($stmt4); if(mysqli_stmt_num_rows($stmt4) == 0) //The flow does not reach here, if the query fails to return rows. //insert record. }
×
×
  • Create New...