dousui6193 2018-12-03 15:37
浏览 114

将复选框的值传递给PHP中嵌入的HTML中的Javascript

Good afternoon. I am trying to pass the values associated with a list of checkboxes to a javascript function that then calculates the total for all values associated with selected checkboxes.

The HTML code is embedded within PHP. When I try to call the javascript function, I get a null result. Could anyone provide some pointers on where I am going wrong. Code below. All help is appreciated! Thank you.

The variables are pulled from the results of an SQL query executed just before the copied code below.

"<form action = 'book.php' method = 'POST'>
  <tr><td>$seat</td>"
  ."<td>$zone</td>"
  ."<td>$price</td>"
  ."<td><input type = 'checkbox' id = 'seat['$seat']' value = '$price'></td></tr>"
  //."<input type = 'hidden' id = '$seat' value = '$price' action = 'checkDetails()'>"
  ;
}

echo "</table>"
      ."Email address for booking ".
      "<input type = 'text' name = 'email id'>"
      ."&emsp;&emsp;"
      ."<button type = 'submit' name = 'book' action = 'book.php'>Book</button></form>"
      ."&emsp;&emsp;"."<button name = 'check price' onclick = 'checkDetails()'>Check Prices</button>";



    } 
    catch (PDOException $e) {
        echo "PDOException: ".$e->getMessage();
    }

echo "<script language = 'javascript'>

  function checkDetails() {
  var seat = document.forms[0];
  var i;
  for (i = 0; i < seat.length; i++) {
    if (seat[i].checked) {
        var total = document.getElementById('seat[i]');
        alert('Your total is ' + total);
    }
}
}

</script>";
  • 写回答

1条回答 默认 最新

  • doudieyou5209 2018-12-03 15:50
    关注

    You are looping through the forms length.

    Since this is rendered in php why dont you set the length of of I programmatically

    var i;
    var length = <?= $seat?>;
    for (i = 0; i < length; i++) {
    if ( document.getElementById('seat[i]').checked) {
        var total = document.getElementById('seat[i]');
        alert('Your total is ' + total.value);
    }
    

    the following code is looping through the form and not the elements

    var seat = document.forms[0];
      var i;
      for (i = 0; i < seat.length; i++) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大