【发布时间】:2014-02-24 10:46:15
【问题描述】:
所以我可以证明我是个白痴/新手/。 我正在寻找一种让 ajax 将 ID 传输到模式的简单方法。 然后采用具有 php 的模态并提供要显示的必要变量。
例如
表格
===============================
[Button] | Data | Data | Data |
===============================
[点击按钮(ID)] -> Modal Pops up -> Name: Data , Email: Data, Username: Data
我不知道这是否有帮助。 我能够弄清楚如何让模态向数据库添加信息,但我似乎无法弄清楚如何将数据从 ID 提取到模态并填充它。
感谢您的帮助!
编辑:(更新) 这是我的索引页面,显示所有手机库存。 “视图”弹出模式但给了我随机信息,它是一个活动 ID,但不是当前订单的 ID。
希望这会有所帮助。 (我会接受任何帮助或批评)
<?php
include "../includes/db_connect.php";
$page = "chauffeur";
$pdo = Database::connect();
if($_SESSION['LoggedIn'] == 1){ }
elseif($_SESSION['LoggedIn'] == "")
{
header("location: http://wcl-wamp/");
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>WCL WebApp</title>
<link href="../css/bootstrap.css" rel="stylesheet">
<link href="../css/td/style.css" rel="stylesheet">
<style>
.body{margin: 0 40px; }
</style>
</head>
<body>
<?php include('../nav.php'); ?>
<div class="body">
<div class="row">
<h3><b>Phone Inventory</b></h3>
<div id="modal-results" ></div>
<?php
try {
$stmt = $pdo->prepare('SELECT * FROM phone_inventory');
$stmt->execute();
$result = $stmt->fetchAll();
if(count($result)) {
foreach($result as $row){
}
}
} catch (Exception $e) {
echo 'ERROR: ' . $e->getMessage();
} ?>
<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>
<h3 class="modal-title" id="myModalLabel"><b>Phone Profile - ID <?= $row['id']; ?></b></h3>
</div>
<div class="modal-body">
<form class="test" role="form">
<div class="form-group">
<label for="phone_number">Phone #
<input type="text" class="form-control" id="phone_number" name="phone_number" value="<?= $row['phone_number']; ?>"></label>
<label for="device_id">Device ID
<input type="text" class="form-control" id="device_id" name="device_id" value="<?= $row['device_id']; ?>"></label>
<label for="device_manufacturer">Device Manufacturer
<input type="text" name="device_manufacturer" id="device_manufacturer" class="form-control" value="<?= $row['device_manufacturer']; ?>" /></label>
<label for="device_model">Device Model
<input type="text" name="device_model" id="device_model" class="form-control" value="<?= $row['device_model']; ?>"/></label>
<label for="phone_alias">Phone Alias
<input type="text" name="phone_alias" id="phone_alias" class="form-control" value="<?= $row['phone_alias']; ?>"/></label>
<label for="chauffeur_number">Chauffeur #
<input type="text" name="chauffeur_number" id="chauffeur_number" class="form-control" value="<?= $row['chauffeur_number']; ?>"/></label>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="update">Check Out</button>
</div>
</div>
</div>
</div>
<div class="row">
<table class="table table-striped table-condensed table-hover">
<thead>
<tr>
<th></th>
<th>Phone #</th>
<th>Device ID</th>
<th>Device Manufacturer</th>
<th>Device Model</th>
<th>Phone Alias</th>
<th>Chauffeur #</th>
</tr>
</thead>
<tbody>
<?php $sql = 'SELECT * FROM phone_inventory ORDER BY id ASC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td><a class="btn btn-xs btn-primary" data-toggle="modal" data-id="'. $row['id'] .'" href="#myModal" >View</a></td>';
echo '<td>'. $row['phone_number'] .'</td>';
echo '<td>'. $row['device_id'] .'</td>';
echo '<td>'. $row['device_manufacturer'] .'</td>';
echo '<td>'. $row['device_model'] .'</td>';
echo '<td>'. $row['phone_alias'] .'</td>';
echo '<td>'. $row['chauffeur_number'] .'</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
<?php include('../includes/js_scripts.php'); ?>
<script>
$(document).ready(function() {
$('.table').dataTable( {
"sPaginationType": "bootstrap",
"iDisplayLength": 10
} );
} );
$(".device").click(function(){
var id = $(this).attr('data-id');
$("#myModal").find("#id").val(id);
$("#myModal").dialog("open");
})
$(".alert").delay(200).addClass("in").fadeOut(4000);
$(function() {
//twitter bootstrap script
$("button#update").click(function(){
$.ajax({
type: "POST",
url: "test.php",
data: $('form.test').serialize(),
success: function(msg){
$("#modal-results").html(msg)
$("#myModal").modal('hide');
},
error: function(){
alert("failure");
}
});
});
});
</script>
</body>
</html>
【问题讨论】:
-
到目前为止你得到了什么?发布您当前的代码。
标签: php ajax twitter-bootstrap modal-dialog