【发布时间】:2016-05-09 00:59:53
【问题描述】:
由于某种原因,我的 form 中的 POST 变量始终为空。这是我的代码...
我有一个引导选择,它返回在从下拉列表中选择新项目时显示的信息(选择)。
代码:
$(function() {
$("#shiftsDD").change(function(){
var selectedItemId = $('#shiftsDD option:selected').val();
$.post('/step/services/shiftInfo.php', { shiftID : selectedItemId }, function(res){
$("#shiftCard").show();
var jsonRes = $.parseJSON(res);
$("#address").html(jsonRes[0]);
$("#duties").html(jsonRes[1]);
$("#employer").html(jsonRes[4]);
$("#payRate").html(jsonRes[5]);
$("#theShiftID").html(jsonRes[6]);
console.log(res);
});
});
});
所有来自 jsonRes 数组的值都在这里赋值:
<form role="form" name="cardForm" method="POST">
<div class="card" id="shiftCard">
Employer:
<h3 id="employer">Test Employer</h3><br>
Address:
<h3 id="address">Test Address</h3><br>
Duties include:
<h3 id="duties">Test duties</h3><br>
Pay rate:
<h3 id="payRate">Test PayRate</h3><br>
<h5 id="theShiftID" name="myShiftID"></h5>
<br>
<button type="submit" class="btn btn-lg btn-green-solid" name="shiftsBtn">Accept work</button>
</div>
</form>
我尝试了很多东西,但我是 ajax 新手,不是 PHP 专家,所以我不知道为什么:
if (isset($_POST['shiftsBtn'])) {
$theId = $_POST['myShiftID'];
...
myShiftID 始终为空
ShiftsDD 选择代码:
<select name="shiftsDD" id="shiftsDD" class="form-control form-control-45">
<option value="0">Select a shift</option>
<?php
$theQuery = "
SELECT DATE_FORMAT(StartTime, '%d/%b/%Y' ) AS ShiftDate, ShiftID,ShiftTitle,TIMESTAMPDIFF(MINUTE, StartTime, EndTime) AS ShiftDuration, ROUND(PayRate,2) as PayRate FROM Shifts;
";
$result = $DBH->query($theQuery);
$result->execute();
while($r = $result->fetch()) {
$theDuration = $r['ShiftDuration'] / 60;
echo "<option value='" . $r['ShiftID'] ."'>[". $r['ShiftDate'] . "] " . $r['ShiftTitle'] . " (Hours: " . $theDuration . ")</option>";
}
?>
</select>
【问题讨论】:
-
添加了它,我在控制台中没有错误。我从 ajax 接收并显示我想要的值,但是当我按下提交按钮时,我得到 post 变量为空(未定义的索引),你可以看到上面的代码似乎很好
-
<h5 id="theShiftID" name="myShiftID"></h5>- 据我所知,H 元素不能作为(POST-able)输入。您可能可以使用 DOM 来执行此操作,但不能按照您现在的方式执行此操作。 “未定义索引” 我知道你会得到,这就是我要求你使用错误报告的原因。 -
能否请您张贴html表格。
-
@Brett 我在我的问题中做了。弗雷德,有一个建议,为什么
$("#theShiftID").html(jsonRes[6]);如果我将其更改为输入文本,为什么不将我的变量放在我的 h5 中?我试过了
标签: php jquery ajax post undefined