Ajax Problem

Discussion in 'jQuery' started by Jeremy Benson, May 22, 2014.

  1. #1
    Hey,
    I've got a bunch of form data that goes into a JSON object that I throw to a PHP script with a post call, but all the post variables throw undefined errors in the PHP script. Not sure why..

    
    
    if(!firstNameError && !lastNameError && !handleError && !passwordError && !acceptError){
       
      //there are no errors so toss the data to serverside script then database.
      var regJSON = {
       
      "firstName":"BOB",
      "lastName":"DYLAN",
      "handle":"DILLY",
      "email":"DILLYSON",
      "password":"DILPASS",
      "accept":"YUP"};
       
       
      $.ajax({
      tpye: "POST",
      dataType: "html",
      data: regJSON,
      url:"../admin/scripts/php/registration.php",
      success: function(data){
       
      alert(data);   
      }
      });
       
      }
    
    Code (markup):

     
    Jeremy Benson, May 22, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well... Without the processing php file, and the errors it's throwing, this one's gonna be hard to solve...
     
    PoPSiCLe, May 22, 2014 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Sorry about that, lol. I thought there was no typos in that script, so assumed it was an Ajax code problem..

    
    $firstName = $_POST['firstName'];
         $lastName = $_POST['lastName'];
         $handle = $_POST['handle'];
         $email = $_POST['email'];
         $password = $_POST['password'];
         $accept = $_POST['accept'];
         
         echo "<p>The Data!</p>";
    
    
    PHP:
     
    Jeremy Benson, May 22, 2014 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    I just installed Firebug. Not sure hot to get the full potential out of it yet, but did get some error...

    SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data - jquery.min.js(line 4).

    As a secondary question. I'm not sure why it's involving a file called jquery.min.js. I installed jquery myself into a file called jquer.js in my dir tree. I sourced that file in the top of my index page, above my other script using jquery, on the same page my forms on.... I think even if I don't source my jquery file I still have jquery functionality throughout my scripts...seems to be global, but have no clue why, lol.
     
    Jeremy Benson, May 22, 2014 IP
  5. somasounds

    somasounds Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #5
    Is that really all you have for the php file? Also, where are the errors showing up, in your alert? Or are you getting php error messages in your error_log? More info please!
     
    somasounds, May 22, 2014 IP
  6. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #6
    Okay, I solved this using Firebug. I saw there was a query sting, so I realized I may need to be using $GET variables...works like a charm now.
     
    Jeremy Benson, May 22, 2014 IP
  7. somasounds

    somasounds Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #7
    You also had a typo in your original ajax call, see your first post.... it says:

    tpye: "POST"
    Code (markup):
    Could be why it wasn't workin ;)
     
    somasounds, May 22, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Since you're using jQuery, I'm also a little confused as to why you don't just do:

    $.post('../admin/scripts/php/registration.php',$('#id_of_form').serialize(),function(data) { // do stuff here })
     
    PoPSiCLe, May 22, 2014 IP
  9. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #9
    Didn't know the function existed...

    That typo is why. I thought I was being smart, turns out I had a typo. Found it at home. The default type is $_GET. I understand Jquery wanting to make their function work no matter what, but extracting my misspelled parameter and sticking in their default is a sloppy choice.. It should have thrown an error....If I hadn't of stumbled across the typo at home I would have never known it was there... Except for of course you guys.
     
    Jeremy Benson, May 23, 2014 IP
  10. somasounds

    somasounds Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #10
    That's why we're here!

    I agree though, should have thrown an error. What browser are you using?
     
    somasounds, May 23, 2014 IP
  11. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #11
    Right now I'm working through firefox.
     
    Jeremy Benson, May 24, 2014 IP