Jump to content
Larry Ullman's Book Forums

sonal

Members
  • Posts

    121
  • Joined

  • Last visited

  • Days Won

    2

Everything posted by sonal

  1. Thanks for the reply, Antonio and Margaux. The problem is I'm using link in between 2 scripts. If I use a submit button, then it works fine. So it looks like the form is submitted by "submit" button, and not by a hyper link. ( I would like to know how to do that with a link, though)
  2. I have to pass selected checkbox value from one script to another. However, my checkbox selection never passes to the receiving page. My scripts are as follow: function chkBox($str) { echo "<div><input type='checkbox' name='member[]' value='$str'>" .ucfirst($str). "</div>"; } $q = "SELECT * from users"; //Run the query: $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if(mysqli_num_rows($r) > 0) { //Asimple message to show: //start a table with header row: echo '<table align="center" cellspacing="10" cellpadding="10" width="80%"><tr class="rowH"> <th><a href="newbookentry.php">Add Book for</a></th> // user will click this link to go to receiving script echo '<form action="newbookentry.php" method="post"> while($row = mysqli_fetch_row($r)) { // list out member names in a horizontal row: echo '<tr class="rowA">'; echo '<td>'; chkBox($row[2]); // next things } Second (receiving script): newbookentry.php //check if a checkbox is selected: if(isset($_POST['member']) && !empty($_POST['member'])) { $mems = $_POST['member']; //echo $mems; } else { $errors[] = 'Please select at least 1 member name.'; echo "Please select a member."; } // more This script always shows error message, even if I select single/multiple checkboxes on first page.
  3. Hi Antonio, I got it. Thank you.... I should have though of that .. well.. thanks! @Paul, thanks to you too. That one is also great to know and keep in mind...
  4. I'm trying to build a site with modularization concept described in the book. Like, index file contains header.html and footer.html. The header file starts session and it is included in other modules wherever required. I want to include login.php on the index.php. index.php includes header.html, which starts session. Now login.php also needs to get access to session. If I use a statement like session_start() at the beginning of login.php, then it gives me error "session already started in header.html"... And that is true, as it is included at the beginning of the index.php file. Same error appears if I include header.html in login.php.. What is the way around?
  5. I have following 3 files : The database connection ($dbc) never gets available inside the function. I tried to make database connection directly inside the datacheck.php file, then also it shows error: undefined variable dbc...I tried to print_r($dbc) inside the function. It also shows error: undefined variable dbc....When I print outside of the function, it shows object details with error 0. For testing purposes, I kept all files in the same folder. So I'm sure file paths are correct. dbconfig.php DEFINE ('DB_USER', 'xmxmx'); DEFINE ('DB_PASSWORD', 'mxmxmx'); DEFINE('DB_HOST', 'mmsmsms'); DEFINE('DB_NAME', 'mmxmxm'); //Make the connection: $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if(!$dbc) { trigger_error('Could not connect to MySQL: '. mysqli_connect_error()); } //IDentify MySql the encoding to be used. mysqli_set_charset($dbc, 'utf8_general_ci'); datacheck.php require_once('dbconfig.php'); function checkUser($uid) { $query = "SELECT * FROM users1 WHERE oauth_uid = '{$uid}'"; $result = mysqli_query($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysqli_error($dbc)); $row = mysqli_fetch_row($result); if(!empty($row)) { echo "USEr is present"; } return $row; } test.php require_once('datacheck.php'); $uid = '12345'; checkUser($uid);
  6. INSERT command with values like : {$curtain_list['track_pole']} INSERT INTO ................ VALUES ( '{$curtain_list['track_pole']}', ) //quote outside curly bracket Hope that's what the problem is about....
  7. I got it now. After searching through a lot about php and XML, I found out that I had made a simple mistake while making url request. Now I get the result as I want it. By reading all these, I think I got to learn a bit about SimpleXML. Thanks a lot, HartleySan. I think discussing things over here gets me different ways to think about a problem.
  8. Me too think that this is something simple, as there is an api. As there is a cross-domain restriction with AJAX, it is required to use a php proxy script to load an xml file provided through isbn db. That xml file then can be used to display data through javascript. So, I'm trying to load the xml file through a php script. As an example, I tried to search for "Modern JavaScript" on isbn db website, which returns 3 results. Pls. see below two different approaches I tried with my php script. The script with simple_xml_load_file provides only 3 results (as required). But that script shows xml syntex error. Another script with get_file_contents returns more than 50000 records in the given xml file. And because only top 10 results are displayed out of them, I never get "Modern JavaScript Develop and Design"... Script 1: header('Content-Type: text/xml'); echo '<?xml version="1.0" encoding="utf-8" standalone="yes" ?> <item>'; $stitle = $_GET['searchterm']; $url = "http://isbndb.com/api/books.xml? access_key=p;results=details,texts&index1=title&value1=$stitle"; $xmlData = @simplexml_load_file($url); print_r($xmlData); echo '</item>'; Script 2: $url = "http://isbndb.com/api/books.xml? access_key=amp;results=details,texts&index1=title&value1=$stitle"; $xml = file_get_contents($url); $xml = simplexml_load_string($xml); header("Content-Type: applicaton/xml"); print_r($xml); So, I want to use script 1, but don't know how to parse/encode coming xml file to convert special characters into xml entities.
  9. Following php script brings out data as required, but gives syntex error like XML Parsing Error: not well-formed and points to unexpected xml characters like '&'. (XML comes from isbn db api). How to deal with them? htmlspecialchars works with string, but simple_xml_load_file returns object. header('Content-Type: text/xml'); echo '<?xml version="1.0" encoding="utf-8" standalone="yes" ?> <item>'; $stitle = $_GET['searchterm']; $url = "http://isbndb.com/api/books.xml?access_key=mykey&results=details,texts&index1=title&value1=$stitle"; $xmlData = @simplexml_load_file($url); print_r($xmlData);
  10. I sure have that access key as I registered with isbn site, and using that api for few months now. I can pull and print required data with a book's isbn. (I have implemented this feature on my website: www.myreadinglog.net) My problem is how can I display data when I pull it for a book's title. Sometime, my query returns more than 5000 titles but I don't know how to display it and get the exact title I want. If I search for a title with "the" word, the query will return many titles starting with numbers... and so on.... I'm missing something somewhere.. HOwever, thanks again.. one more question: How to echo an xml document through php? $xml = file_get_contents($url); header("Content-Type: applicaton/xml"); echo $xml; //This statement prompts to download xml document. $xmlData = @simplexml_load_file($url); header("Content-Type: application/xml"); echo $xmlData; //These statements show syntex error in xml document.
  11. HartleySan, thanks again for quite useful input. However, I don't get details of how to use results page and mine that data. Another thing, I noticed is isbn's api returns results of required search term. But I don't think there is any way we can use it's search feature.
  12. Thanks a lot for taking time to write code for this. I want to display autocomplete list of book titles. The problem is, isbn db provides only 10 books list in one response. So, while a user starts typing a character, my script retrieves first 10 titles with that first character any where in the title. So, I never get the title that I want. I hope to get some work around with this problem...
  13. XML is from isbn db : http://isbndb.com/data-intro.html However, I simplified php script as follows: <?php $stitle = $_GET['searchterm']; //echo $stitle; $url = "http://isbndb.com/api/books.xml?access_key=mykey&results=details,texts&index1=title&value1=$stitle"; $xml = file_get_contents($url); header("Content-Type: applicaton/xml"); echo $xml; ?> It shows book titles, but the list comes up quite long, so I don't get the exact title.. Still need to work to get to the root. Thanks again...
  14. I am trying to use a php proxy script to display book titles from isbn db web service. Following ajax + javascript calls a php script which retrieves data from isbn db. PHP script gives this error: XML Parsing Error: not well-formed Location: http://localhost/readinglog/isbn3.php?searchterm=bible&submit=Search Line Number 1, Column 11:biblebible<?xml version="1.0" encoding="UTF-8"?> JavaScript code: var titlesArray = new Array(); function handleAjaxResponse(e) { 'use strict'; if(typeof e == 'undefined') e = window.event; var ajax = e.target || e.srcElement; //console.log(ajax); if(ajax.readyState == 4 ) { if ((ajax.status >=200 && ajax.status < 300) || ajax.status == 304) { console.log(ajax.responseXML); if(ajax.responseXML) { console.log(ajax.responseXML); var allBooks = ajax.responseXML.getElementsByTagName('BookData'); for(var i=0, count = allBooks.length; i<count; i++) { titlesArray = allBooks.getElementsByTagName('Title')[0].firstChild; console.log(titlesArray); } } } } } function sendValue() { var searchTerm = document.getElementById('searchTerm'); console.log(searchTerm.value); var ajax = getXMLHttpRequestObject(); ajax.onreadystatechange = handleAjaxResponse; ajax.open('GET', 'isbn3.php?searchterm=' + encodeURIComponent(searchTerm.value), true); //ajax.open('GET', './isbn3.php', true); ajax.send(null); // return false; } window.onload = function() { 'use strict'; document.getElementById('searchTerm').onkeyup = sendValue; } code for isbn3.php: if(isset($_GET['searchterm']) && is_string($_GET['searchterm'])) { //Hold the serachterm into a variable, and typecast it.: $isbnQuery =(string) $_GET['searchterm']; if($isbnQuery == NULL) { $error[] = "You forgot to enter ISBN. Please go back and correct the error."; } if(empty($error)) { $isbnData ="http://isbndb.com/api/books.xml?access_key=keyno&results=details,texts&index1=title&value1=$isbnQuery"; //$xmlData = @simplexml_load_file($isbnData); //or die("Invalid ISBN ERROR"); $xmlDoc = file_get_contents($isbnData); //echo '<?xml version="1.0" encoding="utf-8" standalone="yes">'; header("Content-Type: application/xml"); echo $xmlDoc; //Read the entire xml document in string. ?>
  15. Last question of pursue section from chap. 8. I'm trying to update membership.js script to understand event handling. I'have come up with following solution, it works fine, both for checking data and calculating cost. But, once that is done, the page is not enabled upon refresh. (Pressing refresh button on keyboard does not make the page active again.) What is the problem for that? And, I would also like to know, is this the right answer? I mean, is it what is called as delegating event handling? Just want to make sure......... I have used functions from utility file, to get reference to the form elements. ( those U.$() thing in the code). function showData() { 'use strict'; var type = U.$('type'); var years = U.$('years'); var cost = U.$('cost'); console.log(type.value); console.log(years.value); console.log(cost.value); } function disableButton() { 'use strict'; U.$('submit').disabled = true; } function calculate(e) { 'use strict'; if (typeof e == 'undefined') e = window.event; var cost; var type = U.$('type'); var years = U.$('years'); if(years && years.value) { years = parseInt(years.value, 10); } if(type && type.value && years && (years > 0)) { //console.log(type.value); //console.log(years.value); switch (type.value) { case 'basic' : cost = 10.00; break; case 'premium' : cost = 15.00; break; case 'gold' : cost = 20.00; break; } //End of switch. cost *= years; if(years > 1) { cost *= 0.80; } U.$('cost').value = '$' + cost.toFixed(2); U.$('submit').value = 'Submit your order'; U.addEvent(U.$('membership'), 'submit', showData); U.addEvent(U.$('submit'), 'click', disableButton); } else { U.$('cost').value = 'Please enter valid values.'; } if(e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } return false; } //End of calculate function. window.onload = function() { 'use strict'; U.addEvent(U.$('membership'), 'submit', calculate); U.addEvent(document, 'change', calculate); U.$('submit').disabled = false; U.$('submit').value = 'Calculate'; };
  16. Thanks for looking Larry. I edited my post to show that cookie is set properly with value in quotes.
  17. I'm trying to read multiple cookie values as per the example given in the Larry's new book, on page 362. I'm experiencing difficulties for slice function on step 6. This problem is particularly about the code in the book. I used code from w3schools example, and it works fine. But I in my college assignment, I have to use split and slice methods to read cookies. So I'm experimenting with the code given in the book. This book's code (simplified version) is like this: function getCookie(name) { var len = name.length; var cookies = document.cookie.split(';'); for(var i =0; count = cookies.length; i < count; i++) { var value = (cookies.slice(0,1) == ' ') ? cookies.slice(1) : cookies; if(value.slice(0,len) == name) { return cookies.split('=')[1]; } } return false; } In one of my problems, I'm setting 3 cookies in document.cookie: Like this: document.cookie = 'fName=son'; document.cookie = 'lName=pat'; document.cookie = 'email=sp@d.com' I can say that cookies get set as per given values. I check them in browsers cookie settings. everything works fine until I read first cookie (fName). But the example then never reads from second cookie. To read these cookies, I'm using statements like : COOKIE.getCookie('fName') //This returns right cookie value. COOKIE.getCookie('lName') //This never returns cookie value. I'm trying to figure out where is the problem using firebug console. It shows that second cookie name gets sliced off for one character less than its value. That is, if(value.slice(0, len) == name) // This sentence slice off value to len-1 characters. { return .... } firebug console response for console.log(value.slice(0,len)); inside the loop: fName lName=mol lNam email=pmol@mycompany.com emai
  18. Again, HartleySan you'r right. I had a typo error somewhere in ajax constructor, which was the problem. It took enough time of me to see that error. Second, Larry has explained ajax in great detail in his new JS book. And he is explaining it with good examples.
  19. I'm trying to work with ajax, and got stumped for very first example of testing a simple text file through ajax. My script in FireFox 11 says, "ajax is null". Network monitor in Firebug shows "0 requests" for url when I load my html file and click the button to show text file. In IE 9 says, "Automation server can't create object". I'm using IIS 7 as my web server, on Windows 7. It has to do something with this? I think FF, IE 9 and google Chrome supports ajax. Are there any options to disable/enable ajax support? I have enabled javascript in FF, and IE prompts whether to allow javascript.
  20. Replace assignment to output to "message" (where it is "number"). output.textContent = message; } else { output.innerText =message; } "tasks.length " stores number of tasks entered so far. It is then updated in "message" variable. And the "message" variable's value is printed through this conditional.
  21. So nice of you to share such fine thoughts. I'm trying to learn as much as I can as this web technology interests me too. I'll always remember your points as I keep practicing and playing around with different aspects of programming and languages. Thanks...
  22. Larry and HartleySan, Thanks a lot for taking time here. Yes, there was problem in HTML. I have input buttons, and they are no button elements. I read something about that and changed the above code like this: var buttonArray = document.getElementsByTagName('input'); console.log(buttonArray.length); for(var i = 0, count = buttonArray.length; i<count; i++){ if (buttonArray.type.toLowerCase() == "button"){ console.log(buttonArray.name); buttonArray.onclick = clickButton; } So, now I think that I also have to go back and learn, HTML!!!!!! When will I be able to get upgraded from beginner to "Professional?" (or atleast, "intermediate" ?)
  23. I think I'm moving in the circle. I got the name of the element by, var btName = target.name; "The nodeName property returns the name of the specified node." This sentence cause the confusion, but there is much more to this. "If the node is an element node, the nodeName property will return the tagname." If I know the tagname, I can get the name of the element. If I know the element name, I can reference it directly. oh......everything gets confusing..
×
×
  • Create New...