473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Implementing data filter with AJAX

Hello Ajaxxer

ich created a .json file with data records from which I want to extract data records per user request.
I created 2 ajax components: one for getting user data and one for getting external json data. Now I cannot join both. Can you help me out here ?

- here is the code for the html input form:
Expand|Select|Wrap|Line Numbers
  1. - hier ist der HTML FORM Code zu User Input:
  2.   <form id="myform" name="myform" enctype="multipart/form-data" action="register1.php" method="post" onsubmit="AJAXSubmit(this); return false;">
  3.     <label>RS1
  4.     <select name="rs">
  5.       <option value="red" selected>für RW</option> 
  6.       <option value="white">für WW</option>
  7.     </select>    
  8.     </label>
  9.     <label>Region
  10.     <select name="region">
  11.       <option value="region1" selected>REG A</option> 
  12.       <option value="region2">REG A</option>
  13.       <option value="region3">REG A</option>
  14.       <option value="region4">REG A</option>
  15.     </select>    
  16.     </label>
  17.        <input id="bg" type="checkbox" name="bg" value="1"/> <label for="bg">bg</label>
  18.       <input id="submit" type="submit" value="submit">
  19.     </fieldset>
  20. </form>
  21.  
- I use the AJAX Form Submit Framework ::
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest

Expand|Select|Wrap|Line Numbers
  1. var AJAXSubmit = (function () {
  2.  
  3.   function ajaxSuccess () {
  4. //    var jsonObj = JSON.parse(data_file);
  5. //    document.getElementById("id01").innerHTML = this.responseText;
  6. loadJS(function(response) {
  7. // Do Something with the response e.g.
  8. jsonresponse = JSON.parse(response);
  9. // Assuming json data is wrapped in square brackets as Drew suggests
  10. document.getElementById("id01").innerHTML = jsonresponse;
  11. //console.log(jsonresponse[0].name);
  12.  
  13. });
  14. //  document.getElementById("id01").innerHTML = mydata[0].name;
  15.   }
  16.  
  17. // new  
  18. function loadJS(callback) {
  19.  
  20. var xobj = new XMLHttpRequest();
  21. xobj.overrideMimeType("application/json");
  22. xobj.open('GET', 'grapes1.json', true);
  23. xobj.onreadystatechange = function () {
  24. if (xobj.readyState == 4 && xobj.status == "200") {
  25. callback(xobj.responseText);
  26. }
  27. }
  28. xobj.send(null);
  29. }
  30.  
  31.   function submitData (oData) {
  32.     /* the AJAX request... */
  33.     var oAjaxReq = new XMLHttpRequest();
  34.     oAjaxReq.submittedData = oData;
  35.     oAjaxReq.onload = ajaxSuccess;
  36.     if (oData.technique === 0) {
  37.       /* method is GET */
  38.       oAjaxReq.open("get", oData.receiver.replace(/(?:\?.*)?$/, oData.segments.length > 0 ? "?" + oData.segments.join("&") : ""), true);
  39.       oAjaxReq.send(null);
  40.     } else {
  41.       /* method is POST */
  42.       oAjaxReq.open("post", oData.receiver, true);
  43.       if (oData.technique === 3) {
  44.         /* enctype is multipart/form-data */
  45.         var sBoundary = "---------------------------" + Date.now().toString(16);
  46.         oAjaxReq.setRequestHeader("Content-Type", "multipart\/form-data; boundary=" + sBoundary);
  47.         oAjaxReq.sendAsBinary("--" + sBoundary + "\r\n" + oData.segments.join("--" + sBoundary + "\r\n") + "--" + sBoundary + "--\r\n");
  48.       } else {
  49.         /* enctype is application/x-www-form-urlencoded or text/plain */
  50.         oAjaxReq.setRequestHeader("Content-Type", oData.contentType);
  51.         oAjaxReq.send(oData.segments.join(oData.technique === 2 ? "\r\n" : "&"));
  52.       }
  53.     }
  54.   }
  55.  
  56.   function processStatus (oData) {
  57.     if (oData.status > 0) { return; }
  58.     /* the form is now totally serialized! do something before sending it to the server... */
  59.     /* doSomething(oData); */
  60.     /* console.log("AJAXSubmit - The form is now serialized. Submitting..."); */
  61.     submitData (oData);
  62.   }
  63.  
  64.   function pushSegment (oFREvt) {
  65.     this.owner.segments[this.segmentIdx] += oFREvt.target.result + "\r\n";
  66.     this.owner.status--;
  67.     processStatus(this.owner);
  68.   }
  69.  
  70.   function plainEscape (sText) {
  71.     /* how should I treat a text/plain form encoding? what characters are not allowed? this is what I suppose...: */
  72.     /* "4\3\7 - Einstein said E=mc2" ----> "4\\3\\7\ -\ Einstein\ said\ E\=mc2" */
  73.     return sText.replace(/[\s\=\\]/g, "\\$&");
  74.   }
  75.  
  76.   function SubmitRequest (oTarget) {
  77.     var nFile, sFieldType, oField, oSegmReq, oFile, bIsPost = oTarget.method.toLowerCase() === "post";
  78.     /* console.log("AJAXSubmit - Serializing form..."); */
  79.     this.contentType = bIsPost && oTarget.enctype ? oTarget.enctype : "application\/x-www-form-urlencoded";
  80.     this.technique = bIsPost ? this.contentType === "multipart\/form-data" ? 3 : this.contentType === "text\/plain" ? 2 : 1 : 0;
  81.     this.receiver = oTarget.action;
  82.     this.status = 0;
  83.     this.segments = [];
  84.     var fFilter = this.technique === 2 ? plainEscape : escape;
  85.     for (var nItem = 0; nItem < oTarget.elements.length; nItem++) {
  86.       oField = oTarget.elements[nItem];
  87.       if (!oField.hasAttribute("name")) { continue; }
  88.       sFieldType = oField.nodeName.toUpperCase() === "INPUT" ? oField.getAttribute("type").toUpperCase() : "TEXT";
  89.       if (sFieldType === "FILE" && oField.files.length > 0) {
  90.         if (this.technique === 3) {
  91.           /* enctype is multipart/form-data */
  92.           for (nFile = 0; nFile < oField.files.length; nFile++) {
  93.             oFile = oField.files[nFile];
  94.             oSegmReq = new FileReader();
  95.             /* (custom properties:) */
  96.             oSegmReq.segmentIdx = this.segments.length;
  97.             oSegmReq.owner = this;
  98.             /* (end of custom properties) */
  99.             oSegmReq.onload = pushSegment;
  100.             this.segments.push("Content-Disposition: form-data; name=\"" + oField.name + "\"; filename=\""+ oFile.name + "\"\r\nContent-Type: " + oFile.type + "\r\n\r\n");
  101.             this.status++;
  102.             oSegmReq.readAsBinaryString(oFile);
  103.           }
  104.         } else {
  105.           /* enctype is application/x-www-form-urlencoded or text/plain or method is GET: files will not be sent! */
  106.           for (nFile = 0; nFile < oField.files.length; this.segments.push(fFilter(oField.name) + "=" + fFilter(oField.files[nFile++].name)));
  107.         }
  108.       } else if ((sFieldType !== "RADIO" && sFieldType !== "CHECKBOX") || oField.checked) {
  109.         /* field type is not FILE or is FILE but is empty */
  110.         this.segments.push(
  111.           this.technique === 3 ? /* enctype is multipart/form-data */
  112.             "Content-Disposition: form-data; name=\"" + oField.name + "\"\r\n\r\n" + oField.value + "\r\n"
  113.           : /* enctype is application/x-www-form-urlencoded or text/plain or method is GET */
  114.             fFilter(oField.name) + "=" + fFilter(oField.value)
  115.         );
  116.       }
  117.     }
  118.     processStatus(this);
  119.   }
  120.  
  121.   return function (oFormElement) {
  122.     if (!oFormElement.action) { return; }
  123.     new SubmitRequest(oFormElement);
  124.   };
  125.  
  126. })();
  127.  
