如何调用 PHP 函数通过 Javascript 函数更新 mysql table 中的数据?
How can I call PHP function to update data in mysql table by Javascript function?
我正在尝试使用 javascript function.When 将数据更新到 mysql 数据库 我单击一个标签,此函数将调用 php 中的另一个函数做更新这些数据。如果我点击一个标签,代码就会完成这些工作。你能帮帮我吗?
我试图通过调用 publicatePhone php 函数在我的 sql table phone 中插入数据,但没有插入数据。这是我的代码:
**Javascript 函数:
function answer1yes(clicked) {
var s = document.getElementById("answer1oui").checked;
if (s = "true"){
<?php include('functions.php'); ?>
var x="<?php publicatePhone(); ?>";
alert(x);
return false;
}else{
alert('not checked');
}
}
** 文件 functions.php:
<?php function publicatePhone(){
$con=mysql_connect("localhost","root","hihi51978");
mysql_select_db("script_database2");
//$publicate = "INSERT INTO phone (phone_number, publicate_number) VALUES
('212661132084', 'yes')";
$publicate = "UPDATE phone SET publicate_number = 'yes' WHERE
phone_number='212661132084'";
$publicate_result = mysql_query($publicate);
if ($publicate_result==true){echo 'request executed successfelly';}
else {echo 'request not executed';}
}
?>
**形式:
<div id="answer1"style="margin: 20%;">
Show my phone number on website's result:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post" />
<p><label name="labelyes" style="background-color: #035d54; padding: 6px
30px 6px 30px; border-radius: 35px;" onclick="Javascript:answer1yes()" >
<input style="display: none;" type="radio" id="answer1oui" name="question1"
value="yes">Yes</label><p>
</form>
</div>
代码显示 "request executed successfelly" 但请求没有更新我的 phone table
所以,你的问题是 $publicate_result 似乎是真的,但它没有正确更新 SQL-table (或根本没有)?
如果是这样:$publicate_result 的实际值是多少?也许它有一个错误值,PHP 类型转换为 true,因为变量不为空。
当你回显你的变量时它说了什么 $publicate_result?
(你可以试试
echo var_dump($publicate_result);
获取有关变量值的更多信息)
我的意思是我认为通过 Javascript 调用 PHP 函数的最常见做法是使用 XMLHttpRequests,这也是我推荐的做法。
它是这样工作的:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "filename", true);
xhttp.send();
来源:https://www.w3schools.com/xml/xml_http.asp
如果你想使用 jQuery(我在使用 XMLHttpRequests 时推荐),执行 XMLHttpRequest 的语法要简单得多:
$.ajax({
type: 'POST',
url: 'yourfile.php',
data: {
// someData
},
success: (data) => {
// Code that will be executed after a successful
// finish of the request
// data: stuff that was echoed in PHP
},
error: (xhr, status, error) => {
// Code that will be executed after a non successful
// finish of the request
// This line will display an error to your console,
// which holds the PHP-error-code
console.error(xhr.responseText);
}
});
请注意,当您执行 XMLHttp- 或 Ajax-Request(简单来说基本相同)时,它是 asynchronous 这意味着, $.ajax() 命令之后的代码将在之后立即执行,即使请求尚未完成。
所以如果你想在请求完成后立即执行代码,你必须将代码放在 success 或 error回调函数。
如果您想查看有关如何在您的案例中使用 Ajax-requests 的更具体示例,请询问。
亲切的问候
好吧,这就是我处理类似事情的方式:
form.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="form.js"></script>
<title>Form</title>
</head>
<body>
<div id="answer1" style="margin: 20%;">
Show my phone number on website's result:
<!-- You only need a "post" and "action" attribute when you are -->
<!-- submitting this form with an: <input type="submit" value=""> -->
<form>
<button onclick="showPhoneNumberOnResults(true)">yes</button>
<button onclick="showPhoneNumberOnResults(false)">no</button>
</form>
</body>
</html>
form.js:
function showPhoneNumberOnResults(answer) {
if (answer) {
alert('Show number');
$.ajax({
type: 'POST',
url: 'process_answer',
data: { answer: answer },
success: (date) => console.log(data),
error: (xhr, status, error) => console.error(xhr.responseText)
});
} else {
alert('Don\'t show number');
}
}
这个(parameter) => { statements }这个东西叫做"lambda expression",它只是一种写函数的简写形式。
process_answer.php:
<?php
require_once 'database_connect.php';
$dbConnection->query(/* insert your UPDATE statement */);
database_connect.php:
<?php
$HOST = 'localhost';
$DATABASE = 'script_database2';
$USER = 'root';
$PASSWORD = 'hihi51978';
try {
$dbConnection = new PDO('mysql:host='.$HOST.';dbname='.$DATABASE, $USER, $PASSWORD);
// set the PDO error mode to exception
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
我这里用的是PDO。它只是提供了一个界面,您可以在其中使用每个不同的数据库,而不仅仅是 mysql。
您可以在这里阅读更多相关信息:http://php.net/manual/en/intro.pdo.php.
是的,顺便说一句,我认为您要使用的词是 "publish" 而不是 "publicate"。 :P
圣诞问候。
我正在尝试使用 javascript function.When 将数据更新到 mysql 数据库 我单击一个标签,此函数将调用 php 中的另一个函数做更新这些数据。如果我点击一个标签,代码就会完成这些工作。你能帮帮我吗?
我试图通过调用 publicatePhone php 函数在我的 sql table phone 中插入数据,但没有插入数据。这是我的代码:
**Javascript 函数:
function answer1yes(clicked) {
var s = document.getElementById("answer1oui").checked;
if (s = "true"){
<?php include('functions.php'); ?>
var x="<?php publicatePhone(); ?>";
alert(x);
return false;
}else{
alert('not checked');
}
}
** 文件 functions.php:
<?php function publicatePhone(){
$con=mysql_connect("localhost","root","hihi51978");
mysql_select_db("script_database2");
//$publicate = "INSERT INTO phone (phone_number, publicate_number) VALUES
('212661132084', 'yes')";
$publicate = "UPDATE phone SET publicate_number = 'yes' WHERE
phone_number='212661132084'";
$publicate_result = mysql_query($publicate);
if ($publicate_result==true){echo 'request executed successfelly';}
else {echo 'request not executed';}
}
?>
**形式:
<div id="answer1"style="margin: 20%;">
Show my phone number on website's result:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post" />
<p><label name="labelyes" style="background-color: #035d54; padding: 6px
30px 6px 30px; border-radius: 35px;" onclick="Javascript:answer1yes()" >
<input style="display: none;" type="radio" id="answer1oui" name="question1"
value="yes">Yes</label><p>
</form>
</div>
代码显示 "request executed successfelly" 但请求没有更新我的 phone table
所以,你的问题是 $publicate_result 似乎是真的,但它没有正确更新 SQL-table (或根本没有)?
如果是这样:$publicate_result 的实际值是多少?也许它有一个错误值,PHP 类型转换为 true,因为变量不为空。
当你回显你的变量时它说了什么 $publicate_result?
(你可以试试
echo var_dump($publicate_result);
获取有关变量值的更多信息)
我的意思是我认为通过 Javascript 调用 PHP 函数的最常见做法是使用 XMLHttpRequests,这也是我推荐的做法。
它是这样工作的:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "filename", true);
xhttp.send();
来源:https://www.w3schools.com/xml/xml_http.asp
如果你想使用 jQuery(我在使用 XMLHttpRequests 时推荐),执行 XMLHttpRequest 的语法要简单得多:
$.ajax({
type: 'POST',
url: 'yourfile.php',
data: {
// someData
},
success: (data) => {
// Code that will be executed after a successful
// finish of the request
// data: stuff that was echoed in PHP
},
error: (xhr, status, error) => {
// Code that will be executed after a non successful
// finish of the request
// This line will display an error to your console,
// which holds the PHP-error-code
console.error(xhr.responseText);
}
});
请注意,当您执行 XMLHttp- 或 Ajax-Request(简单来说基本相同)时,它是 asynchronous 这意味着, $.ajax() 命令之后的代码将在之后立即执行,即使请求尚未完成。
所以如果你想在请求完成后立即执行代码,你必须将代码放在 success 或 error回调函数。
如果您想查看有关如何在您的案例中使用 Ajax-requests 的更具体示例,请询问。
亲切的问候
好吧,这就是我处理类似事情的方式:
form.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="form.js"></script>
<title>Form</title>
</head>
<body>
<div id="answer1" style="margin: 20%;">
Show my phone number on website's result:
<!-- You only need a "post" and "action" attribute when you are -->
<!-- submitting this form with an: <input type="submit" value=""> -->
<form>
<button onclick="showPhoneNumberOnResults(true)">yes</button>
<button onclick="showPhoneNumberOnResults(false)">no</button>
</form>
</body>
</html>
form.js:
function showPhoneNumberOnResults(answer) {
if (answer) {
alert('Show number');
$.ajax({
type: 'POST',
url: 'process_answer',
data: { answer: answer },
success: (date) => console.log(data),
error: (xhr, status, error) => console.error(xhr.responseText)
});
} else {
alert('Don\'t show number');
}
}
这个(parameter) => { statements }这个东西叫做"lambda expression",它只是一种写函数的简写形式。
process_answer.php:
<?php
require_once 'database_connect.php';
$dbConnection->query(/* insert your UPDATE statement */);
database_connect.php:
<?php
$HOST = 'localhost';
$DATABASE = 'script_database2';
$USER = 'root';
$PASSWORD = 'hihi51978';
try {
$dbConnection = new PDO('mysql:host='.$HOST.';dbname='.$DATABASE, $USER, $PASSWORD);
// set the PDO error mode to exception
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
我这里用的是PDO。它只是提供了一个界面,您可以在其中使用每个不同的数据库,而不仅仅是 mysql。 您可以在这里阅读更多相关信息:http://php.net/manual/en/intro.pdo.php.
是的,顺便说一句,我认为您要使用的词是 "publish" 而不是 "publicate"。 :P
圣诞问候。