weixin_33724570 2018-09-01 18:17 采纳率: 0%
浏览 31

AJAX调用后的PHP POST表单

Problem: My php form does not submit.
This is my page:

enter image description here

It has a php rendered list of colors:

<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>

Each color has a unique ID. Clicking on a color fires the AJAX script that works fine:

<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

When the ajax call happens, what happens to the URL of the page? I'm passing the ID of the color through the URL but when trying to GET it with PHP it seems it doesn't find it. Here's the code of the page:

$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>";
  }
  }     


};

?>

Thank you for your time, any help appreciated.

EDIT: I tried all the changes you all adviced, also tried $_REQUEST["variant_id"] as Banujan Balendrakumar said, but still no result. The only way I found to make it work was to change the form action from this:

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

to

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

This way it works because on button click, it opens that page and gets the ID value from there but it's just a way to get around the problem... Any other ideas?

  • 写回答

2条回答 默认 最新

  • weixin_33721427 2018-09-01 19:47
    关注

    replace $_GET["variant_id"] to $_REQUEST["variant_id"]

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