943,846 Members | Top Members by Rank

Ad:
Jul 17th, 2008
0

How to query database through javascripts

Expand Post »
Hi,

I don't know much about javascripts. I am preparing a module in PHP in which I have to validate data entered by a user through javascripts. Now there is a field in which I have ensure that the value entered by the user already resides in the database. How could I achieve this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jul 17th, 2008
0

Re: How to query database through javascripts

First you have to make a script in PHP to query the database with the value passed from the script below and then give an output in the script by using either print or echo. In the page that you want to pass the value back to the script you need to add the following....
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript1.2" type="text/javascript">
  2. function makeRequest(url) {
  3. var httpRequest;
  4.  
  5. if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  6. httpRequest = new XMLHttpRequest();
  7. if (httpRequest.overrideMimeType) {
  8. httpRequest.overrideMimeType('text/xml');
  9. }
  10. }
  11. else if (window.ActiveXObject) { // IE
  12. try {
  13. httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  14. }
  15. catch (e) {
  16. try {
  17. httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. catch (e) {}
  20. }
  21. }
  22.  
  23. if (!httpRequest) {
  24. alert('Giving up :( Cannot create an XMLHTTP instance');
  25. return false;
  26. }
  27.  
  28. httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
  29. httpRequest.open('GET', url, true);
  30. httpRequest.send('');
  31.  
  32. }
  33.  
  34. // Function to deal with returned request
  35. function alertContents(httpRequest) {
  36. if (httpRequest.readyState == 4) {
  37. if (httpRequest.status == 200) {
  38. changeText(httpRequest.responseText);
  39. //alert(httpRequest.responseText);
  40. } else {
  41. alert('There was a problem with the request. '+httpRequest.status);
  42. }
  43. }
  44. }
  45.  
  46. Then with is in the page you only have to make your own function in JS to deal with the reply back from your script on the webserver.
  47.  
  48. function changeText(){
  49. // This is were you put your script to handel if it is the same or not.
  50. }
  51. </script>
Last edited by Tekmaven; Jul 17th, 2008 at 1:33 pm. Reason: Code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davonz is offline Offline
3 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: how to display only checked value in checkbox by using javascript?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: newbie coping the data in html field





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC