为每行指定按钮的值

时间:2016-09-28 22:45:34

标签: php

我正在尝试使用php,我真的很陌生。

我创建了一个页面(使用php代码)将信息发送到数据库,一个页面显示数据库中的信息。

我的问题是:假设我要输入10个信息,但我只显示其中的5个,如何显示其余信息点击每行按钮?

我是否必须为按钮指定值?我无法理解如何将按钮与行相关联。

基本上对于每一行,我想显示该特定行的其余信息,点击该特定按钮。

任何帮助都会适用。感谢

-----在db中插入信息的脚本-----

<?php

$name = mysqli_real_escape_string($link, $_POST['name']);
$mail = mysqli_real_escape_string($link, $_POST['mail']);
$number = mysqli_real_escape_string($link, $_POST['number']);
$device = mysqli_real_escape_string($link, $_POST['device']);
$model = mysqli_real_escape_string($link, $_POST['model']);
$problem = mysqli_real_escape_string($link, $_POST['problem']);

        $sql = "INSERT INTO cj (name, mail, number, device, model, problem) VALUES ('$name', '$mail', '$number', '$device', '$model', '$problem')";
        $result = mysqli_query($link, $sql);
        // if query fails stop script and echo error
    if( $result === false)
    {echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        exit;
    }        
    header("location:add-customer.php?message=A job and customer has been added to the database");

    exit;
        echo "You'll never see this";
    ?>

-----------页面显示信息------

     <?php
$sql = "SELECT * from "database name"";
    echo "
<table class='table'>
    <thead>
        <tr>";
/* Get field information for all columns */

    echo "<form action='' method=post>";
    echo "<tr class='info'>

                 <input type=hidden name=hidden value=" . $row['id'] . ">
                <td>" . $row['id'] . "</td> 
                <td>" . $row['device'] . "</td>
                <td>" . $row['model'] . "</td> 
                <td>" . $row['problem'] . "</td>
                <td>

            </td>        

                <td> 
                <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal' value= " . $row['id'] . " >  Info</a></form></td>

           </tr>"; 
    echo "</form>";


}
echo "
    </tbody>

</table>";

?>

<div class="container">
  <!-- Trigger the modal with a button -->
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog modal-lg">
  <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Customer Information</h4>
        </div>

        <div class="modal-body">

       <?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
    die('Unable to connect to database [' . $link->connect_error . ']');
}
$sql = "SELECT name,mail,number from databasename WHERE **???**;


if (!$result = $link->query($sql)) {

    die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
    <thead>
        <tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
    echo "
        <th>" . $finfo->name . "</th>";
}
echo "
        </tr>
    </thead>
    <tbody>";
while ($row = $result->fetch_assoc()) {
    echo "<tr class='info'>

                <td>" . $row['name'] . "</td>
                <td>" . $row['mail'] . "</td>
                <td>" . $row['number'] . "</td>

    </tr>";

...

2 个答案:

答案 0 :(得分:0)

连接到数据库并使用PHP获取所有10个信息。但只显示5个信息。在那里放一个按钮,并使用Javascript或Jquery显示其他行。 (Onclick事件)

答案 1 :(得分:0)

我找到了一个使用ajax的解决方案:

这里是代码:( ajax调用第三个解决我问题的php页面)

  <script>
    $(document).ready(function(){
      $('.get_info').click(function(){
        var job_id = $(this).parent().siblings('input[name=hidden]').val();
        $.ajax({
          url: 'THIRDPAGE.php',
          data: 'job_id=' + job_id,
          type: 'POST',
          success: function(data){
            // console.log(data);
            $('.info_data').html(data);
          }
        });
      });
    });
  </script>

对于来到这里的人来说只是评论,我可以更好地解释我做了什么。