首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时循环上升值

同时循环上升值
EN

Stack Overflow用户
提问于 2017-12-22 04:30:57
回答 2查看 69关注 0票数 0

我正在开发在线attendance.But,在循环条件下,我想先展示我的代码

代码语言:javascript
复制
<tbody>
        <?php
        $database = new Database();
        $db = $database->getConnection();
        $user = new User($db);
        $stmt = $user->atten();
        while($ro22 = $stmt->fetch(PDO::FETCH_ASSOC))
        {

        ?>
            <tr>
                <td><input name ="uname" id ="uname" onBlur="checkAvailability2()" style ="border:none" value = "<?php echo $ro22['user_id'] ?>"/></td>
                <td><?php echo $ro22['first_name'] ?> <?php echo $ro22['last_name'] ?></td>
                <td><?php echo $ro22['parent_contact'] ?></td>

                <td><input type="button" value="<?php echo $ro22['ai'] ?>" id="pres" name="pres" onclick="return change(this);" onBlur="checkAvailability()" class="w3-button w3-teal"/></td>

            </tr>
        <?php } ?>

 </tbody>

这是输出

我想要什么

我想要更新现在,基于101,102,103的缺值.价值

我试过很多次,但都失败了。请提前帮助我,谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-12-22 05:06:25

您需要在单击时调用该页面并传递user_id。使用jQuery很容易做到这一点:

代码语言:javascript
复制
function change(row) {
    $.post('thispage.php', { user_id: $(row).val() }, function(){ window.location.reload(); } );
}

然后接收PHP中的帖子:

代码语言:javascript
复制
if (!empty($_POST['user_id'])) {
    /* toggle admission status */
}

在请求完成并切换状态后,页面将重新加载。

票数 0
EN

Stack Overflow用户

发布于 2017-12-22 05:13:43

以下是一个一般的例子。它由您的PHP程序( AJAX sender)和另一个PHP文件( AJAX request receiver)组成,我将其重写为我认为您想要的方式,一个javascript文件(包含AJAX函数)。

您可以通过更改接收到的use-cases文件中的数据库查询来获得不同的PHP

Javascript文件(AJAX):

代码语言:javascript
复制
// Send the `id` of the element
function checkAvailability(id)
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function()
  {
    // This `if` underneath is success. It means we got a response back
    if (this.readyState == 4 && this.status == 200)
    {
      if(this.responseText == "OK")  
      {
          alert('ID: ' + id + ' changed. Response: ' + this.responseText);
          document.getElementById("demo").innerHTML = 'The student has been updated.';
      }
      else if(this.responseText == "Not OK") 
      {
          alert('Something went wrong with id: ' + id);
      }
    }
  };
  // For example you send a request to attendance.php sending `id` info
  // - attendance.php just needs to echo the response to answer back
  xhttp.open("GET", "attendance.php?id=" + id, true);
  xhttp.send();
}

主PHP页面(发送请求的文件):

代码语言:javascript
复制
// U need jQuery to be able to send AJAX requests. Copy this, add to your html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<?php
  $database = new Database();
  $db = $database->getConnection();
  $user = new User($db);
  $stmt = $user->atten();

  echo '<table>
  <tr>
    <th>Student ID</th>
    <th>Student name</th>
    <th>Phone number</th>
    <th>Today\'s attendance</th>
  </tr>';
  while($ro22 = $stmt->fetch(PDO::FETCH_ASSOC))
  {
    echo '<tr>
      <td><input name ="uname" id ="uname" onBlur="checkAvailability2()" style ="border:none" value="'.$ro22['user_id'].'"/></td>
      <td>'.$ro22['first_name'].' '.$ro22['last_name'].'</td>
      <td>'.$ro22['parent_contact'].'</td>
      <td><input type="button" value="'.$ro22['ai'].'" id="pres" name="pres" onclick="change(this.id);" onBlur="checkAvailability(this.id)" class="w3-button w3-teal"/></td>
    </tr>';
   }
   echo '</table>';
?>

接收文件:

代码语言:javascript
复制
 <?php
     $conToDatabase = ... // Here goes DB connection data

     if(isset($_GET['id']) && ctype_digit($_GET['id']))
     {
          $clean['id'] = $_GET['id'];
     }

     // Query your DB here using variable $clean['id'] as ID
     $querySuccess = ... 

     // if query successful echo 'OK';
     // else echo 'Not OK';
 ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47935875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档