0

I have a piece of code which is giving me trouble.
I am trying to get order values from a php class function :

public function generalSettings(){


    $totalprijs = 0;
    foreach($_SESSION['items'] as $key => $value){
        $products->setProduct($key);
        $totalprijs = $totalprijs + ($products->prijs_exBTW * $value);
    }


    $inclbtw = ($totalprijs * ('1.'.$this->BTWPercnt));
    if($totalprijs > $this->franco_vanaf){
        $verzendkosten = 0;
    }else{
        $verzendkosten = $this->verzendkosten;
    }

    $btw = ($totalprijs + $verzendkosten) * ('0.'.$this->BTWPercnt);

    if($totalprijs > $this->franco_vanaf){
        $totaalInc = ($totalprijs + $btw);
    }else{
        $totaalInc = ($totalprijs + $btw + $this->verzendkosten);
    }

    $return = array(
        "subtotaal" => $totalprijs,
        "btw" => $btw, 
        "inclbtw" => $inclbtw,
        "verzendkosten" => $verzendkosten,
        "totaalInc" => $totaalInc
    );
    return($return);    
}

When I access this function from within the class it works.
And when I call other function that uses this function in my checkout it works.

But when I try to access it in my AJAX-handling file it says:

Warning: Invalid argument supplied for foreach()

The code,when I call the function in the ajax file is as below:

if($isValid == true){
    unset($notneeded);
    $notneeded = array("ww1","ww2","huisnr","vhuis","companyvat","companyname","tel","firstname","lastname");
    foreach($_POST['gegevens'] as $key => $value){
        if(in_array($key,$verplichtArray) && (!in_array($key,$notneeded))){ 
            $fields .= "`".$key."`,";
            $values .= "'".$value."',";
        }
    }
    $shoppingcar = new Winkelwagen;
    $order = $shoppingcar->generalSettings();

    $fields .= '`timestamp`,`klant`,`totaal`,`totaalInc`,`verzendkosten`,`status`,`betaalmethode`';
    $values .= "now(),'".$acc->id."','".$order['subtotaal']."','".$order['totaalInc']."','".$order['verzendkosten']."','3','".mysql_real_escape_string($_POST['betaalwijze'])."'";

    if(isset($_POST['gegevens']['V'])){
        $fields .= ',`V`';
        $values .= ",'X'";
    }

    $message = "INSERT INTO order (".$fields.") VALUES (".$values.")";
}

It seems like when I call the function from the ajax file the session's empty but when I call the function from the file where I call to the ajax file it works just fine.

Could anyone explain what I'm doing wrong???

EDIT

The piece of jquery i use to call the ajax file as requested:

$('#afrekenen').click(function(){
            clearInterval(myInterval);
            var fields = $('.addressform :input');
            $.each(fields, function(field,val){
                $(val).removeClass('errorInput');
            })
            var gegevens = {};
            var adresform = $('.addressform').serializeArray();
            $.each(adresform, function(index, val){
                gegevens[this.name] = this.value;
            });
            if(!$('input[name=payment]:checked').val()){
                var betaalwijze = 0;
            }else{
                var betaalwijze = $('.betaalwijze').val();
            }

            var voorwaarden = $('input[name=voorwaarden]:checked').val();

            $.ajax({
                type: 'post',
                url: '/inc/afrekenen.php',
                data: {"gegevens":gegevens ,"betaalwijze":betaalwijze,"voorwaarden":voorwaarden},
                success: function(data) {
                    response = jQuery.parseJSON(data)
                    if(response.isValid == false){

                        $('#errormsg').html('<div class="alert alert-danger">'+
                        '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
                        +response.message+'</div>');

                        $.each(response.fouteVelden, function(index, object){
                            $('#'+object+'').addClass('errorInput');
                        });
                    }else{
                        $('#errormsg').html('<div class="alert alert-success">'+
                        '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
                        +response.message+'</div>');
                    }
                }
            });
    }); 

if have the ob_start(); tag and the session_start(); tag

also only the sessions that are somehow linked to my class are returning 1 when i try to print_r them the rest of my sessions still remain

the foreach through the $_SESSION['item'] in the first part of my code isn't working all the other parts of code work

EDIT

A good nights sleep seems to have solved the conflict... don't know what the error was and don't know how i fixed it but it works! Thank you for the suggestions :)

2
  • print_r($_POST['gegevens']); seems to be null Commented May 13, 2014 at 9:07
  • How do you call the ajax? Any difference in domain.com and www.domain.com? Commented May 13, 2014 at 9:11

3 Answers 3

2

if /inc/afrekenen.php is in the same domain as your class then session is shared and phpsid cookie is passed along your ajax request. In this case, the only problem would your session not being started in /inc/afrekenen.php.

Verify that session is started in /inc/afrekenen.php.

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

1 Comment

The Session is started
0
// print $_POST['gegevens'] if it is returning you a json string so
// do this

$postDataGegevens = json_decode($_POST['gegevens']);

// if $postDataGegevens in current format 
// then pass in foreach

foreach($postDataGegevens as $key => $value){ 
    // your code here ...
}

3 Comments

I'm sorry that part of the code is working.. the foreach through the $_SESSION['item'] in the first part of my code isn't working
Did you initialize yoour session in your class file. "session_start()"
i didn't in my classfile is this neccesary?
0

I noticed that my sessions where saved in

session_save_path('../tmp');

added this to the top of my ajax file and it worked it's magic

Thank you for the suggestions

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.