PHP form with Javascript


PHP form with Javascript

一直在尝试编辑我在W3学校找到的脚本。

编辑:我想要一个搜索文本框。我可以在其中输入数据,它将触发 PHP 脚本并返回值而无需重新加载主页。因此,当我输入"404040"并单击搜索按钮时,它应该执行此adminsearchvip.php?q=404040然后返回值而不重新定位主页。 编辑结束。

原始脚本是一个下拉菜单,但我想要一个文本框。

我不确定发送"提交"按钮的代码是否正确。

第一个文件搜索VIP.php

    <html>
<head>
<script>
function showUser()
{
if (str=="")
  {
  document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","adminsearchvip.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<label for="Number"></label>
<input type="text" name="Number" id="Numbers" value="">
<input type='button' onclick='showUser(Numbers.value)' value='Search' />
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html> 

还有剧本本身。

管理员搜索贵宾.php

<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','xxxxxx','xxxxx','jossan');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }
mysqli_select_db($con,"jossan");
$sql="SELECT * FROM vip_stat WHERE code = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Number</th>
<th>Bought Date</th>
<th>Type</th>
<th>Price</th>
<th>Used</th>
<th>Activation Date</th>
<th>Username</th>
</tr>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['number'] . "</td>";
  echo "<td>" . $row['time'] . "</td>";
  echo "<td>" . $row['type'] . "</td>";
  echo "<td>" . $row['Price'] . "</td>";
  echo "<td>" . $row['used'] . "</td>";
  echo "<td>" . $row['utime'] . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysqli_close($con);
?> 

我已经测试过使用我的网络浏览器进行 www.homepage.com/adminsearchvip.php?q=9494943,并且可以正常工作,我得到了正确的响应。所以脚本似乎有效。但是搜索页面甚至没有显示任何内容。

"一直在尝试编辑我在W3学校找到的脚本。

好吧,这是你的问题。不要使用 W3 学校。

由于您希望使用 ajax 加载 html 表并在用户单击按钮时将其插入到 dom 中,因此您可以使用 jquery load 轻松完成此任务。下面是一个粗略的示例:

//note: put id="buttonId" on the button element in order for this to work
$('#buttonId').click(function(){
    var value = $('#Numbers').val();
    if (value !== '') {
        $('#txtHint').load("adminsearchvip.php?q="+value);
    }
});

顺便说一下,您应该在 adminsearchvip 脚本中使用预准备语句,而不是将 $q 变量连接到 sql。

从你的问题来看,我建议你试试jquery。

链接到页面标题部分的 Jquery,

showUser()
{
 var url='adminsearchvip.php';
 var q = $("#Numbers").val();
 $("#txtHint").html('Loading Please Wait...');
 $.get(url,{ q: q } ,function(data){  $("#txtHint").html(data) }); 
}

这应该为您完成。

由于是搜索,因此我使用 $.get 如果是发布请求,请使用 $.post .如果您发现更多问题,请告诉我

这是由Google CDN托管的jQuery的链接 //ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

这就是您的表单的外观

<form>
<label for="Number"></label>
<input type="text" name="Number" id="Numbers" value="">
<input type='button' onclick='javascript: showUser();' value='Search' />
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

更改以下行

<input type='button' onclick='return showUser()' value='Search' />

现在在你的函数 showUser 中添加一行。

function showUser()
{
  var str = document.getElementById("Number").value;

现在,如果您仍有问题,请告诉我。