簡體   English   中英

javascript ajax php顯示

[英]javascript ajax php display

通過使用javascript ajax,我已將值傳遞給php頁面...從數據庫中檢索后顯示值並在ajax頁面中打印...在警報頁面中顯示...但是我需要在ajax頁面中顯示...。

Ajax編碼

<script>
        function optionsAlert()
        {
            alert("call");
            var xmlhttp;
            var qw=document.getElementById('c').value;
            var qw1=document.getElementById('c1').value;
            var qw2=document.getElementById('unit33').value;
            var qw3=document.getElementById('dept').value;
            var qw4=document.getElementById('class').value;
            alert(qw);
            alert(qw1);
            alert(qw2);
            alert(qw3);
            alert(qw4);

            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)
             {
                alert(xmlhttp.responseText);    

                if(xmlhttp.responseText==1){
                alert("name already Exists" );                  
                return false;   
                }        
               else if(xmlhttp.responseText==0)
               {
                alert("name available");
                document.login.submit();    
               }

            }
         }

            var queryString = "?q=" + qw + "&q1=" + qw1 + "&q2=" + qw2 + "&q3=" + qw3 + "&q4=" + qw4;
            xmlhttp.open("get",'ques.php' + queryString,true);
            xmlhttp.send();
        }

php頁面

<?php 
include('connection.php'); 
if(($_GET['q']!='') && ($_GET['q1']!='') && ($_GET['q2']!='') && ($_GET['q3']!='') && ($_GET['q4']!=''))
{
    $a= mysql_query("select * from unit1 where unit='".$_GET['q']."' and stopic='".$_GET['q1']."' and qtype='".$_GET['q2']."' and dept='".$_GET['q3']."' and class='".$_GET['q4']."'");
    //$a= mysql_query("select * from unit1 where unit='unit1' and stopic='Subtopic1' and qtype='normal questions'");
    if(mysql_num_rows($a)>0)
    {
        while($row=mysql_fetch_array($a))
        {
            $a1=$row['qno'];
            $a2=$row['ques'];
            $a3=$row['ch1'];
            $a4=$row['ch2'];
            $a5=$row['ch3'];
            $a6=$row['ch4'];
            $a7=$row['ans'];
            echo $a1."<br>";echo $a2."<br>";echo $a3."<br>";echo $a4."<br>";echo $a5."<br>";echo $a6."<br>";echo $a7."<br>";
            echo 1;
        }
    }
    else
    {
        echo 0;
    }
}
?>

在ajax編碼下面,創建具有id的div元素

在你的ajax方法

if(xmlhttp.responseText==1){
                document.getElementById("id").innerHTML = "name already Exists";                   
                return false;   
                }        
               else if(xmlhttp.responseText==0)
               {
                document.getElementById("id").innerHTML="name available";
                document.login.submit();    
               }

“ id”是您自己的div ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM