首页 / AJAX / php – 使用ajax提交表单
php – 使用ajax提交表单
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用ajax提交表单,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2479字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试使用jquery向数据库提交值.我是ajax的新手,但我需要用ajax来做.
这是我到目前为止我的PHP代码所做的
function insertSeries()
{
$options = array(
'user' => $_POST['user'],
'email' => $_POST['email'],
'summary' => $_POST['summary'],
'due_date' => $_POST['due_date'],
'problem_type' => $_POST['problem_type'],
'status' => $_POST['status']
);
$sql = "insert into ticket_summary('user','email','summary','due_date','problem_type','status') Values (?,?,?,?,?,?)";
$result = mysql_query($sql, $options) or die('Could not insert data');
}
我的HTML代码是
<?php
include 'eric_api.php';
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/jquery.js"></script>
<script src="js/api_calls.js"></script>
<link rel="stylesheet" href="css/normalizer.css" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="wrapper">
<h1>Ticketing System</h1>
<div>
<div id="ticket_form_wrapper">
<form id="insert_ticket" method="post" action="">
<p>
<label for="user">User</label>
<br />
<input type="user" id="user" class="post_fields" />
</p>
<p>
<label for="email">Email</label>
<br />
<input type="email" id="email" class="post_fields" />
</p>
<p>
<label for="summary">Summary</label>
<br />
<input type="summary" id="summary" class="post_fields" />
</p>
<p>
<label for="due_date">Due Date</label>
<br />
<input type="due_date" id="due_date" class="post_fields" />
</p>
<p>
<label for="problem_type">Problem Type</label>
<br />
<input type="problem_type" id="problem_type" class="post_fields" />
</p>
<p>
<label for="status">Status</label>
<br />
<input type="status" id="status" class="post_fields" />
</p>
<p>
<input type="submit" id="submit" value="Submit" />
<input type="button" onclick="window.location='index.php'" value="Go to List"/>
<div class="form_result"> </div>
</p>
</form>
</div>
</div>
</body>
</html>
这是我使用jquery的ajax
$('#insert_ticket').submit(function(e){
var postData = $(this).serialize();
alert(postData);
$.ajax({
type: 'POST',
url: 'http://localhost/api/eric_api.php?q=insertseries',
data: postData,
success: function(response){
$('#insert_ticket').find('.form_result').html(response);
},
error: function(){
alert('error');
}
});
e.preventDefault();
});
我不知道我做错了什么.任何帮助将不胜感激
解决方法:
而不是干扰表单的提交事件,而是干扰点击事件.要对现有设置进行最小的更改,只需将单击处理程序添加到表单提交按钮即可.处理程序内部的第一件事,调用e.preventDefault().您必须在处理程序中显式选择表单以序列化数据.然后,将现有的ajax调用移动到该处理程序中,它应该可以正常工作.此外,请确保您实际上在PHP代码中的某处调用insertSeries().
干杯
内容总结
以上是互联网集市为您收集整理的php – 使用ajax提交表单全部内容,希望文章能够帮你解决php – 使用ajax提交表单所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。