Jump to content

Coder2345

Members
  • Posts

    21
  • Joined

  • Last visited

Everything posted by Coder2345

  1. I'm not switching to any code? I'm letting you know that I have a different file on the same server that works if I use that code. That is the only code for that file. I used it when I first started coding (literally), in order to know if I was able to establish a connection with the server. I will try what you suggested. I'm just very confused as to why that file works and the one we are discussing doesn't. It's the same database and the same user with same password that has access to it. So it should work. The only difference is the table. Thank you for the suggestion. Trying it now.
  2. You see, both localhost and the servername connect when I use this code in a file; "Connected successfully" is the output. So I'm not sure why the same criteria isn't working in this case.
  3. I've tried 'localhost' and the servername. I get the same error no matter what. Should I try the IP or domain name?
  4. I tried this but it still says it cannot connect. Do you think the data type could be causing it not to work? id = int(6) FirstName = varchar(20) LastName = varchar(20) Age = int(3) Hometown = varchar(100) Job = archer(100) Or could it be to do with the xmlhttp not having a valid object? Thank for your help so far. It's appreciated.
  5. From some digging around, it was suggested that xmlhttp needs to have a valid value (handle/object) before the line of code above can be executed properly. Does anyone have any suggestions as to what I should include in the code to solve this?
  6. I just used "ajax_demo" as an example. My actual database is called something different. My table is also called something different. To illustrate: database = my_DB table = ajax_demo column = id other columns = FirstName, LastName, Age, Hometown, Job
  7. I was missing one letter in the code. That solved that error issue. But now it's back to saying "could not connect" when I select a person from the list. I was advised to use localhost by a staff member from my web host, so maybe this is the issue? That is how I attempt to connect at present. A different PHP file from earlier practice looks like this: This code works, whereas the other one doesn't. Do you think localhost is the issue, as I had originally used www.servername.com instead of localhost when I wrote the code?
  8. I can't quite pinpoint the area where this might be occurring. A quick Google search of "xmlhttp is not defined" lead me to this: https://www.npmjs.com/package/xmlhttprequest Should I add that code to mine?
  9. This is the error I get when I try to select a person from the list; How does one solve this issue? Any help would be much appreciated.
  10. I've tried what you suggested. "Could not connect:" is the response when a name is selected from the form. The same response occurs when the php page is run in the browser. Two questions; What is user referring to here? My table? Shouldn't user have quotes around it? Also, what is users referring to? Any help would be much appreciated.
  11. Thank you for your input. I'll try what has been suggested.
  12. My apologies, but I don't quite understand what you mean?
  13. Thanks. Do you mean like this? I have both set to showUser in the HTML file.
  14. When I run the PHP file and use Developer Tools in the Chrome browser; When I run the HTML file and use Developer Tools in the Chrome browser; ^ This occurs when I attempt to select one of the users from the list. Thanks for replying.
  15. I changed it to the name of my database, but it still doesn't work? Any other suggestions? When I view the code in Netbeans, the line below states this; $q = intval($_GET['q']);
  16. I'm trying to select data from a MySQL database that is hosted on a webserver. I want to be able to retrieve the data from a table within the database and then illustrate it within a HTML table. There's an example on W3Schools that I've been following, but I'm unable to retrieve the data successfully. http://www.w3schools.com/php/php_ajax_database.asp Below is the source code: (HTML) <html> <head> //Javascript code <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 (this.readyState == 4 && this.status == 200) { document.getElementById("txtHint").innerHTML = this.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> PHP File: (getuser.phd) <!DOCTYPE html> <html> <head> <style> table { width: 100%; border-collapse: collapse; } table, td, th { border: 1px solid black; padding: 5px; } th {text-align: left;} </style> </head> <body> <?php $q = intval($_GET['q']); $con = mysqli_connect('www.example.com','user_Admin','12345-678','my_DB'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"ajax_demo"); $sql="SELECT * FROM user WHERE id = '".$q."'"; $result = mysqli_query($con,$sql); echo "<table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; while($row = mysqli_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>"; mysqli_close($con); ?> </body> </html> *MySQL table is attached I think the issue might exist from mysqli_select_db($con,"ajax_demo"); onwards inside the PHP file. Should I be referring to the table that contains the data inside the database? I have the PHP File hosted on my webserver, so I'm not sure why it won't retrieve that data when a person is selected from the list of options on the HTML page. Any help would be much appreciated.
×
×
  • Create New...