0

I have this ajax form, which at the end I have the values in an array. the code is below..but i keep getting null in my php response for these items. I think I am doing this .ajax call wrong, any ideas would be greatly helpful. I also dont know what to write in the success section of the .ajax call. is that what I am missing? Please have a look below:

$('#submit_third').click(function(){
    //update progress bar
    $('#progress_text').html('100% Complete');
    $('#progress').css('width','339px');
    //prepare the fourth step

   var fields = new Array(
        $('#usernameReg').val(),
        $('#passwordReg').val(),
        $('#email').val(),
        $('#firstname').val(), 
        $('#lastname').val(),
        $('#city').val(),
        $('#phone').val(),
        categoryResult,
        $("#msg").val()                     
    );

    var tr = $('#fourth_step tr');
    tr.each(function(){
        //alert( fields[$(this).index()] )
        $(this).children('td:nth-child(2)').html(fields[$(this).index()]);
    });


    //slide steps
    $('#third_step').fadeOut(1000);
    $('#fourth_step').delay(1000).fadeIn(1000);          

    $('#submit_fourth').click(function(){
        //send information to server
        console.log(fields);
       $.ajax({
            type: "POST",
            data: 'fields',
            dataType: 'json',
            url: "php/postEngine.php",
            success: function(html){

            }
        });


    });     
});

php info (i am using flourish:

include_once($_SERVER['DOCUMENT_ROOT'] . '/../inc/init.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/dev/form_data.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/dev/db_handling.php');
ini_set('display_errors', 'On');
try {

    $tbl_user   =   'tbl_users';

    $statement = $db->prepare("INSERT INTO $tbl_user 
        (username,
        email,
        firstname,
        lastname,
        city,
        phone)
        VALUES 
        (%s,
        %s,
        %s,
        %s,
        %s,
        %s
        )"
    );

    $db->query($statement,
        $un,
        $email,
        $firstname,
        $lastname,
        $city,
        $phone
        );

    //  Update of Email_Replied to 1

    //$db->execute("UPDATE accommodation SET Email_Replied = '1' WHERE ID = %i", $user_id); 

} catch (Exception $e) {
    echo $error_msg;
    echo $un;
    echo $email;
    echo $firstname;
    echo $lastname;
    echo $city;
    echo $phone;
    echo json_encode($un);
    $error_msg = 'An error occured on postEngine.php';

}

?>

$un         =   fRequest::get('fields[0]');
$email      =   fRequest::get('fields[2]');
$firstname  =   fRequest::get('fields[3]');
$lastname   =   fRequest::get('fields[4]');
$city       =   fRequest::get('fields[5]');
$phone      =   fRequest::get('fields[6]');

?>

1
  • 3
    Can we see your php code too? Commented Nov 6, 2012 at 16:11

1 Answer 1

6

It should be :

data: fields,

not

data: 'fields',

right now all you're doing is sending the string 'fields' to the server, not the variable fields ?

Also make sure you're expecting an array on the server, and JSON back to the client etc. as you have specified JSON in the dataType, but in your success function you're calling it HTML, which it is not, not that it would matter as html is just a variable, but it does look like you're not expecting JSON, but still expecting JSON, and when the dataType is set to JSON, any response containing non valid JSON (like HTML) will fail ?

Sign up to request clarification or add additional context in comments.

2 Comments

ill mark it correct, because you are. Nice, but I am getting undefined for values, how do I tell flourish (php un-framework) what the data type is?
I made this all a bit easier by using jquery $('#formID').serialize() of my form id for my data, created new vars from the form, and found serialize did all the parsing for me without problems of encoding. anyways, this is useful too . thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.