我想通過使用pdo和引導模式的js函數獲取數據,但我的問題是數據通過php函數不能正常進入json。 details.php沒有正確獲取數據,並且我在json解析中的getEventDetails函數中發現了未捕獲的語法錯誤。 這裏是我的代碼數據不是解析裏面的json
這裏我'獲取數據並進行更新功能
<html>
<table class="table table-bordered table-striped">
<form method="POST">
<tr>
<th>Event Name</th>
<th>Event Date</th>
<th>Update</th>
<th>Delete</th>
</tr>
<?php
include '../class.php';
$object = new crud();
$users = $object->readEvent();
if (count($users) > 0)
{
$number = 1;
foreach ($users as $user)
{
echo '<tr>
<td>'.$user['Event_Name'].'</td>
<td>'.$user['Event_Date'].'</td>
<td>
<button type="button" class="btn btn-warning" onClick="GetEventDetails('.$user['Event_ID'].')" class="btn btn-warning">Update</button>
</td>
<td>
<button type="button" onClick="DeleteEvent('.$user['Event_ID'].')" class="btn btn-danger">Delete</button>
</td>
</tr>';
}
}
?>
</form>
</table>
<!-- Modal-Update Event List-->
<div class="modal fade" id="update_event_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Update</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="update_event_name">Event Name</label>
<input type="text" id="update_event_name" class="form-control"/>
</div>
<div class="form-group">
<label for="update_event_date" class="col-xs-2 col-form-label">Date and time</label>
<input type="text" id="update_event_date" class="form-control" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="updateEvent()" >Save Changes</button>
<input type="hidden" id="hidden_event_id">
</div>
</div>
</div>
</div>
GetEventDetail功能
function GetEventDetails(id)
{
// Add User ID to the hidden field for furture usage
$("#hidden_event_id").val(id);
$.post("ajax/details.php",
{
id: id
},
function (data, status)
{
// PARSE json data
var user = JSON.parse(data);
// Assing existing values to the modal popup fields
$("#update_event_name").val(user.event_name);
$("#update_event_date").val(user.event_date);
}
);
// Open modal popup
$("#update_event_modal").modal("show");
}
在此之後它去details.php,代碼details.php
<?php
include '../class.php';
if(isset($_POST['id']) && isset($_POST['id']) != "")
{
$user_id = $_POST['id'];
//echo $id;
$object = new crud();
$object->EventDetails($user_id);
}
else
{
echo 0;
}
?>
它進入php函數。
public function EventDetails($user_id)
{
$query = $this->DB_conn->prepare("SELECT Event_Name, Event_Date FROM My_Event WHERE Event_ID = :id");
$query->bindParam("id", $user_id, PDO::PARAM_STR);
$query->execute();
return json_encode($query->fetch(PDO::FETCH_ASSOC));
}
更新事件溫控功能
function updateEvent()
{
// get values
var update_event_name = $("#update_event_name").val();
var update_event_date = $("#update_event_date").val();
//update_category_name = update_category_name.trim();
// get hidden field value
var id = $("#hidden_event_id").val();
// Update the details by requesting to the server using ajax
$.post("ajax/updatedata.php",
{
id: id,
update_event_name: update_event_name,
update_event_date: update_event_date
},
function (data, status)
{
alert("Data: " + data + "\nStatus: " + status);
$("#update_event_modal").modal("hide");
// read records again
readEvent();
// clear fields from the popup
$("#update_event_name").val("");
$("#update_event_date").val("");
}
);
}
updatedata.php
<?php
include("../class.php");
if (isset($_POST['update_event_name']) && isset($_POST['update_event_date']) && isset($_POST['id']))
{
$id = $_POST['id'];
$update_event_name = $_POST['update_event_name'];
$update_wedding_date = $_POST['update_event_date'];
$object = new crud();
$object->UpdateEvent($update_event_name, $update_wedding_date, $id);
}
else
{
echo 0;
}
?>
public function UpdateEvent($update_event_name, $update_wedding_date,$id)
{
$query = $this->DB_conn->prepare("UPDATE My_event SET Event_Name = :update_event_name, Event_Date = :update_wedding_date WHERE Event_ID = :id");
$query->bindParam("update_event_name", $update_event_name, PDO::PARAM_STR);
$query->bindParam("update_wedding_date", $update_wedding_date, PDO::PARAM_STR);
$query->bindParam("id", $id, PDO::PARAM_STR);
$query->execute();
}
看起來像'updateEvent()'是javascript,但是您正嘗試從PHP腳本調用它。這將是有用的知道你正在得到什麼錯誤 – RiggsFolly
沒有我正在使用兩個函數分開一個是JS和另一個PHP。 –
但是我沒有在你的crud類中看到UpdateEvent – RiggsFolly