dashcode errors in custom JavaScript

Discussion in 'Web Design and Development' started by jarv, Jan 21, 2011.

  1. macrumors newbie

    Joined:
    Jan 17, 2011
    #1
    hi,

    I am getting several error from my javaScript I inserted into my javaScript page in DashCode

    Code:
    function ajaxFunction(){
    	var townRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		townRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			townRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				townRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	townRequest.onreadystatechange = function(){
    		if(townRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = townRequest.responseText;
    		}
    	}
    	var name = document.getElementById('name').value;
    	var county = document.getElementById('county').value;
    	var town = document.getElementById('town').value;
    	var queryString = "?name=" + name + "&county=" + county + "&town=" + town;
    	townRequest.open("GET", "http://www.mypubspace.com/dashtest/pubs.php" + queryString, true);
    	townRequest.send(null); 
    }
    

    this all works fine in my page:
    http://www.mypubspace.com/dashtest/order.html
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
  3. thread starter macrumors newbie

    Joined:
    Jan 17, 2011
    #3
    Hi,

    Here I have a HTML page which uses Ajax to GET data from a PHP page

    http://www.mypubspace.com/dashtest/order.html

    I would like to use this for an iPhone Application.
    here are the steps I have taken

    step 1: added JavaScript to my .js page in DashCode
    step 2: assigned behaviours to 2 buttons in DashCode
    step 3: tested and received error: 'ajaxDisplay' null is not an object

    here is my HTML/JS
    Code:
    <html>
    <body>
    
    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxFunction(){
    	var townRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		townRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			townRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				townRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	townRequest.onreadystatechange = function(){
    		if(townRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = townRequest.responseText;
    		}
    	}
    	var name = document.getElementById('name').value;
    	var county = document.getElementById('county').value;
    	var town = document.getElementById('town').value;
    	var queryString = "?name=" + name + "&county=" + county + "&town=" + town;
    	//Add the following line
    	townRequest.open("GET", "http://www.mypubspace.com/dashtest/townpubs.php" + queryString, true);
    	townRequest.send(null);
    }
    
    function countyFunction(){
    	var countyRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		countyRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			countyRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				countyRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	countyRequest.onreadystatechange = function(){
    		if(countyRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = countyRequest.responseText;
    		}
    	}
    	var name = document.getElementById('name').value;
    	var county = document.getElementById('county').value;
    	var town = document.getElementById('town').value;
    	var queryString = "?name=" + name + "&county=" + county + "&town=" + town;
    	//Add the following line
    	countyRequest.open("GET", "http://www.mypubspace.com/dashtest/countypubs.php" + queryString, true);
    	countyRequest.send(null);
    }
    
    
    
    function townlistFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxTownlist');
    			var county = document.getElementById('county').value;
    			var town = document.getElementById('town').value;
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	ajaxRequest.open("GET", "http://www.mypubspace.com/dashtest/town-select.php", true);
    	ajaxRequest.send(null); 
    }
    
    function countylistFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxCountylist');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	ajaxRequest.open("GET", "http://www.mypubspace.com/dashtest/county-select.php", true);
    	ajaxRequest.send(null); 
    }
    
    function MM_jumpMenu(targ,selObj,restore){ //v3.0
      eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
      if (restore) selObj.selectedIndex=0;
    }
    
    //-->
    </script>
    
    <a href="#" onClick="townlistFunction();">show towns list</a>
    <a href="#" onClick="countylistFunction();">show counties list</a>
    <form name='myForm'>
    <div id="ajaxTownlist"></div>
    <div id="ajaxCountylist"></div>
    <input type='hidden' id='name' />
    <input type='hidden' id='county' />
    <input type='hidden' id='town' />
    </form>
    <div id='ajaxDiv'></div>
    </body>
    </html>
    
    
    here is one of my PHP pages
    Code:
    <?php
    $dbhost = "xxx";
    $dbuser = "xxx";
    $dbpass = "xxx";
    $dbname = "xxx";
    	//Connect to MySQL Server
    mysql_connect($dbhost, $dbuser, $dbpass);
    	//Select Database
    mysql_select_db($dbname) or die(mysql_error());
    	// Retrieve data from Query String
    	var ajaxDisplay = document.getElementById('ajaxDiv');
    $town = $_GET['rsTown'];
    	// Escape User Input to help prevent SQL Injection
    
    $town = mysql_real_escape_string($town);
    //build query
    $query = "SELECT DISTINCT rsTown, COUNT(PUBID) As PubCount FROM pubs GROUP BY rsTown  ORDER BY rsTown ASC";
    $qry_result = mysql_query($query) or die(mysql_error());
    
    	// Insert a new row in the table for each person returned
    		  
    $display_string .= "<select name=\"town\" id=\"town\" onChange=\"ajaxFunction();\" class=\"textbox\">\n";
    $display_string .= "<option value=\"\">Search by Town...</option>\n";
    while($row = mysql_fetch_array($qry_result))
    {
        $display_string .= "<option value=\"{$row['rsTown']}\" onclick=\"\">{$row['rsTown']} ({$row['PubCount']})</option>\n";
    }
    $display_string .= "</select>\n";
    	
    echo $display_string;
    ?>
    
    I think I need an area to show the data in for the <div id="ajaxDiv"></div>
    I really hope someone can help me?!

    thanks
     

Share This Page