Jump to content

Help Turning standard PHP Request to AJAX request - Banging my head on the wall.


Recommended Posts

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.

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?

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>";
  }
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.