Jump to content

iwato

Members
  • Content count

    780
  • Joined

  • Last visited

  • Days Won

    1

iwato last won the day on June 10

iwato had the most liked content!

Community Reputation

6 Neutral

1 Follower

About iwato

  • Rank
    Invested Member

Contact Methods

  • AIM
    moogoonghwa
  • Website URL
    http://www.grammarcaptive.com
  • Yahoo
    iddor
  • Skype
    kiusau

Profile Information

  • Gender
    Male
  • Location
    Seattle, Washington USA 98104

Previous Fields

  • Languages
    HTML, CSS, Javascript, PHP, MySQL and Spoken Language

Recent Profile Visitors

9,116 profile views
  1. Manipulating the $.ajax( ) data Parameter

    So, how do I access the XHR object? XHR.data.podType? Will this get me the value of podType in the success function? Roddy
  2. BACKGROUND: I have successfully integrated both pagination and the simplePagination paginator into my website. Now I must fine tune the integration so as to overcome several associated non-optimal conditions. In order to achieve it appears that I need to understand better the use of the variables and functions utilized with the AJAX request. Please consider the following pieces of code and answer the questions below. The JAVASCRIPT: .click(function() { $.ajax({ url: '_utilities/php/ajax_data_dscr.php', dataType: 'JSON', data: { 'podType': podType }, statusCode: { 404: function() { alert( "Page not found" ); }}, success: function (jsonData){ console.log(jsonData) $('#main').html(''); $.each(jsonData, function(key, object){ $('#main').append("<div class='paginate'><div class='item_info podcast_item'><div class='pod_num'>" + object.podcast_no_item + "</div><div class='pod_title'>" + object.item_title + "<br />" + object.item_pubdate + "</div></div><div class='pod_describe'>" + object.item_description + "</div></div>"); }); var collection = $(".paginate"); var collNum = collection.length; var perPage = 10; collection.slice(perPage).hide(); $("#pagn").pagination({ items: collNum, itemsOnPage: perPage, cssStyle: "light-theme", onPageClick: function(pageNum) { var start = perPage * (pageNum - 1); var end = start + perPage; // console.log(event); // console.log(pageNum); collection.hide().slice(start, end).show(); } }); } The HTML: <div id='main'>...</div><!-- end div#main --> <div id='pagn'>&nbsp;</div><!-- end div#pagn --> BACKGROUND: From the above code you can probably notice that the #main DIV is emptied before it is dynamically filled via jQuery's .append() method. The heading of the newly filled #main DIV should vary with the kind of information that is entered. To this end I created the following switch. Unfortunately, however, I cannot get it to work. Either the value of the podType variable is not being read, or the code itself has not been properly set into the AJAX routine. The following is an example of one of my attempts. beforeSend: function(podType) { $('#main').html(''); switch (podType) { case clausal: $('#main').html('<h1>Clausal Analysis</h1>'); break; case linear: $('#main').html('<h1>Linear Analysis</h1>'); break; case inversion: $('#main').html('<h1>Socratic Inversion</h1>'); break; case chrono: $('#main').html('<h1>All Categories</h1>'); break; } } QUESTION ONE: How do I access the key-value pair entered as the value of the data property? QUESTION TWO: How would you go about insuring that a new list heading is entered with each different value of podType. NOTE: You can visit the page by clicking on the words PROXY LINK at the bottom of http://www.grammarcaptive.com/sender_proxy.php In the navigation bar on the page that opens find the word Chronology, click on it, and then click on the phrase Podcast index ... This will show you a successful implementation of simplePagination and provide you with a visual understanding of what I am able and not able to achieve. Roddy
  3. JSON question - simple

    Hi, i am sending along a copy of a JSON object that I created from a PHP array using a PHP method called json_encode(). This is what it looks like. [ {"podcast_no_item":54,"item_title":"Experiment Two","item_description":"Test","item_pubdate":"2017-06-18 10:31:48"}, {"podcast_no_item":54,"item_title":"Experiment Two","item_description":"Test","item_pubdate":"2017-06-18 10:31:48"}, {"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-18 11:10:56"}, {"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-18 11:10:57"}, {"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-19 08:43:49"}, {"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-19 08:43:49"}, {"podcast_no_item":57,"item_title":"Eureka_3","item_description":"I have found it.","item_pubdate":"2017-09-24 11:03:50"}, {"podcast_no_item":58,"item_title":"Is it ready?","item_description":"It sure looks like it.","item_pubdate":"2017-06-19 15:44:59"}, {"podcast_no_item":58,"item_title":"Is it ready?","item_description":"It sure looks like it.","item_pubdate":"2017-06-19 17:21:11"} ] I have included spaces and tabs so that it is easy for you to read. You can think of a JSON object as one very long array element whose sub-elements consist of a series of objects with property-value pairs. For example, the above JSON object contains nine objects with four property-value pairs each. I am not at all sure that the extension on your file is important. I have seen JSON contained in several different file formats. In any case, make sure that you do not place a comma after the last object in the array element. Also, if you would like to assign the object to a variable as a string so that you can pass it with a $_GET or $_POST HTTP request, make sure that you enclose it in quotation marks and understand that your JSON object is now a JSON string and must be converted back to an object after it has been sent and received. Sometimes this is done automatically. Sometimes it is not in which case you can use JSON.parse(). Roddy
  4. The problem has been resolved by echoing a JSON string produced with json_encode( ) and eliminating the JSON.parse function in the success function. Apparently the new version of $.ajax( ) parses incoming data strings automatically. Try it and see: You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php
  5. Good morning! Firstly, I receive a parse error when I echo the PHP variable $json_data without the Javascript. In contrast, I receive no error message when I use the Javascript. I believe now that the problem is with the success function. Find below the two key functions that together produce no error, but still do not produced the expected result -- namely, the alert and a value in the console window. <script> (function jsonOutput() { var jsonData = '<?php echo $json_data; ?>'; jsonData = JSON.stringify(jsonData); return jsonData; })() </script> and success: function(jsonData){ var data_array = JSON.parse(jsonData); document.console && console.log(data_array); alert(data_array[0]['podcast_no_item']); } and still again, [ [60,"New Title","Test","2017-06-19 17:26:41"], [51,"The First Tri-Lateral Attempt","Having rearranged the PHP code, the MySQL database will not be updated until all mandatory elements have been completed and the submit button is depressed.","2017-06-18 09:50:37"], [52,"Cooperation","New description","2017-09-24 11:00:17"], [52,"New Title","New description","2017-06-18 09:56:36"], [53,"Experiment One","Test","2017-06-18 10:24:49"], [54,"Experiment Two","Test","2017-06-18 10:31:48"], [54,"Experiment Two","Test","2017-06-18 10:31:48"], [55,"Experiment Three","Test","2017-06-18 11:10:56"], [55,"Experiment Three","Test","2017-06-18 11:10:57"], [55,"Experiment Three","Test","2017-06-19 08:43:49"], [55,"Experiment Three","Test","2017-06-19 08:43:49"], [57,"Eureka_3","I have found it.","2017-09-24 11:03:50"], [58,"Is it ready?","It sure looks like it.","2017-06-19 15:44:59"], [58,"Is it ready?","It sure looks like it.","2017-06-19 17:21:11"], [60,"New Title","Test","2017-06-19 17:24:23"], [60,"New Title","Test","2017-06-19 17:26:41"], [60,"New Title","Test","2017-08-18 08:21:45"], [63,"Test","Test","2017-08-18 08:21:27"], [64,"Podcast #64","Practice Podcast","2017-08-18 08:21:04"], [65,"Transition Without End","There is a time for everything including a time to end.","2017-08-18 08:20:42"], [60,"New Title","Test","2017-06-19 17:26:41"] ] And, still again, if there is doubt or a need for further experimentation. You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php
  6. As before you are welcome to view the entire procedure at You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php
  7. Although I am happy to report that I have managed to rid myself of the Object: object and am likely now in communication with the data file, I am still unable to achieve the desired result -- namely, the Javascript alert ( ) and more positive feedback from the introduction of the console.log( ) method. I believe that there were several coding mistakes that were bringing about the original error message including a missing parenthesis in the condition of the data file's if-statement and my treatment of the $.getJSON arguments. I have switched to the $.ajax( ) method, as its format provides me with greater flexibility and a clearer picture of what I am actually trying to achieve. Further, I have move the Javascript outside of the PHP routine in the requested data file. In addition, I have checked to insure that the desired JSON object actually exists and have displayed the result below. The check was made by turning off the $_GET request and running a simple echo statement of the result. In addition, please find below the PHP file that generates the JSON string and the AJAX triggered by a click function using the $.ready( ) method. <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); if(isset($_GET['podType'])) { define('_HOST_NAME','...'); define('_DATABASE_NAME','...'); define('_DATABASE_USER_NAME','...'); define('_DATABASE_PASSWORD','...'); $mysqli_obj = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME); if($mysqli_obj->connect_errno) { die("ERROR : -> ".$mysqli_obj->connect_error); } $tbl_name = 'rss2_podcast_item'; $podcast_items = []; $result_obj = $mysqli_obj->query("SELECT * FROM " . $tbl_name); while($row = $result_obj->fetch_assoc()) { foreach($row as $key => $value) { $item_arr[$key] = $value; } $items[] = $item_arr; } //Creates a nested array whose subarrays consist of the four indicated elements. foreach ($items as $sub_arr => $element) { $podcast_item[] = $element['podcast_no_item']; $podcast_item[] = $element['item_title']; $podcast_item[] = $element['item_description']; $podcast_item[] = $element['item_pubdate']; $podcast_items[] = $podcast_item; $podcast_item = []; } $json_data = json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); } ?> <script> (function jsonOutput() { var jsonData = '<?php echo $json_data; ?>'; jsonData = JSON.stringify(jsonData); return jsonData; })() </script> .click(function() { $.ajax({ url: '../exp1/ajax_data.php', dataType: 'JSON', data: {'podType': podType}, success: function(jsonData){ var data_array = $.parseJSON(jsonData); document.console && console.log(data_array); alert(data_array[0]['podcast_no_item']); } }); }) [ [60,"New Title","Test","2017-06-19 17:26:41"], [51,"The First Tri-Lateral Attempt","Having rearranged the PHP code, the MySQL database will not be updated until all mandatory elements have been completed and the submit button is depressed.","2017-06-18 09:50:37"], [52,"Cooperation","New description","2017-09-24 11:00:17"], [52,"New Title","New description","2017-06-18 09:56:36"], [53,"Experiment One","Test","2017-06-18 10:24:49"], [54,"Experiment Two","Test","2017-06-18 10:31:48"], [54,"Experiment Two","Test","2017-06-18 10:31:48"], [55,"Experiment Three","Test","2017-06-18 11:10:56"], [55,"Experiment Three","Test","2017-06-18 11:10:57"], [55,"Experiment Three","Test","2017-06-19 08:43:49"], [55,"Experiment Three","Test","2017-06-19 08:43:49"], [57,"Eureka_3","I have found it.","2017-09-24 11:03:50"], [58,"Is it ready?","It sure looks like it.","2017-06-19 15:44:59"], [58,"Is it ready?","It sure looks like it.","2017-06-19 17:21:11"], [60,"New Title","Test","2017-06-19 17:24:23"], [60,"New Title","Test","2017-06-19 17:26:41"], [60,"New Title","Test","2017-08-18 08:21:45"], [63,"Test","Test","2017-08-18 08:21:27"], [64,"Podcast #64","Practice Podcast","2017-08-18 08:21:04"], [65,"Transition Without End","There is a time for everything including a time to end.","2017-08-18 08:20:42"], [60,"New Title","Test","2017-06-19 17:26:41"] ] I look forward to a speedy remedy of my ignorance. Finally, I have replaced the deprecated jQuery .parseJSON() function with the Javascript JSON.parse() function. Roddy
  8. I even found a missing parenthesis in the condition expression of the if-statement and fixed that. Still, the error message remains the same.
  9. Yes, I have cleared the cache. You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php Find the <div> entitled Navigation Bar and click on any of the menu items. Each will send a different value for podType, but the if condition that controls the PHP in the data file does not care what it receive, just so long as it is something. Roddy
  10. I also tried this <?php ... ?> <script> (function jsonOutput() { var jsonData = '<?php echo $json_data; ?>'; jsonData = JSON.stringify(jsonData); return jsonData; })() </script> but still, the error message and associated link remain unchanged.
  11. 1) No. I am not building the URL, I am simply entering it as a relative path. 2) Also, because $.getJSON() appears to be a special case of $.ajax(), I should think that it follows the same rules for the data parameter. This is what it says for the parameter for $.ajax(). So, I believe you are correct about the reformatting of the value that I enter for the data parameter. 3) You wrote: Yes, but how to I output the JSON string. Originally, I did it as a PHP echo statement. I understand far too poorly what $.ajax() does when the dataType is 'JSON'. I changed the data parameter to read $.getJSON({ url: '../exp1/ajax_data.php', data: {'podType': podType}, success: function(jsonData){ var data_array = $.parseJSON(jsonData); alert(data_array[0]['podcast_no_item']); } }); and the data file to read <?php if(isset($_GET['podType']) { define('_HOST_NAME','baha.lunarbreeze.com'); define('_DATABASE_NAME','thege0_grammarcaptive'); define('_DATABASE_USER_NAME','thege0'); define('_DATABASE_PASSWORD','Liberty2015'); $mysqli_obj = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME); if($mysqli_obj->connect_errno) { die("ERROR : -> ".$mysqli_obj->connect_error); } $tbl_name = 'rss2_podcast_item'; $podcast_items = []; $result_obj = $mysqli_obj->query("SELECT * FROM " . $tbl_name); while($row = $result_obj->fetch_assoc()) { foreach($row as $key => $value) { $item_arr[$key] = $value; } $items[] = $item_arr; } //Creates a nested array whose subarrays consist of the four indicated elements. foreach ($items as $sub_arr => $element) { $podcast_item[] = $element['podcast_no_item']; $podcast_item[] = $element['item_title']; $podcast_item[] = $element['item_description']; $podcast_item[] = $element['item_pubdate']; $podcast_items[] = $podcast_item; $podcast_item = []; } $json_data = json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); } ?> <script> var jsonData = '<?php echo $json_data; ?>'; jsonData = JSON.stringify(jsonData); </script>
  12. This is all that I am doing. $.getJSON({ url: "../exp1/ajax_data.php", data: podType, success: function(json_data){ var data_array = $.parseJSON(json_data); alert(data_array[0]['podcast_no_item']); } }); I read this in simple English as, "Send an HTTP request with the value of podType to '../exp1/ajax_data.php'. Discover a variable called json_data in ajax_data.php" . Read the value of json_data into the success call function for further processing. Convert the JSON string into a JSON object and extract from it the value of the first element of the first subarray of the Javascript array just created. This too fails. Is this the source of the problem? Must this be stringified before it is sent back to the requesting file? [[60,"New Title","Test","2017-06-19 17:26:41"],[51,"The First Tri-Lateral Attempt","Having rearranged the PHP code, the MySQL database will not be updated until all mandatory elements have been completed and the submit button is depressed.","2017-06-18 09:50:37"],[52,"Cooperation","New description","2017-09-24 11:00:17"],[52,"New Title","New description","2017-06-18 09:56:36"],[53,"Experiment One","Test","2017-06-18 10:24:49"],[54,"Experiment Two","Test","2017-06-18 10:31:48"],[54,"Experiment Two","Test","2017-06-18 10:31:48"],[55,"Experiment Three","Test","2017-06-18 11:10:56"],[55,"Experiment Three","Test","2017-06-18 11:10:57"],[55,"Experiment Three","Test","2017-06-19 08:43:49"],[55,"Experiment Three","Test","2017-06-19 08:43:49"],[57,"Eureka_3","I have found it.","2017-09-24 11:03:50"],[58,"Is it ready?","It sure looks like it.","2017-06-19 15:44:59"],[58,"Is it ready?","It sure looks like it.","2017-06-19 17:21:11"],[60,"New Title","Test","2017-06-19 17:24:23"],[60,"New Title","Test","2017-06-19 17:26:41"],[60,"New Title","Test","2017-08-18 08:21:45"],[63,"Test","Test","2017-08-18 08:21:27"],[64,"Podcast #64","Practice Podcast","2017-08-18 08:21:04"],[65,"Transition Without End","There is a time for everything including a time to end.","2017-08-18 08:20:42"],[60,"New Title","Test","2017-06-19 17:26:41"]]
  13. Are you suggesting that the PHP echo statement produces an object? I just replaced the following with the code that follows. FOLLOWING CODE echo json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); CODE THAT FOLLOWS <?php ... $json_data = json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); ?> <script> var json_data = '<?php echo $json_data; ?>'; </script> <?php Still no change: the alert does not appear and the error message along with the accompanying URL remain the same. Roddy
  14. OK. Here are the two paths: ... /experimentation/exp2/podcast_index.php .../experimentation/exp1/ajax_data.php The value of the URL property of the AJAX object in podcast_index.php is url: "../exp1/ajax_data.php", I have checked the spelling of each folder and file. I find nothing wrong. Any other ideas? This is what the Javascript Console returns as part of the error message. http://www.grammarcaptive.com/podcasting/experimentation/exp2/[object Object] Is $.getJSON looking for a JSON object as the value of a defined variable, when indeed, I am only echoing the JSON string? If this is the problem what form should the variable take? Can I use a PHP variable? Must it be a Javascript variable? How should I set the datatype of the $.ajax() or $.getJSON() PlainObject to correspond? Truly, I do not know how AJAX retrieves its desired information. Does it scan the entire page looking for a variable name? Does it look for some sort of page output? Or, does it simply read everything of the designated data type regardless if it is needed? None of the literature that i have read tells what truly happens once the designated URL has been accessed.
  15. BACKGROUND: I have created a PHP routine that gathers data from a MySQL database. The data that it gathers depends on the value of an HTTP request that uses the $_GET superglobal as its transfer mechanism. The routine further organizes the retrieved information into a nested array such that each element of the array corresponds to a different table row, and each element of each nested array corresponds to a subset of the table fields. Once the nested array has been completed the json_encode() function converts the array into a JSON string and readies it for transport. The PHP <?php if(isset($_GET['podType']) { define('_HOST_NAME','...'); define('_DATABASE_NAME','...'); define('_DATABASE_USER_NAME','...'); define('_DATABASE_PASSWORD','...'); $mysqli_obj = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME); if($mysqli_obj->connect_errno) { die("ERROR : -> ".$mysqli_obj->connect_error); } $tbl_name = 'rss2_podcast_item'; $podcast_items = []; $result_obj = $mysqli_obj->query("SELECT * FROM " . $tbl_name); while($row = $result_obj->fetch_assoc()) { foreach($row as $key => $value) { $item_arr[$key] = $value; } $items[] = $item_arr; } //Creates a nested array whose subarrays consist of the four indicated elements. foreach ($items as $sub_arr => $element) { $podcast_item[] = $element['podcast_no_item']; $podcast_item[] = $element['item_title']; $podcast_item[] = $element['item_description']; $podcast_item[] = $element['item_pubdate']; $podcast_items[] = $podcast_item; $podcast_item = []; } echo json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); } ?> The Javascript <script> $(document).ready(function() { $('li.podDex').each(function(){ var podType = $(this).attr('id'); //Causes the weight of the font to change when the mouse passes over the text. $('#'+podType).mouseover(function() { $(this).css({"cursor": "pointer", "font-weight":"800"}); }) .mouseout(function() { $(this).css("font-weight", "normal"); }); //Causes the weight of the font to change, displays the hidden text to appear in the #main <div> element, and brings about a change in color. $('#'+podType).mouseover(function() { $(this).css({"cursor": "pointer", "font-weight":"800"}); }) .click(function() { $.getJSON({ url: "../exp1/ajax_data.php", data: podType, success: function(json_data){ var data_array = $.parseJSON(json_data); alert(data_array[0]['podcast_no_item']); } }); }) .mouseup(function() { $(this).css({"color": "#fadb9d","font-weight": "normal"}); $('body, html').animate({scrollTop: $('#main').offset().top},800); }); }); }); </script> The HTML <ul> <li id='linear' class='podDex'>Linear Analysis</li> <li id='clausal' class='podDex'>Clausal Analysis</li> <li id='inversion' class='podDex'>Socratic Inversion</li> <li id='chronology' class='podDex'>Chronology</li> </ul> The Console Error Message is You are welcome to try it yourself. Please go to Grammar Captive and click on any of the four designated <li> elements. QUESTION: What might be causing the $.getJSON() call to fail? NOTE: A similar, but not identical, result occurs on my local test server.
×