0

I'd like run INSERT query by ajax. That query need value from php session but js request doesn't see that value. Whay it's happend ? What should i do ?

Some code below:

JS:

$('div#content').on( "submit" , "form" , function ( event ){
        event.preventDefault();
        var href        = "ajaxRequest.php" + $(this).attr("action");
        var method      = $(this).attr("method");
        var values      = $(this).serializeArray();

        $.ajax({
            url         : href,
            data        : values,
            type        : method,
            dataType    : "html",
            cache       : false,
            success     : function ( content ){
                alert( content );
            }
        });

    });

PHP:

public function addBoard( $params = null )
        {
            $ID_user            =   $_SESSION[ 'user_id' ];
            $board_model        =   $params[ 'board_model' ];

            $query = "INSERT INTO `".prefix."boards` (                      
                        `ID_user` ,
                        `board_model`
                        )
                    VALUES (
                        '$ID_user', '$board_model');";

            if( $this->SQL->dbquery( $query ) )
                echo 'Added';
            else
                echo 'Failed';
        }
1
  • add session_start() Commented Apr 8, 2013 at 11:15

3 Answers 3

3

write session_start(); at top of your PHP page.

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

1 Comment

You should have gotten the accepted answer... so I will give ya an upvote!!
0

you may need to start session on top of page. It seems here that you defined that function in ajax file but not called. So have you called that function in ajax file

Comments

0

For the server, an ajax request is not different from a regular page request. Session behaves in the same way as it behaves in page request.

All you need to do is start the session in your php file which responds to the ajax request.

Comments

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.