我尝试从JS(客户端)向PHP(服务器端)发送数据username
。问题是我的PHP中有notice undefined index : username
,但我可以回显数据。这是我的代码:
function showdatabase(){
var username =localStorage.getItem('username');
//document.write(username);
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","dataquery.php?username="+username,true);
xmlhttp.send();
}
<?php
include('connection.php');
$username=$_GET['username']; echo $username;
//query of database
$result =mysqli_query($mysqlConnection,"SELECT * FROM users WHERE user_ID LIKE '$username'");
echo "<table border='4'> <tr> <th> Full Name</th> </tr>";
//read data from array from database.
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>"; echo "<td>".$row['nama_penuh']."</td>"; echo "</tr>"; }
?>
我是编程新手,希望大家都能提供帮助。提前谢谢你:)
答案 0 :(得分:0)
您需要在函数username
的参数中添加showdatabase
,以便:
showdatabase(username) {
// ** Content of your function **//
}
当您调用您的函数时,需要初始化用户名:
<a href="javascript: showdatabase('John');">MyCall</a>