04 ajax执行php并传递参数
Posted heteng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了04 ajax执行php并传递参数相关的知识,希望对你有一定的参考价值。
js的方式
_this.value1 = "abc";
_this.value2 = 1;
$.ajax({ url: ‘xxxxxx.php‘,
type: ‘post‘,
data:{"value1":value1,"value2":value2},
dataType:‘text‘,
success: function()
{
alert("添加成功");
}
});
xxxxxx.php端
<?php
$servername = "服务器";
$username = "用户名";
$password = "密码";
$dbname = "数据库";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$temp=$_POST[‘content‘];//这里即可获取
$time=$_POST[‘time‘];//这里即可获取
$sql = "INSERT INTO mood (content, time)
VALUES (‘".$temp."‘,‘".$time."‘)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
以上是关于04 ajax执行php并传递参数的主要内容,如果未能解决你的问题,请参考以下文章