php - 根据 mysql 的输入填充表

标签 php mysql ajax mysqli

我有一个输入,其中输入了一个代码,并用来自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>

最佳答案

也许我在下面写了这样的东西。 添加了 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 
	}
?>

关于php - 根据 mysql 的输入填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683307/

相关文章:

javascript - ajax $.get 回调报告为未定义

java - 如何在 java 或 php 中解析一个非常大的 xml 文件并插入到 mysql 数据库中

php - Laravel 查询构建器具有 where 函数的限制

php - GDATA Feed API-用PHP解析时间戳

mysql - Sails + MySQL 重新连接并尝试

mysql - 在没有 super 权限的情况下在 MySQL 中执行事件(?)

php - 使用密件抄送和使用 wp_mail 抄送发送电子邮件

仅限今年的 MySQL(now() - 间隔 1 个月)

javascript - 我怎样才能消除差距

javascript - 通过ajax调用获取多选列表框值到spring Controller