我想将数据发布到我的数据库中(通过controlpanel.php输入,使用name.php处理)但是当我使用jquery提交数据时,我的$ _POST值不会通过name.php。我是jquery的新手,我想弄清楚为什么会这样?
test_process.js
$('#insert_submit').on('click', function(event) {
alert("Hello");
event.preventDefault();
var insert_name = $('#insert_template_name').val();
$.post('ajax/name.php', {name: insert_name}, function(data) {
$('#insert_data').text(data);
});
});
AJAX / name.php
<?php
include('C:\xampp\htdocs\email1\class\db.php');
require_once('C:\xampp\htdocs\email1\config\db.php');
require_once('C:\xampp\htdocs\email1\class\Login.php');
$login = new Login();
$user = $_SESSION['user_name'];
$db = new Database();
$db->connect();
//test
echo $_POST['insert_template'];
echo '<br/>' . $_POST['insert_template_name'];
//insert
if (isset($_POST['insert_template'])) {
$template_name = $db->escape($_POST['insert_template_name']);
$template_description = $db->escape($_POST['insert_template']);
$db->insert('templates', array('template_name' => $template_name, 'template_description' => $template_description, 'userID' => $user));
echo "Inserted successfully";
}
Controlpanel.php
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<h2><u>Insert</u></h2>
<form action="ajax/name.php" name="insert_template_form" id="insert_template_form method="post">
<textarea rows="10" cols="25" name="insert_template" id="insert_template"></textarea> <br/>
Name your template: <input type="text" name="insert_template_name" id="insert_template_name"/><br/>
<input type="submit" name="insert_submit" value="submit" id="insert_submit"/></form>
<div id="insert_data"></div>
<script type="text/javascript" src="test_process.js"></script>
</body>
</html>
答案 0 :(得分:4)
您只需将一个参数传递给PHP代码:
$.post('ajax/name.php', {name: insert_name}, function(data) {
^^^^---here
并且您的PHP代码中没有{1 $_POST['name']
。因此,您的插入查询使用的是未定义的变量。