无法将隐藏的表单值传递给php

时间:2014-07-20 12:35:43

标签: php html forms hidden

当我将隐藏值传递给php文件时,它给出(true)1作为答案。

我将模态中的值传递给php文件。

使用jquery检索范围值。

PHP代码:

<?php 
include "dbcon.php";
if(isset($_POST['pd_del']))
{
echo mysql_error();
$delid=isset($_POST['delidd']);
echo $delid;

}else
{
    echo mysql_error();
}
 ?>

HTML CODE:

  

表单将产品ID发送到php文件

<form name="prd_del" action="del_prod.php" method="post">  
<div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="delModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">DELETE PRODUCT</h4>
      </div>
      <div class="modal-body">
        <h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5> 
        <input type="hidden" name="delidd" id="delid">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary" name="pd_del" >Delete It!</button>
      </div>
    </div>
  </div>
</div>
</form>

1 个答案:

答案 0 :(得分:0)

您的HTML:

<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5> 
<input type="hidden" name="delidd" id="delid">

你的JS:

$(".modal-body #delid").text( pd_del_id);

第一个问题是你有2个具有相同id值的元素(sppan和输入字段)。你不应该。

更改您的一个ID,类似:

<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5> 
<input type="hidden" name="delidd" id="delid_value">

在你的JS中,如果我明白你想做什么:

$(".modal-body #delid").text(pd_del_id); // Here you put the product's ID in your span to show it to the user.
$(".modal-body #delid_value").val(pd_del_id); // Here you put the product's ID in your hidden field to send it with your form.

现在,您的PHP:

$delid=isset($_POST['delidd']);
echo $delid;
如果设置了变量,

isset()函数返回true或false。 变量$ _POST ['delidd']已设置(隐藏字段始终发送到您的PHP)。 如果您想获得价值(产品的ID):

if (!empty($_POST['delidd'])) {
// The value is not empty
   $delid = $_POST['delidd'];
} else {
// The value is empty : there is a problem (For example echo an error message or do whatever you have to do in that case.)
}