AJAX调用后的PHP POST表单

时间:2018-09-01 18:17:18

标签: php ajax forms post

问题:我的php表单未提交。
这是我的页面:

enter image description here

它具有php呈现的颜色列表:

<div class="table-container">
   <div>
      <table class="myTable" id="myTable">
         <tr class="header">
           <th>Variants</th>
           <th>Size</th>
           <th>Price (€)</th>
         </tr>

              <?php                             
                $current_name = $_GET['prod-name'];
                $get_product_det = "SELECT * FROM product_details WHERE product_name='$current_name' ORDER BY position";
                $run_product_det = mysqli_query($con, $get_product_det);
                while ($row_product_det = mysqli_fetch_array($run_product_det)){
                $id = $row_product_det['id'];
                $product_variant = $row_product_det['product_variant'];
                $product_size = $row_product_det['size'];
                $product_price = $row_product_det['price'];
                $position = $row_product_det['position'];

                echo "<tr data-index='$id' data-position='$position'>
                      <td><a id='$id'>$product_variant</a></td>
                      <td>$product_size</td>
                      <td>$product_price</td>
                      </tr>";
                };  
              ?>
      </table>
   </div>

每种颜色都有唯一的ID。单击颜色会触发效果良好的AJAX脚本:

<div class='product-det-div' id='product_details'>
            <script>
              var links = document.getElementsByTagName('a');
              for (var i = 0, il = links.length; i < il; i++) {
                links[i].onclick = function() {
                  var id = this.id;
                  var product_details = document.getElementById('product_details');
                  var request = new XMLHttpRequest();

                  request.open('POST', 'product_details.php?variant_id=' + id, true);

                  request.onreadystatechange = function() {
                    if (request.readyState === 4 & request.status === 200) {
                      product_details.innerHTML = request.responseText;
                    } else {
                      product_details.innerHTML = 'An error occurred during your request: ' + request.status + ' ' + request.statusText;
                    }
                  };

                  request.send();
                };
              };
            </script>
     </div>

enter image description here

发生ajax调用时,页面的URL会发生什么?我正在通过URL传递颜色的ID,但是当尝试使用PHP进行获取时,似乎找不到它。 这是页面的代码:     

$current_id = $_REQUEST['variant_id'];

$get_product_det = "SELECT * FROM product_details WHERE id=$current_id";
$run_product_det = mysqli_query($con, $get_product_det);

while ($row_product_det = mysqli_fetch_array($run_product_det)){
$product_variant = $row_product_det['product_variant'];
$product_size = $row_product_det['size'];
$product_price = $row_product_det['price'];


    echo "
      <form action='' method='post'>
      <h2 style='margin-bottom: 20px;'>$product_variant</h2>
      <div><label>Nome Prodotto</label><input value='$product_variant'></div>
      <div><label>Dimensione</label><input value='$product_size' type='number' name='product_size'></div>
      <div><label>Prezzo (€)</label><input value='$product_price' id='product_price' name='product_price'></div>
      <button type='submit' name='edit_variant_btn'>Send</button>
      </form>
    ";

  if(isset($_POST['edit_variant_btn'])) {
      $variant = $_POST['product_size'];    
      $current_id = $_REQUEST['variant_id'];

      $update_size = "UPDATE product_details SET size = '$variant' WHERE id = '$current_id'"; 
      $run_update = mysqli_query($con, $update_size);

      if($run_update) {
  echo "<script>window.open('variable_product.php?prod-name=Polycolor', '_self');</script>";
  }
  }     


};

?>

感谢您的时间,感谢您的帮助。

编辑:我尝试了所有建议的更改,还尝试了$ _REQUEST [“ variant_id”],如Banujan Balendrakumar所说,但仍然没有结果。 我发现使它起作用的唯一方法是更改​​表单操作:

 <form action='' method='post'>

 <form action='product_details.php?variant_id=$current_id' method='post'>

之所以这样工作,是因为单击按钮会打开该页面并从那里获取ID值,但这只是解决问题的一种方法... 还有其他想法吗?

2 个答案:

答案 0 :(得分:0)

将$ _GET [“ variant_id”]替换为$ _REQUEST [“ variant_id”]

答案 1 :(得分:0)

enter image description here

它正在为我工​​作。...

<div class="table-container">
   <div>
      <table class="myTable" id="myTable">
         <tr class="header">
           <th>Variants</th>
           <th>Size</th>
           <th>Price (€)</th>
         </tr>

              <?php                  
                $con = new mysqli('localhost','root','','check');
                $get_product_det = "SELECT * FROM product_details ORDER BY position";
                $run_product_det = mysqli_query($con, $get_product_det);
                while ($row_product_det = mysqli_fetch_array($run_product_det)){
                $id = $row_product_det['id'];
                $product_variant = $row_product_det['product_variant'];
                $product_size = $row_product_det['size'];
                $product_price = $row_product_det['price'];
                $position = $row_product_det['position'];

                echo "<tr data-index='$id' data-position='$position'>
                      <td><a id='$id'>$product_variant</a></td>
                      <td>$product_size</td>
                      <td>$product_price</td>
                      </tr>";
                };  
              ?>
      </table>
   </div>
   <div class='product-det-div' id='product_details'>
            <script>
              var links = document.getElementsByTagName('a');
              for (var i = 0, il = links.length; i < il; i++) {
                links[i].onclick = function() {
                  var id = this.id;
                  var product_details = document.getElementById('product_details');
                  var request = new XMLHttpRequest();

                  request.open('POST', 'product_details.php?variant_id=' + id, true);

                  request.onreadystatechange = function() {
                    if (request.readyState === 4 & request.status === 200) {
                      product_details.innerHTML = request.responseText;
                    } else {
                      product_details.innerHTML = 'An error occurred during your request: ' + request.status + ' ' + request.statusText;
                    }
                  };

                  request.send();
                };
              };
            </script>
     </div>

产品详细信息

<?php

$current_id = $_REQUEST['variant_id'];
$con = new mysqli('localhost','root','','check');
$get_product_det = "SELECT * FROM product_details WHERE id=$current_id";
$run_product_det = mysqli_query($con, $get_product_det);

while ($row_product_det = mysqli_fetch_array($run_product_det)){
$product_variant = $row_product_det['product_variant'];
$product_size = $row_product_det['size'];
$product_price = $row_product_det['price'];


    echo "
      <form action='' method='post'>
      <h2 style='margin-bottom: 20px;'>$product_variant</h2>
      <div><label>Nome Prodotto</label><input value='$product_variant'></div>
      <div><label>Dimensione</label><input value='$product_size' type='number' name='product_size'></div>
      <div><label>Prezzo (€)</label><input value='$product_price' id='product_price' name='product_price'></div>
      <button type='submit' name='edit_variant_btn'>Send</button>
      </form>
    ";

  if(isset($_POST['edit_variant_btn'])) {
      $variant = $_POST['product_size'];    
      $current_id = $_REQUEST['variant_id'];

      $update_size = "UPDATE product_details SET size = '$variant' WHERE id = '$current_id'"; 
      $run_update = mysqli_query($con, $update_size);

      if($run_update) {
  echo "<script>window.open('variable_product.php?prod-name=Polycolor', '_self');</script>";
  }
  }     


};

?>