【问题标题】:Can't retrieve numeric input and assign it to a variable无法检索数字输入并将其分配给变量
【发布时间】:2021-08-13 00:52:25
【问题描述】:

我正在尝试检索数字输入并将其存储在变量 q 中,但我似乎无法使用 post 这种方式访问​​它。

<?php 
$sql = "SELECT * FROM produit";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){

?>
<div class="prod<?php echo $row['ID_produit']?>">
<img src="images/<?php echo $row['image']?>" alt="image">
<h3><?php echo $row['nom_produit']?></h3>
<h3>Prix:<?php echo $row['prix']?>&euro;</h6>
<form action="" method="post">
<label name="qte">QTE:</label>
<input type="number" name="qte"><br><br>

<a href="panier.php?action=ajout&amp;
l=<?php echo $row['nom_produit']?>&amp;
q=<?php $_POST['qte']?>&amp;
p=<?php echo $row['prix']?>" 
onclick="window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=yes, 
scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350'); return false;">
GOGOGO  </a>
</form>
</div>
<?php
}
?>
 </div>

我也试过

<?php
 if (isset($_POST['submit'])) {
 $example = $_POST['qte'];
 echo $example;
 }
 ?>

【问题讨论】:

  • q=?这一切都是在提交表单后发生的吗?或者您是否尝试在单击 GOGO 链接时获取该输入元素的值?
  • @Kinglish 在我点击 GOGO 链接后,我被发送到另一个页面,它显示了我想要的变量 l 和 p 但不是 q 的值。虽然当我给 q 一个设定值,比如 2 时,它会起作用。
  • 该表单上没有提交按钮,$_POST['qte'] 很可能永远不会设置

标签: php variables input


【解决方案1】:

此代码存在几个问题。您试图通过$_POST 变量在每个循环中引用qte,而您真正想要的只是获取用户的输入。为了稍微清理一下代码并使内容更具可读性,我更改了那个大的 &lt;a href... 标记以触发一个函数,该函数将获取 qte 值,形成 URL 并打开窗口。请注意,我将 productIDprix 值传递给函数。我把它们放在引号中,以防它们是空白的。我还在您的qte input 元素中添加了一个ID,这样我们就可以获得该值。看看,如果您需要澄清,请告诉我

<?php 
$sql = "SELECT * FROM produit";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){    
?>
<div class="prod<?php echo $row['ID_produit']?>">
<img src="images/<?php echo $row['image']?>" alt="image">
<h3><?php echo $row['nom_produit']?></h3>
<h3>Prix:<?php echo $row['prix']?>&euro;</h6>
<form action="" method="post" onsubmit="return false;">
<label for="qte">QTE:</label>
<input type="number" name="qte" id="qte_<?php echo $row['ID_produit']?>"><br><br>

<a href='javascript:void(0)' 
  onclick='gogoLink("<?php echo $row['ID_produit']?>", "<?php echo $row['prix']?>")'>
GOGOGO  </a>
</form>
</div>
<?php
}
?>

// then outside the loop is the function 

<script>
function gogoLink(id, prix) {
  let url ="panier.php?action=ajout"
  url += "&l=" + id
  url += "&q=" + document.getElementById("qte_"+id).value;
  url += "&p=" + prix;

  window.open(url, '', 'toolbar=no, location=no, directories=no, status=yes, 
  scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350'); 
}
</script>

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2016-06-14
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多