idontknowphp Posted April 14, 2012 Share Posted April 14, 2012 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. Again, I am wondering how to go about converting this standard request to AJAX using jQuery... here is my form & 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 { $clean = str_replace(' ', '', $categories); $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>"; } } ?> Thanks a lot guys, I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/260907-help-turning-standard-php-request-to-ajax-request-banging-my-head-on-the-wall/ Share on other sites More sharing options...
trq Posted April 14, 2012 Share Posted April 14, 2012 If you have been unable to follow the documentation or tutorials you may have already read what makes you believe we are going to be better at explaining things? Do you really want us to write a tutorial here or what exactly are you asking? Quote Link to comment https://forums.phpfreaks.com/topic/260907-help-turning-standard-php-request-to-ajax-request-banging-my-head-on-the-wall/#findComment-1337246 Share on other sites More sharing options...
idontknowphp Posted April 14, 2012 Author Share Posted April 14, 2012 What happened was I tried to do it myself, but i don't think i grasped the concept of how to process the post/get properly...Instead of properly porcessing "manage-products.php?action=newProduct" it would instead not process it at all and would tack on "manage-categories.php?category=Test&submit=Add+Categories"...I just was hoping someone with more background could look at it and say "duh retard, that's not how you do it..." But i realize i forgot to post what i tried to use as far as the jQuery...here is the code again with jQuery $("#theform").submit(function(event) { var $form = $(this), $inputs = $form.find("input, select, button, textarea"), serializedData = $form.serialize(); $inputs.attr("disabled", "disabled"); $.ajax({ url: "manage-products.php?action=newProduct", type: "post", data: serializedData, success: function(response, textStatus, jqXHR) { console.log("Win."); }, error: function(jqXHR, textStatus, errorThrown) { console.log("Fail: " + textStatus, errorThrown); }, complete: function() { $inputs.removeAttr("disabled"); } }); event.preventDefault(); }); <form id="theform"> <input name="category" type="text" /> <input class="btn_add" name="submit" type="submit" value="Add Categories"/> </form> <?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 { $clean = str_replace(' ', '', $categories); $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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260907-help-turning-standard-php-request-to-ajax-request-banging-my-head-on-the-wall/#findComment-1337247 Share on other sites More sharing options...
idontknowphp Posted April 14, 2012 Author Share Posted April 14, 2012 Thanks any how, heh, figured it out... Quote Link to comment https://forums.phpfreaks.com/topic/260907-help-turning-standard-php-request-to-ajax-request-banging-my-head-on-the-wall/#findComment-1337254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.