Eggzorcist Posted June 3, 2009 Share Posted June 3, 2009 I'm trying to make a form which will using ajax get information through php and sql and will display that information Ajax style but I'm currently getting an error. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <LINK REL=stylesheet HREF="stylesheet.css" TYPE="text/css"> </head> <body> <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your browser broke!"); return false; } } } ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 1){ document.myForm.time.value = ajaxRequest.responseText; } } var id = document.getElementById('id').value; var queryString = "?id=" + id; ajaxRequest.open("GET", "trackeractions.php" + queryString, true); ajaxRequest.send(null); } //--> </script> <div class='wrap'> <p><img src="images/logo.png" width="190" height="29" /><br /> </p> <div class="nav">Home</div> <div class="nav2">Services</div> <div class="nav2">About Us</div> <div class="nav2">Customer Support</div> <div class="nav2">Tracker</div> <div class="spacer"></div> <div class="title"> <div class="textle">Price & Services </div> </div> <div class='picture'><img src="images/cargo.png" alt="cargoplane" width="450" height="199" /></div> <div class='content'> Package ID:<input name="id" type="text" value="" size="10" maxlength="10" /> <br /> <input type='button' onclick='ajaxFunction()' value='Track' /> </div> </div> </body> </html> php file <?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "tgj4u"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); // Retrieve data from Query String $id = $_GET['id']; // Escape User Input to help prevent SQL Injection $id = mysql_real_escape_string($id); //build query $query = "SELECT * FROM maildata WHERE id = '$id'"; //Execute query $qry_result = mysql_query($query) or die(mysql_error()); // Insert a new row in the table for each person returned $info = mysql_fetch_array($qry_result) echo "Sent by: " . $info['sender'] . "<br>"; echo "Recipient: " . $info['recipient'] . "<br>"; echo "Departed: " . $info['depart'] . "<br>"; echo "Satus: " . $info['depart']; . "<br>"; echo "Current Location: " . $info['currentlocation'] . "<br>"; echo "Arrival Location: " . $info['finallocation'] . "<br>"; echo "Expected on: " . $info['expected'] . "<br>"; ?> I've tried alot of things, but I dont see where the error is... Thanks Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted June 3, 2009 Share Posted June 3, 2009 I'm trying to make a form which will using ajax get information through php and sql and will display that information Ajax style but I'm currently getting an error. What error do you receive? And does the php script work when you call it directly? Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted June 3, 2009 Author Share Posted June 3, 2009 I don't get any visual error but when I click submit it does "error on page" at the bottom info of the browser where it normally says the status of that loads the page or "done" status. The PHP does not yet work fully independantly, however, it does show information independantly just not the query information but everything else does. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted June 3, 2009 Share Posted June 3, 2009 This is the error I receive document.getElementById("id") is null which is logical since the following does not have an id="id" <input name="id" type="text" value="" size="10" maxlength="10" /> I suggest you get js debugger tool like firebug or use the Web Developer toolbar Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted June 3, 2009 Author Share Posted June 3, 2009 Thanks, I've been following this tutorial on tizag. http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php Its one of the first time I'm using ajax, do you have any suggestions of how I could be going to fix the problem? Thanks Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted June 3, 2009 Author Share Posted June 3, 2009 I changed <input name="ID" id='id' type='submit' onclick='ajaxFunction()' value='Track' /> and still get the error Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted June 3, 2009 Share Posted June 3, 2009 Yup. Wrap your form elements inside <form> tags. And get a js debugging tool so you can track the javascript errors easier or when you can't figure it out you can post the js error so someone can help you easier https://addons.mozilla.org/nl/firefox/addon/1843 webdeveloper toolbar https://addons.mozilla.org/en-US/firefox/addon/60 firebug the error you are getting now is a different one Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted June 3, 2009 Share Posted June 3, 2009 Hmmm sort of have to eat my own words but anyway see the following? document.myForm.time.value = ajaxRequest.responseText; thats where the received text gets placed on your page change that to something like: document.getElementById('your_div').innerHTML = ajaxRequest.responseText; then add a div element to your page <div id="your_div"><div> Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted June 3, 2009 Author Share Posted June 3, 2009 I downloaded firebug but coudnt seem to find the mistake. So far all its doing is refreshing the page and adding the ?id=55583&ID=Track to the url I'm unsure why ID=Track and I am not seeing any echo thats within my php document for some reason. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted June 3, 2009 Share Posted June 3, 2009 the submit button submits the form. You can either replace the submit button with a hyper link or add "return false;" to the onclick <input type='button' onclick='return false;ajaxFunction()' /> Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted June 3, 2009 Author Share Posted June 3, 2009 I've added that input button and it still seems to be not working. I'm having trouble seeing why as I've followed the tizag tutorial accurately. http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted June 3, 2009 Author Share Posted June 3, 2009 I managed to get it to work Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.