2012-09-24 17 views
0

我有以下的html:做一個形式有兩個按鈕從一個PHP文件選擇操作

<body onload="showcontent()"> <!-- onload optional --> 
     <div id="content"><img src="loading.gif"></div> <!-- leave img out if not onload --> 
    </body> 

我也有下面的腳本:

功能showcontent(){

if(window.XMLHttpRequest) { 
    xmlhttp = new XMLHttpRequest(); 
    } else { 
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    } 

    xmlhttp.onreadystatechange = function() { 
    if(xmlhttp.readyState == 1) { 
     document.getElementById('content').innerHTML = "<img src='loading.gif' />"; 
    } 
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     document.getElementById('content').innerHTML = xmlhttp.responseText; 
    } 
    } 

    xmlhttp.open('GET', 'elsevier.php', true); 
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); 
    xmlhttp.send(null); 

} 

In elsevier.php我希望當一個表的行數超過500時,會顯示兩個按鈕,告訴我是否繼續或取消。如果我把這個代碼放在.php文件中,什麼都沒有發生......(除了出現兩個按鈕)。

if(mysqli_errno($con)==1062){ 
        @$q55="SELECT COUNT(*) FROM testarticles"; 
        $result55 = mysqli_query($con, $q55); 
        $row = $result55->fetch_row(); 
        echo '#: ', $row[0].'<br>'; 

        if($row[0]>=500&&$answer==false){ 
         echo '<form action="" method="GET">'; 
         echo "<label>Do you want to continue or cancel?</label>"; 
         echo '<input type="button" id="but1" name="but1" value="Continue">'; 
         echo '<input type="button" id="but2" name="but2" value="Cancel">'; 
         echo '</form>'; 

         //sleep(2); 

         if(isset($_GET["but1"])){ 
          $answer=true; 
          break; 
         } 
         elseif(isset($_GET["but2"])){ 
          @$q56="DELETE * FROM journal, volume, issue, articles, testarticles WHERE import_time=$unixtimestamp"; 
          $result56 = mysqli_query($con, $q56); 
          exit; 
         } 
        } 

我想在這一步中執行php腳本來停止並顯示兩個按鈕可供選擇。如果我按繼續,我希望腳本從停止的位置執行。

有沒有人有任何想法?先謝謝你!

+0

我認爲這不能用php做..也許我錯了..你可以通過將結果添加到會話變量,然後繼續從該會話的最後一項添加數據庫var –

+0

if它可以用其他方式完成你有什麼想法嗎?謝謝。 –

+0

你能舉個例子嗎?謝謝。 –

回答

0
session_start(); 

if(mysqli_errno($con)==1062){ 

       $where = isset($_SESSION['last_id']) ? " WHERE id > '" . $_SESSION['last_id'] . "'" : ""; 
       if(isset($_SESSION['last_id'])) 
        unset($_SESSION['last_id']); 

       @$q55="SELECT COUNT(*) FROM testarticles$where ORDER BY id"; 
       $result55 = mysqli_query($con, $q55); 
       $row = $result55->fetch_row(); 
       echo '#: ', $row[0].'<br>'; 

       if($row[0] >= 500 && $answer == false){ 

        $_SESSION['last_id'] = $row['id']; 

        echo "<label>Do you want to continue or cancel?</label>"; 
        echo '<input type="button" id="but1" name="but1" value="Continue" onclick="showcontent(true)" />'; 
        echo '<input type="button" id="but2" name="but2" value="Cancel">'; 

        //sleep(2); 

        if(isset($_GET["but1"])){ 
         $answer=true; 
         break; 
        } 
        elseif(isset($_GET["but2"])){ 
         @$q56="DELETE * FROM journal, volume, issue, articles, testarticles WHERE import_time=$unixtimestamp"; 
         $result56 = mysqli_query($con, $q56); 
         exit; 
        } 
       } 

你必須還修改showcontent功能,因爲當你按下繼續按鈕檢索到的內容應附加,而不是替代

function showcontent(){ 
    if(window.XMLHttpRequest) {   
    xmlhttp = new XMLHttpRequest();   
    } else {   
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');   
    }   

    xmlhttp.onreadystatechange = function() {   
    if(xmlhttp.readyState == 1 && !update) {   
     document.getElementById('content').innerHTML = "<img src='loading.gif' />";   
    }   
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     if(update) 
     alert("this is an update and I should append data, not to replace the div content"); 
     else {  
     document.getElementById('content').innerHTML = xmlhttp.responseText;   
     //this is the first call of the function so the content of the div will be replaced 
     } 
    }    
    }   

    xmlhttp.open('GET', 'elsevier.php', true);   
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');   
    xmlhttp.send(null);   

} 

HTML代碼:

<body onload="showcontent(false)"> <!-- onload optional -->    
    <div id="content"><img src="loading.gif"></div> <!-- leave img out if not onload -->    
</body> 

注意這不是一個驗證碼,所以你應該修改它

+0

非常感謝你的時間!我現在就試試吧! –

+0

我不明白這一行:$ where = isset($ _ SESSION ['last_id'])? 「WHERE id>'」。 $ _SESSION ['last_id']。 「'」:「」; –

+0

這相當於:'if(isset($ _ SESSION ['last_id']))$ where =「where ...」; else $ where =「」;'。這是使用if else語句的簡短方式 –

-1

使用'echo'後面是< html代碼>用於創建按鈕。你會完成的。

+0

雖然添加舊問題的答案有時很有用,但通常不會。處理較新的問題通常更有用和有益。 – Paul