I am kicking myself in the arse here because i can't for the life of me figure it out...this is supposed to be a quick and dirty project, but, I decided i want to try something new, and I have little to no experience with the AJAX methods in jQuery...I spent quite literally 5 days trying to learn and understand how to appropriately implement the AJAX calls, but to know avail...I learned some basic stuff, but not what i need to execute the code below.
我在这里屁股踢我自己,因为我不能为我的生活弄清楚...这应该是一个快速和肮脏的项目,但是,我决定我想尝试新的东西,我有一点点在jQuery中没有使用AJAX方法的经验...我花了5天的时间试图学习和理解如何正确实现AJAX调用,但要知道有用......我学到了一些基本的东西,但不是我需要的东西执行下面的代码。
Again, I am wondering how to go about converting this standard request to AJAX using jQuery...
再次,我想知道如何使用jQuery将此标准请求转换为AJAX ...
here is my form & php
这是我的表格和PHP
HTML:
<form action="categories.php?action=newCategory" method="post">
<input name="category" type="text" />
<input name="submit" type="submit" value="Add Categories"/>
</form>
PHP:
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['category'])) {
if ($_GET['action'] == 'newCategory') {
$categories = $_POST['category'];
$query = "SELECT * FROM categories WHERE category ='$categories' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result)) {
echo '<script>alert("The Following Catergories Already Exist: ' . $categories . '")</script>';
} else {
// Simply cleans any spaces
$clean = str_replace(' ', '', $categories);
// Makes it possible to add multiple categories delimited by a comma
$array = explode(",", $clean);
foreach ($array as &$newCategory) {
mysql_query("INSERT INTO categories (category) VALUES ('$newCategory')");
}
echo "<script>alert('The following Categories have been added successfully: " . $categories . "')</script>";
}
}
} else {
echo "<script>alert('Please Enter at Least One Category.')</script>";
}
}
?>
1 个解决方案
#1
1
here's the proper syntax for making the call in the background and not submitting the form, but still sending/retrieving results.
这是在后台进行调用而不提交表单的正确语法,但仍然是发送/检索结果。
$(function(){
$('form').submit(function(e){
e.preventDefault(); // stop default form submission
$.ajax({
url: 'categories.php',
data: 'action=newCategory',
success: function(data){
//here we have the results returned from the PHP file as 'data'
//you can update your form, append the object, do whatever you want with it
//example:
alert(data);
}
});
});
});
Also:
I would not do this ->
我不会这样做 - >
echo "<script>alert('Please Enter at Least One Category.')</script>";
echo“
Just do echo 'Please Enter at Least One Category.';
只需回复'请输入至少一个类别。';
If you need to create an error system, you can do things like,
如果您需要创建错误系统,您可以执行以下操作:
echo "Error 1001: <!> Please enter at least One Category!';
echo“错误1001: 请输入至少一个类别!”;
Then in your Ajax callback for 'success', we can split the returned object in <!>
. Example to follow:
然后在Ajax回调中“成功”,我们可以在 中拆分返回的对象。示例如下:
success: function(data){
if($(data+':contains("<!>")'){
var errMsg = $(data).split('<!>');
alert(errMsg[0]+' : '+errMsg[1]);
//above would output - Error 1001 : Please enter at least One Category!;
}
}
#1
1
here's the proper syntax for making the call in the background and not submitting the form, but still sending/retrieving results.
这是在后台进行调用而不提交表单的正确语法,但仍然是发送/检索结果。
$(function(){
$('form').submit(function(e){
e.preventDefault(); // stop default form submission
$.ajax({
url: 'categories.php',
data: 'action=newCategory',
success: function(data){
//here we have the results returned from the PHP file as 'data'
//you can update your form, append the object, do whatever you want with it
//example:
alert(data);
}
});
});
});
Also:
I would not do this ->
我不会这样做 - >
echo "<script>alert('Please Enter at Least One Category.')</script>";
echo“
Just do echo 'Please Enter at Least One Category.';
只需回复'请输入至少一个类别。';
If you need to create an error system, you can do things like,
如果您需要创建错误系统,您可以执行以下操作:
echo "Error 1001: <!> Please enter at least One Category!';
echo“错误1001: 请输入至少一个类别!”;
Then in your Ajax callback for 'success', we can split the returned object in <!>
. Example to follow:
然后在Ajax回调中“成功”,我们可以在 中拆分返回的对象。示例如下:
success: function(data){
if($(data+':contains("<!>")'){
var errMsg = $(data).split('<!>');
alert(errMsg[0]+' : '+errMsg[1]);
//above would output - Error 1001 : Please enter at least One Category!;
}
}