关于PHP,MYSQL,AJAX的问题

Questions on PHP,MYSQL,AJAX

本文关键字:问题 AJAX MYSQL PHP 关于      更新时间:2023-09-26

我遇到了一个问题,我计划用javascript从mysql获取值。

我想做的是,当你加载一个PHP页面编辑信息,你使用javascript添加一个div,会有一堆类型的选择,但我想通过从数据库检索做到这一点。但它似乎不起作用,所以我继续使用另一种类型的代码。

<!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
$mysql_server_name="localhost";
$mysql_username   ="root";
$mysql_password   ="root";
$mysql_database   ="master_db";
$conn =mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
        if (!$conn)
              {
              die('Could not connect: ' . mysql_error());
              }
mysql_select_db($mysql_database,$conn);
$sql="SELECT * FROM `bopl_certification_type`";
$result = mysql_query($sql);
echo "<div class='span6 form-horizontal'><i class='icon-remove'     onclick='removeDiv(this)'></i><div class='control-group'><label class='control-    label'>Certificate Name: </label><div class='controls'><select         style='width:250px' name='certType[]'>";
//To get option
while($row = mysql_fetch_array($result)) {
echo "<option value=$row[certName]>$row[certName]</option>";
}
//End of option
echo "</select><input type='text' class='input-xlarge' name='test' style='width:235px' name='fileInServ[]' readonly='readonly' /><input type='file' name='docupload[]' /></div><div class='control-group'><label class='control-label'>Issue date: </label><div class='controls'><input type='text' id='datepickerIs' name='certIsDate[]' placeholder='dd-mm-yyyy' class='comboDate' /></div></div><div class='control-group'><label class='control-label'>Expired date: </label><div class='controls'><input type='text' id='datepickerEx' name='certExDate[]' placeholder='dd-mm-yyyy' class='comboDate' /></div></div>    </div>";
?>
</body>
</html>

这是getuser。php这是index。html

<html>
<head>
<script>
function showUser() {
    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",true);
    xmlhttp.send();
   }
</script>
</head>
<body>
<form>
<select name="users" onclick="showUser()">
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</body>
</html>

这是我想要的正确结果。但似乎我试图融入我现有的代码。它根本不起作用。还有别的办法吗?

首先,我将为您的服务器调用创建一个函数以简化事情。这是我使用的,但它使用主线程,所以如果你想让它是异步的,那么你将不得不添加readystatechange,并在初始声明上更改true为false,然而,这是我的函数检索。

function httpGet(theUrl){
	//FETCH Data From Server
	xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET", theUrl , false );
	xmlhttp.send();   
	return xmlhttp.responseText;
}

然后你可以在脚本的任何地方调用

  document.getElementById("txtHint").innerHTML = httpGet("getUser.php");

现在对于任何额外的帮助,我们真的需要知道,什么错误你接收。