以下是我的代码,
<div class="col-md-12" role="main">
<div class="bs-docs-section">
<h1 class="page-header">Today's Activity <button class="btn btn-primary">Add new</button></h1>
</div>
<?php
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE staffID= '" . $staff_information["staff_Id"] . "' AND day='" . $day . "' ");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-3">
<button class="btn btn-warning" data-toggle="modal" data-target="#myModal" schedule="
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</button>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Insert Today's Activity</h4>
</div>
<div class="modal-body">
<p class="activity"></p>
<p>Click on the submit button if the above infomation is correct.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary btn-ok SUB" >Submit</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#myModal').on('show.bs.modal', function (e) {
$(this).find('.btn-ok').attr('schedule', $(e.relatedTarget).attr('schedule'));
$('.activity').html('You have selected: <strong>' + $(this).find('.btn-ok').attr('schedule') + '</strong>');
});
$(document).ready(function(){
$(".SUB").click(clickSUB);
})
function clickSUB(){
$("#processSUB").load("hello.php");
}
</script>
<div id="processSUB"></div>
<?php
}
mysqli_close($con);
}
?>
</div>
我的hello.php代码,
<?php
echo 'You have submitted';
echo $schedule['timetableId'];
?>
&#13;
有没有办法获得&#39; timetableId&#39;从按钮?
答案 0 :(得分:2)
检查load()的第二个参数以将数据作为参数传递,您可以传递timetableId
或任何其他所需参数。
load('hello.php', { timetableId : javascriptVarWithValue });
您将在hello.php
中的$_POST
中收到传递的数据
在hello.php中,您可以访问:
<?php echo $_POST['timetableId']; ?>
在循环中,加载timetableId
之类的内容:
<div id="tableID" data-timetable-id="<?php $schedule['timetableId']; ?>" style="display: none;"></div>
在clickSUB()
中获取如下值:
function clickSUB(){
// changed to data('timetableId') instead of data('timeTableId')
var timeTableId = $('#tableID').data('timetableId');
console.log(timeTableId);
$("#processSUB").load("hello.php", { timetableID : timeTableId });
}