Thanks very much for your assistance
Frank
Jun 11 '15 #1
3 1496
Dormilich
8,658 Expert Mod 8TB
Now I cannot join both.
I only see a single AJAX code.
Jun 11 '15 #2
Hello Dormilich
I put in only the main AJAX function because that is the place where to insert the code; Which other code do you need?
Greetings, Frank
Jun 14 '15 #3
Dormilich
8,658 Expert Mod 8TB
you said something about 2 AJAX components, but posted only one.
Jun 14 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ivandimko | last post by:
Can someone help me make a dialog box in VB that can filter data? Example i have 3 Regions (US, EU, AP) and then different people under different regions, i need to filter with a dialog box only...
7
by: raknin | last post by:
I'm using AJAX on my website, but internet explorer does not seem to actually be refreshing the data I retrieve via AJAX when I refresh the page. For example, I have a button that when pressed uses...
4
abdoelmasry
by: abdoelmasry | last post by:
Hi Prof's im new in ajax i need some help i wanna create active web page linked to mysql database i mean .. i open my page , it will automatically view database contents
3
by: dmorand | last post by:
I have a page where I'm using ajax to retrieve some employee info when a user clicks a "Retrieve Employee Info" button. The issue I'm having is that when the user updates the employee info,...
3
by: eisman28 | last post by:
Hi, I need to implement a suitable data structure (in c++) for the following problem. Each state has x,y,z coordinates and a value function (x^2+y^2+z^2) Starting with the first state (x1,y1,z1)...
0
by: Colin Rodrigues | last post by:
I want to use ajax in jquery to get data for my page... The problem is that the url which i call has some query strings to be sent along with it... for example: the url which i call for getting...
2
by: heybobo | last post by:
Hi, my aim is to display the content of my json data i receive from my php file. php is sending : PHP---> $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
2
by: milatif19 | last post by:
I can get the text file data from Apache but not the xml file data. Both files are in the same place on C:\xampp\htdocs\mdata. The code for the xml data is this: <!DOCTYPE html> <html>...
2
by: harman30 | last post by:
Here in my code below I display posts in table that has 3 columns i am limiting posts so initially only two rows are visible now I want to add load more button which shows few more posts and then few...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.