SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Location
    Darlington, UK
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ColdFusion Ajax Proble

    OK I'm writing a simple CF MX7 app that passes the values of several drop downs through Ajax to a query which then returns a sting containing the drop down values.

    The submit form looks like this:

    Code:
    		<table>
    			
    				<tr>
    					<td>Colour:</td>
    					<td>
    						
    						<select name="vgvals" id="vgvals" onchange="javascript:autosuggest();">
    							
    								
    								<option value="12-31" >Red</option>
    							
    								
    								<option value="12-32" >Blue</option>
    							
    								
    								<option value="12-33" >Pink</option>
    							
    						</select>
    					</td>
    				</tr>
    			
    				<tr>
    					<td>Size:</td>
    					<td>
    						
    						<select name="vgvals" id="vgvals" onchange="javascript:autosuggest();"> 							
    								
    								<option value="13-34" >10</option>
    							
    								
    								<option value="13-35" >20</option>
    							
    						</select>
    					</td>
    				</tr>
    				
    		</table>
    Now what I want my Ajax script to do is take the selected value of drop down and run them against a query so I have this:
    Code:
    /* ---------------------------- */
    /* XMLHTTPRequest Enable         */
    /* ---------------------------- */
    function createObject() {
        var request_type;
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer"){
        request_type = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
            request_type = new XMLHttpRequest();
        }
            return request_type;
    }
    
    var http = createObject();
    
    /* -------------------------- */
    /* SEARCH                     */
    /* -------------------------- */
    function autosuggest() {
    q = document.getElementById('vgvals').value;
    // Set te random number to add to URL request
    nocache = Math.random();
    http.open('get', 'ajax/prodvarsearch.cfm?q='+q+'&nocache = '+nocache);
    http.onreadystatechange = autosuggestReply;
    http.send(null);
    }
    function autosuggestReply() {
    if(http.readyState == 4){
        var response = http.responseText;
        if(response!=""){
            document.getElementById('results').innerHTML=response;
            document.getElementById('results').style.display="block";
        } else {
            document.getElementById('results').style.display="none";
        }
    }
    }
    Basically what my CF query is doing is looking for matching record in the table using a string of all the drop down values so pseuodo code:

    select * from table where varstring = 12-31,13-34

    In other words a list of the select box values. I should mention there will always be at least one select box but there could also be several dependent on the page setup, so an undetermined number of select boxes.

    I'm sure there must be a way for me to build up my string of values for the query line but at the moment the Ajax code is only taking the first select value and ignoring the subsequent ones.

    Any pointers?
    Cheers,
    Phil


  2. #2
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've got a few things that appear to be wrong.

    1) Both your dropdowns have the same name, and id. But your fields appear to be different pieces of data (size and color).

    2) You're getting the value of vgvals, but that should only be returning ONE value, not all of them.

    3) Your SQL looks like it's trying to select integer values, but those would be string values. If it's any other character but a number or a - (for negative ints), then it's considered a varchar.

    4) It's poor database design to store strings as related data. Technically each record should contain only ONE corresponding value, but that's a different story.

    First suggestion is to give each select field a unique name and ID.
    Then you should get the value of each separately.
    Then, you should be passing each value through distinctly.
    Lastly, it's not a good idea

    Have you tried alert()ing your q value when the function fires to see
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •