how to access database through javascript?

2020-04-16 04:09发布

I am creating one admin page where I have multiple textboxes.when I enter the userid in one textbox I want to display user name in next textbox when admin moves to next text box.for this I can use ajax or javascript? which one will be better?how can I do it through javascript.

8条回答
爷的心禁止访问
2楼-- · 2020-04-16 04:42

You will need to use ajax which is 9/10ths javascript anyway. I suggest using a JS framework so that you don't have to code for different browsers. Then you will need something on the server side that will process the request and spit the required data back at you. Popular formats are XML (the x in ajax), JSON, or just plain ol' HTML. Either way you will need a javascript callback to process the result (or error condition, don't forget that) and populate your textbox. Personally I prefer just returning HTML fragments but if you've got a framework chances are you'll find XML and JSON handling in it so go crazy.

查看更多
何必那么认真
3楼-- · 2020-04-16 04:43

No ajax or javascript needed.
Just make alphabetical listing of all users with links to edit page. You can also add to this page a form with a single textbox to enter userid manually.
Both links and form leads to the edit page that use userid from the query string to retrieve users detais.

查看更多
家丑人穷心不美
4楼-- · 2020-04-16 05:02

Use an AJAX-Call to get the data you need from a server side script (here php) and insert the result of the call in the desired textbox.

查看更多
成全新的幸福
5楼-- · 2020-04-16 05:04

You cannot access the DB directly through Javascript.

You can use AJAX to call a serverside (e.g. PHP) page that queries the DB though

查看更多
成全新的幸福
6楼-- · 2020-04-16 05:04

The answer it's AJAX, but first of all it's better if you understand what AJAX is: Asynchronous JavaScript And XML. Then, for simplify your task it's better if you use a javascript framework like jQuery or mootools.
I personally suggest jQuery.

查看更多
三岁会撩人
7楼-- · 2020-04-16 05:07
<script>
    function showUser(str) {
        if (str=="") {
          document.getElementById("spName").innerHTML="";
          return;
        } 

        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("spName").innerHTML=xmlhttp.responseText;
          }
        }

        xmlhttp.open("GET","getUserName.php?q="+str,true);
        xmlhttp.send();
    }
</script>

getUsreName.php

<?php

$q=$_GET["q"];

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="pnpdb"; // Database name 

// Connect to server and select databse.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name",$link)or die("cannot select DB");


$sql="SELECT name FROM tblprofile WHERE userId = '".$q."'";

$result = mysql_query($sql);

$row = mysql_fetch_array($result);

$name =$row['name'];

if($name == '' || empty($name)) {
  echo "<b>ID not found.</b>";
} else {
  echo "<b>".$name."</b>";
}

mysql_close($link);
?>
查看更多
登录 后发表回答