我疯了!我正在尝试将一个表单从jquery提交到php并在我的数据库中插入一条记录。我没有收到任何错误,但没有提交任何记录。当我转到php页面并将变量放入URL时,一切正常。但是当我通过jquery页面提交变量时,它不起作用。有人可以帮帮我吗?
HTML:
<form id="tellusForm" >
<div class="formHead">Tell us your story</div>
<div id="thank">Thank you.</div>
<table cellpadding="5">
<tr><td>Name:</td><td><input id="tellName" type="text"/></td></tr>
<tr><td>Country of origin:</td><td><select id="tellCountry"></select></td></tr>
<tr><td>Age:</td><td><input type="text" id="tellAge"/></td></tr>
<tr><td>Occupation:</td><td><input type="text" id="tellOccupation"/></td></tr>
<tr><td>Email address:</td><td><input type="text" id="tellEmail"/></td></tr>
<tr><td>Phone number:</td><td><input type="text" id="tellPhone"/></td></tr>
<tr><td>A bit about you:</td><td><textarea id="tellAbout" style="width: 100%;"></textarea></td></tr>
<tr><td></td><td><input type="submit" value="Send" class="submit"/></td></tr>
</table>
</form>
jquery的:
$('.submit').click(function(){
$.get('tellus.php', { name: "laura"}, function(data){
eval(data);
});
});
tellus.php:
<?php
require_once ('../constants_test.php');
$name = $_GET['name'];
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit;
}
$q = "Insert into tellus(`Name`) values ('" . $name . "')";
if ($db->query($q)) {
echo "console.log('you got it')";
};
$db->close();
?>
答案 0 :(得分:1)
感谢所有试图帮助我的人!我最终不得不这样做:
$.post("./tellus.php", {tellName:name}, function(data){
alert(data)
});
ajax调用必须有一些无效的东西。
答案 1 :(得分:0)
使用serialize():
$("#tellusForm").on("submit", function() {
event.preventDefault();
$.ajax({
url: 'tellus.php',
data: $(this).serialize()
}).done(function(data) {
$(html).append(data);
});
});
还要确保在表单元素上有名称:
<input type="text" name="tellAge" id="tellAge" />
这个主题在这里很常见,所以请尝试搜索其他帖子 - 例如Submit form using AJAX and jQuery也可以发挥作用。
答案 2 :(得分:0)
ajax调用无法正常工作,因为您正在使用$.serialize
序列化您将返回的表单对象;
{key:'tellName', value='name'} // <input name="tellName" value="name" />
而您期待key:value
对。为了使数据能够发送您期望的内容,您应该使用此jQuery函数修改对象:
$.fn.serializeObject = function(){
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
}
只要您提交表单,就拨打电话。它会将您的表单结构化为key:value
对。
<form id="register">
<input type="text name="username" />
<input type="text" name="email" />
<input type="submit value="Register!" />
</form>
和javascript(记得包含serializeObject
函数)
$('#register').on('submit', function(){
var data = $(this).serializeObject()
// data will return {username: value, email:value}
$.post('process.php', data, function(response){
try{
var r = JSON.parse(response) // Check if the response is an object
console.log(JSON.stringify(r))
}
catch(e){
console.log(response) // The response is a string
}
})
})
干杯!
答案 3 :(得分:-1)
I suggest you should use POST for actions which include insert/delete/update of data in database. It keeps the data safe.
If you want to still use GET and test out something. I suggest do not use jquery in the middle to handle the get. Submit button on click submits the form anyway, so the jquery $.get is unnecessary.
To your form element add
<form name='' action='tellus.php'>...</form>
This should fix the problem.
答案 4 :(得分:-1)
这个怎么样
jquery的
$("#submit").click(function() {
formData = {
name: "abc"
}
$.ajax({
type: 'GET',
contentType: 'application/json',
url: "http://yourpage/tellus.php",
dataType: "json",
data: formData,
success: function(data) {
console.log(data.message);
},
error: function(data) {
console.log(data.message);
}
});
});
PHP
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_GET['name'])) {
$name = $_GET['name'];
// include db connect class
require_once ('../constants_test.php');
// connecting to db
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit;
}
// mysqli inserting a new row
$q = "INSERT INTO tellus('Name') values ('" . $name . "')";
// check if row inserted or not
if ($db->query($q)) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "you got it";
// echoing JSON response
echo json_encode($response);
$db->close();
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
$db->close();
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>