nouaman Posted January 9, 2015 Share Posted January 9, 2015 hi thereplease be noted that i need help with the code found on w3schools regarding fetching data from mysql dbase using php and ajax and im using 2 files one is html and second one is phpi used the 2 codes below but when i select a user all i get is this please see below :Firstname Lastname Age Hometown Job "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['FirstName'] . ""; echo "" . $row['LastName'] . ""; echo "" . $row['Age'] . ""; echo "" . $row['Hometown'] . ""; echo "" . $row['Job'] . ""; echo ""; } echo ""; mysql_close($con); ?>Note : that i have created the same database suggested i just wanted to try that and see if it works and then proceed with my own database and tablesNote : my database is :testmy table is :userHTML CODE :---------<html><head> <script>function showUser(str) { if (str == "") { document.getElementById("txtHint").innerHTML = ""; return; } else { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("txtHint").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send(); }}</script></head><body><form><select name="users" onchange="showUser(this.value)"> <option value="">Select a person:</option> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Joseph Swanson</option> <option value="4">Glenn Quagmire</option> </select></form><br><div id="txtHint"><b>Person info will be listed here.</b></div></body></html>---------------------PHP CODE :=========<?php$q = intval($_GET['q']);$con = mysql_connect('localhost','root','','test');if (!$con) { die('Could not connect: ' . mysql_error($con));}mysql_select_db("test",$con);$sql="SELECT * FROM user WHERE Id = '".$q."'";$result = mysql_query($con,$q);echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>";}echo "</table>";mysql_close($con);?> Link to comment Share on other sites More sharing options...
Ingolme Posted January 9, 2015 Share Posted January 9, 2015 From the output you're getting it sounds like the PHP is not actually being executed. This program has two parts that you have to debug independently from each other: The PHP part and the Javascript part. The Javascript part First, test the Javascript. In order to prevent any PHP errors from ruining the Javascript testing we'll just put this at the beginning of the PHP file which we'll remove once the test succeeds: <?phpecho 'Hello, World!';exit;?> Now in the Javascript code we'll just alert(xmlhttp.responseText) to see if it works. The PHP part To debug this, we have to open the PHP file right in our browser and give a test value to the q variable, for example getuser.php?q=1 If it displays what we expected it to, then the program is working. If it doesn't then there probably are some error messages which will be clear to see. Link to comment Share on other sites More sharing options...
nouaman Posted January 9, 2015 Author Share Posted January 9, 2015 Hi there thanks for ur reply first, i have tried as u suggested this code : <?phpecho 'Hello, World!';exit;?> and it returned hello world which is fine so i have tried to replace the (q) in my code with a value which 2 and executed the php file and it returned the right row corresponding to number 2 please check image below to see what im talking about ( ! ) Notice: Undefined index: q in C:wampwwwajaxdataphpgetuser.php on line 23 Call Stack # Time Memory Function Location 1 0.0020 143968 {main}( ) ..getuser.php:0 Firstname Lastname Age Hometown Job Joseph Swanson 39 Quahog Police Officer thanks in advance Link to comment Share on other sites More sharing options...
justsomeguy Posted January 9, 2015 Share Posted January 9, 2015 It sounds like it's working fine. When you open the file in the browser make sure you type in a URL, don't just double-click on the HTML file. Link to comment Share on other sites More sharing options...
nouaman Posted January 9, 2015 Author Share Posted January 9, 2015 hi there im not sure what u r refering to here i use local host so when i run the html file i get the problem reported in my first post so when say type the URL what do u mean please check below : when i click on run the html file this is the adress i get on my browser : http://127.0.0.1:8020/ajaxdataphp/ajaxtest.html (problem when i choose a value like reported in first post) so what do you mean by typinh the url here thank u in advance Link to comment Share on other sites More sharing options...
nouaman Posted January 9, 2015 Author Share Posted January 9, 2015 hi actually i tried this http://localhost/ajaxdataphp/ajaxtest.html its working partially but when i choose the first opttion or name it shows me the second one , same for second shows me the 3rd and third shows me the 4th so it skeeps the 1st row i think there something to go with Id if im not mistaken i tried to change in the option values in html but same result any suggestions would be appreciated thanks in advance Link to comment Share on other sites More sharing options...
justsomeguy Posted January 9, 2015 Share Posted January 9, 2015 If you want to see what the ajax is doing, you can open your browser's developer tools and check on the net tab. You should see the ajax request going out and you can check on the response from the server and things like that. The console tab in the developer tools will show any error messages that happen. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 9, 2015 Share Posted January 9, 2015 its working partially but when i choose the first opttion or name it shows me the second one ,Maybe the IDs are wrong. The values in the option tags need to match the IDs of the people in the database. Link to comment Share on other sites More sharing options...
nouaman Posted January 9, 2015 Author Share Posted January 9, 2015 hi there thank you so much for your help i really appreciate it it working like a charme now i have one last question if you do not mind why i had to type the URL http://localhost/ajaxdataphp/ajaxtest.html to get this working instead of just clicking on run in the platform that im using in building my project thanks in advance Link to comment Share on other sites More sharing options...
justsomeguy Posted January 9, 2015 Share Posted January 9, 2015 I don't know, I don't know what the run command does or what environment you're using. Maybe it's using a different server. Link to comment Share on other sites More sharing options...
nouaman Posted January 9, 2015 Author Share Posted January 9, 2015 ok sounds good so anyway that was a help since you suggested that i will take it into concideration for next files as well thanks again Link to comment Share on other sites More sharing options...
sandeepm Posted February 28, 2015 Share Posted February 28, 2015 till now we are getting the data from ajax by using jquery i didn't try using php is it really posible ??? Link to comment Share on other sites More sharing options...
Ingolme Posted February 28, 2015 Share Posted February 28, 2015 You should start your own thread for questions. AJAX is a Javascript technology that makes requests to the server. An AJAX request may load a PHP file, which will cause the PHP code to run. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now