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();
?>

相关推荐