【问题标题】:Fill table from input from mysql从mysql的输入填充表
【发布时间】:2015-06-23 08:47:54
【问题描述】:

我有一个输入,其中输入了一个代码,并用来自mysql optenida的信息填充了下表,问题是我希望每次输入一个代码,表中的所有数据都添加(不删除前面的)。我有了使用 Ajax 的想法,但不知道从哪里开始。所以你看到有一种我没有看到的更简单的方法(在谷歌上找到)。我不喜欢将这些数据添加到表中,我希望它是临时的(直到表被确认,才会添加到数据库中)。

有什么想法吗?

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
  <style>
    table {
        width:100%;
        border: 1px solid black;
        border-collapse: collapse;
    }

    td {
        border: 1px solid black;
        border-collapse: collapse;
    }
  </style>
</head>
<body>
 <form action="index.php" method="post">
   <input type="text" name="input_codigo" placeholder="Codigo del producto" autocomplete="off" autofocus required><br><br>
 </form>
 <table>
   <tr>
     <td><b>Codigo</b></td>
     <td><b>Descripcion</b></td>
     <td><b>Precio</b></td>
     <td><b>Cantidad</b></td>
     <td><b>Importe</b></td>
   </tr>
     <?php
     session_start();
     error_reporting(E_ALL ^ E_NOTICE);
     require ("conectar.php");
     $_SESSION["codigo"] = $_POST["input_codigo"];
     $sql = "SELECT * FROM $tabla WHERE codigo = ".$_SESSION['codigo']."";
     $result = $conexion->query($sql);

     if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {

          echo "
          <tr>
            <td>".$row["codigo"]."</td>
            <td>".$row["descripcion"]."</td>
            <td>$".$row["venta"]."</td>
            <td><input type='number' name='cantidad' value='1' min='1' max='5'></td>
            <td>$".$row["venta"]."</td>
          </tr>
          ";
         }
     } else {
          echo "";
     }
     $conexion->close();

     ?>
 </table>
</body>
</html>

【问题讨论】:

  • 考虑将此任务分解为可管理的子任务:1)从数据库中获取数据,2)将数据转换为 JSON 等 AJAX 友好格式,3)通过 AJAX 将数据发送到客户端, 和 4) 将 AJAX 结果转换为 HTML 表格。这些任务中的每一个都比较常见,因此您可以在本网站和其他地方找到数百个代码示例来指导您。
  • @GeorgeCummins 非常感谢您现在开始搜索,希望有人可以帮助我提供一些代码。最好的问候。

标签: php mysql ajax mysqli


【解决方案1】:

也许像这样我写在下面。 添加了 jQuery 和 Ajax 请求以获取数据,然后将其添加到表中。 稍微更改了 PHP,这样如果是 AJAX 请求,则不会返回主 HTML。

希望它对你有用(我没有测试过)。

<?php
   session_start();
   error_reporting(E_ALL ^ E_NOTICE);

	$bAjaxRequest = false;
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
		$bAjaxRequest = true;
	}
	// if not and ajax request deliver the complete HTML
	if(!$bAjaxRequest) {
?>
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
  <style>
    table {
        width:100%;
        border: 1px solid black;
        border-collapse: collapse;
    }

    td {
        border: 1px solid black;
        border-collapse: collapse;
    }
  </style>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
 <form action="index.php" method="post" id="frmQuery" name="frmQuery">
   <input type="text" name="input_codigo" id="input_codigo" placeholder="Codigo del producto" autocomplete="off" autofocus required><br><br>
 </form>
 <table id="tblData" name="tblData">
   <tr>
     <td><b>Codigo</b></td>
     <td><b>Descripcion</b></td>
     <td><b>Precio</b></td>
     <td><b>Cantidad</b></td>
     <td><b>Importe</b></td>
   </tr>
     <?php
	} // end if(!$bAjaxRequest) {
	// we are always going to return the TR's or ""
     require ("conectar.php");
    
     // ALWAYS, BUT ALWAYS VERIFY/VALIDATE USER INPUT!!!
     $_SESSION["codigo"] = mysql_real_escape_string($_POST["input_codigo"]); // for example
     
     $sql = "SELECT * FROM $tabla WHERE codigo = ".$_SESSION['codigo']."";
     $result = $conexion->query($sql);

     if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {

          echo "
          <tr>
            <td>".$row["codigo"]."</td>
            <td>".$row["descripcion"]."</td>
            <td>$".$row["venta"]."</td>
            <td><input type='number' name='cantidad' value='1' min='1' max='5'></td>
            <td>$".$row["venta"]."</td>
          </tr>
          ";
         }
     } else {
          echo "";
     }
     $conexion->close();

	// if not and ajax request deliver the complete HTML
	if(!$bAjaxRequest) { ?>
 </table>
 <script type="text/javascript">
 function loadData(codigo) {
 		$.post( "index.php", { input_codigo: codigo }, function( data ) {
  		$("#tblData").append(data);
		});
 }
 
 	$(function() {
  	// jQuery POST are never cached, but if you change to GET you'll need this next line
  	//$.ajaxSetup ({ cache: false });
  	
  	$("#frmQuery").submit(function(e) {
  		e.preventDefault();
  		loadData($("#input_codigo").val());
  	});	
	});
 </script>
</body>
</html>
<?php 
	}
?>

【讨论】:

    猜你喜欢
    • 2018-03-08
    • 2014-04-15
    • 2015-08-22
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多