tags:

views:

67

answers:

1

Hello, My problem is the folowing: I am trying to retrieve the postvalue, but whenever I put that statement in the php code the ajaxcall will fail.

I cannot see what I am missing here??

$.ajax({
        url: "includes/livetabs.php?actie=sessiegebruikersnaam", 
      data: {gebruikersnaam: tbgebruikersnaam},
        cache: false,
        dataType: "json",
        success: function(data) {
      //opslaan van gebruikersnaam in php sessie
     /*$.post("includes/livetabs.php", {"sessiegebruikersnaam": chatnaam},
        function(data){*/
        //doe nog iets
       aa= data.status;
       bb=data.naam;
      //krijg de instellingen terug
      alert(aa);
      alert(bb); 
     }});

//php section
if(isset($_GET['actie'])){
**$n=$_POST['gebruikersnaam'];**
    if ($_GET['actie']=="sessiegebruikersnaam"){
    if (!isset($_SESSION['username'])){
    $_SESSION['username'] = $n ;}
    header('Content-type: application/json');
    //geef ook meteen de secondary instellingen terug

    ?>

{
        "status": "somevalue",
        "naam": "anothervalue"
}
    <?php
    exit(0); // Stop het script.
    }
}

thanks, Richard

A: 

EDIT: The stuff you are passing to the server should be wrapped in parenthesis, also, I would specify the type of request for clarity:

data: ({gebruikersnaam : tbgebruikersnaam}),
type: "POST", //just in case

Badly formed json will not work with $.ajax. Try using json_encode on the server to generate the json:

//php section
if(isset($_GET['actie'])){
**$n=$_POST['gebruikersnaam'];**
    if ($_GET['actie']=="sessiegebruikersnaam"){
    if (!isset($_SESSION['username'])){
    $_SESSION['username'] = $n ;}
    header('Content-type: application/json');
    //geef ook meteen de secondary instellingen terug
    echo json_encode(array('status' => 'someValue', 'naam' => 'anotherValue'));
    exit();
    ?>

Also, it might be clearer to use $.getJSON instead of $.ajax.

karim79
You would also want to make sure nothing else is being output to the client before that final echo.
karim79
but I don't output anything other then those two testvalues and they work. It's just when I ask for the postvalue it is not working.
Richard
great, thank you. Your last edit did the trick. I was either one or both of those that made it work.
Richard
@Richard If this answer solved your problem you might consider accepting it.
ctford