Php ajax不在DOM中加载内容
加载print.php以显示内容,而不是在html中填充“tempDiv”。同样的代码也适用于其他文件和javascript函数:/ HTML: 调用printDiv()函数以打印所选列:Php ajax不在DOM中加载内容,php,javascript,dom,Php,Javascript,Dom,加载print.php以显示内容,而不是在html中填充“tempDiv”。同样的代码也适用于其他文件和javascript函数:/ HTML: 调用printDiv()函数以打印所选列: function printDiv() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } els
function printDiv()
{
var xmlhttp;
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("searchresults").innerHTML=xmlhttp.responseText;
}
}*/
xmlhttp.open("POST", "print.php", true); // set the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // adds a header to tell the PHP script to recognize the data as is sent via POST
xmlhttp.send(); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("tempDiv").innerHTML = xmlhttp.responseText;
}
}
}
PHP:
发送请求之前是否应该定义readystate函数
第二部分。看起来printdiv函数提交了一个表单。如果您使用的是严格的AJAX过程,那么应该能够删除表单标记。您还需要调整其他一些事情来实现这一点。您的意思是将PHP代码加载到DOM中?>通常ajax使用innerHTML等加载DOM中选定元素中的内容。但是,在我的例子中,Prim.PHP被加载到DOM中,ECHO语句简单地将数据放在空白页上,而不是将其加载到TunDeVik中。html@AkashGupta虽然在你发送的截图中,表格似乎在回响。我丢失标签了吗?我在你的php或js中没有看到它们。虽然一个好的做法是,只要他正在寻找的状态是4,顺序并不重要..这没有什么区别兄弟..:/我的答案的哪一部分?readystate部分..我同意第2部分,但是我需要将所选字段传递到PHP文件,这就是为什么我使用表单和ajax。是否有其他方法通过ajax将所选字段传递给PHP文件
要访问选中的复选框,是否有其他方法可以通过AJAX访问这些选中的复选框,以便删除表单标记?
function viewall(){
var xmlhttp;
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("searchresults").innerHTML=xmlhttp.responseText;
}
}*/
xmlhttp.open("POST", "viewall.php", true); // set the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // adds a header to tell the PHP script to recognize the data as is sent via POST
xmlhttp.send(); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("searchresults").innerHTML = xmlhttp.responseText;
}
}
}
function printDiv()
{
var xmlhttp;
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("searchresults").innerHTML=xmlhttp.responseText;
}
}*/
xmlhttp.open("POST", "print.php", true); // set the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // adds a header to tell the PHP script to recognize the data as is sent via POST
xmlhttp.send(); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("tempDiv").innerHTML = xmlhttp.responseText;
}
}
}
<?php
$col = $_POST['print'];
$flds = "";
foreach($col as $value){
if(isset($col)){
if($flds !="")
$flds .= ",";
$flds .= $value;
}
}
$sql = "SELECT ". $flds." from student";
$con = mysqli_connect("localhost", "root", "","university") or die("could not connect". mysqli_error($con));
$rs = mysqli_query($con, $sql);
echo "<table border='1'";
while($r = mysql_fetch_array($rs)){
echo "<tr>";
echo "<td class='searchtabledata'>".$r[0]."</td>";
echo "<td class='searchtabledata'>".$r[1]."</td>";
echo "</tr>";
}
?>