1

我有以下html:

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

我也有以下脚本:

功能显示内容(){

  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);

}

在 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 脚本并向我显示两个可供选择的按钮。如果我按继续,我希望脚本从它停止的地方执行。

有人知道吗??先感谢您!

4

2 回答 2

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> 

请注意,这不是经过验证的代码,因此您应该对其进行修改

于 2012-09-24T09:45:38.087 回答
-1

使用 ' echo' 后跟 < html code > 来创建按钮。你会完成的。

于 2014-11-12T23:31:59.447 回答