gpt4 book ai didi

php - 在不刷新/离开页面的情况下更新记录

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:18 26 4
gpt4 key购买 nike

我有一个 PHP 页面,里面有一个表格。

该表由 MySQL 数据库中的记录填充。表(房屋)的一个字段可以包含两个值:01

当学生被安置时,该字段的值为 1,否则为 0。在表格中,我想使用带有 O/I 的 JQUERY UI 按钮(就像一个开关)。

单击按钮时,需要更新 MySQL 表中的值,如果值是 1 否则图标应该消失。

我假设我需要 ajax 来执行此操作?
谁能告诉我这是否可以完成?以及如何实现?

最佳答案

可以的

这里有一个例子 http://openenergymonitor.org/emon/node/107

  1. Create a php script called api.php on your server
  2. Copy and paste the example below and save it:
<?php 

//--------------------------------------------------------------------------
// Example php script for fetching data from mysql database
//--------------------------------------------------------------------------
$host = "localhost";
$user = "root";
$pass = "root";

$databaseName = "ajax01";
$tableName = "variables";

//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
include 'DB.php';
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);

//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); //query
$array = mysql_fetch_row($result); //fetch result

//--------------------------------------------------------------------------
// 3) echo result as json
//--------------------------------------------------------------------------
echo json_encode($array);

?>

然后

  1. Create a html script called client.php in the same directory with the following content in it:
<!---------------------------------------------------------------------------
Example client script for JQUERY:AJAX -> PHP:MYSQL example
---------------------------------------------------------------------------->

<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
</head>
<body>

<!-------------------------------------------------------------------------
1) Create some html content that can be accessed by jquery
-------------------------------------------------------------------------->
<h2> Client example </h2>
<h3>Output: </h3>
<div id="output">this element will be accessed by jquery and this text replaced</div>

<script id="source" language="javascript" type="text/javascript">

$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
url: 'api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var id = data[0]; //get id
var vname = data[1]; //get name
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
//recommend reading up on jquery selectors they are awesome
// http://api.jquery.com/category/selectors/
}
});
});

</script>
</body>
</html>

关于php - 在不刷新/离开页面的情况下更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com