Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts

    help with ajax and database

    I have this order.html:
    Code:
    <html>
    <body>
    
    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxFunction(){
    	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){
    			document.myForm.time.value = ajaxRequest.responseText;
    		}
    	}
    	var age = document.getElementById('age').value;
    	var wpm = document.getElementById('wpm').value;
    	var sex = document.getElementById('sex').value;
    	var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
    	ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
    	ajaxRequest.send(null); 
    }
    
    //-->
    </script>
    
    
    
    <form name='myForm'>
    Max Age: <input type='text' id='age' /> <br />
    Max WPM: <input type='text' id='wpm' />
    <br />
    Sex: <select id='sex'>
    <option>m</option>
    <option>f</option>
    </select>
    <input type='button' onclick='ajaxFunction()' value='Query MySQL' />
    </form>
    </body>
    </html>
    and this is the ajax-example.php:
    Code:
    <?php
    $dbhost = "localhost";
    $dbuser = "dbusername";
    $dbpass = "dbpassword";
    $dbname = "dbname";
    	//Connect to MySQL Server
    mysql_connect($dbhost, $dbuser, $dbpass);
    	//Select Database
    mysql_select_db($dbname) or die(mysql_error());
    	// Retrieve data from Query String
    $age = $_GET['age'];
    $sex = $_GET['sex'];
    $wpm = $_GET['wpm'];
    	// Escape User Input to help prevent SQL Injection
    $age = mysql_real_escape_string($age);
    $sex = mysql_real_escape_string($sex);
    $wpm = mysql_real_escape_string($wpm);
    	//build query
    $query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
    if(is_numeric($age))
    	$query .= " AND ae_age <= $age";
    if(is_numeric($wpm))
    	$query .= " AND ae_wpm <= $wpm";
    	//Execute query
    $qry_result = mysql_query($query) or die(mysql_error());
    
    	//Build Result String
    $display_string = "<table>";
    $display_string .= "<tr>";
    $display_string .= "<th>Name</th>";
    $display_string .= "<th>Age</th>";
    $display_string .= "<th>Sex</th>";
    $display_string .= "<th>WPM</th>";
    $display_string .= "</tr>";
    
    	// Insert a new row in the table for each person returned
    while($row = mysql_fetch_array($qry_result)){
    	$display_string .= "<tr>";
    	$display_string .= "<td>$row[ae_name]</td>";
    	$display_string .= "<td>$row[ae_age]</td>";
    	$display_string .= "<td>$row[ae_sex]</td>";
    	$display_string .= "<td>$row[ae_wpm]</td>";
    	$display_string .= "</tr>";
    	
    }
    echo "Query: " . $query . "<br />";
    $display_string .= "</table>";
    echo $display_string;
    ?>
    I have it here:http://shedokan.110mb.com/order.html
    but it doesn't shows stuff from the database

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by shedokan View Post
    Code:
    ...
    Sex: <select id='sex'>
    <option>m</option>
    <option>f</option>
    </select>
    the ajax call works fine the problem lies with the value of the sex since there is no value attribute defined on the options the sex = document.getElementById('sex').value always takes the value '' (firefox in all its wisdom takes the correct value even if the value attribute is not defined...)

    try using
    Code:
    ...
    Sex: <select id='sex'>
    <option value="m">m</option>
    <option value="f">f</option>
    </select>
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    thanks that did it!


  •  

    Posting Permissions

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