我在这里踢自己的屁股,因为我一生都无法弄清楚......这应该是一个快速而肮脏的项目,但是,我决定我想尝试一些新的东西,而且我几乎没有没有使用 jQuery 中的 AJAX 方法的经验...我花了 5 天时间尝试学习和理解如何适当地实现 AJAX 调用,但要知道有用...我学到了一些基本的东西,但不是我需要的执行下面的代码。
同样,我想知道如何使用 jQuery 将这个标准请求转换为 AJAX ......
这是我的表格和 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>";
}
}
?>