Refresh a div tag to reload a PHP session variable

hello everybody,

I’m trying to set a PHP session variable using AJAX script but its not working and a little help would be appreciated.

I have a drop down list of cars. I like to assign the user selection to a PHP session variable. My test script works half way meaning AJAX is picking up the variable but its not assigning the value to the session variable or im not doing something write.

the output is placed in a div tag that should reload witout refreshing the page to show the changes which you can see at the bottom.

  1. user makes a selection from the drop down
  2. on closeDIV button click AJAX is called and initiates a php file which assigns the variable to a session variable.
	
		

Successful
Open DIV





<?php echo '
'; print_r($_GET); echo $_SESSION['myCar']; ?>

javascript

// this controls the buttons
$(document).ready(function() {
	$('#dialog').hide();	
    $('#openDIV').click(function (e) {
        e.preventDefault();
        $('#dialog').show();
    }); 
});
// AJAX script	
$(document).ready(function (){
	$('#carForm').submit(function(e){		 
		e.preventDefault();
		$('#dialog').hide();
		alert("stage 1");
        var myCar = $("#sel_car").val();
		
        if (myCar == ''){
			$('.success').fadeOut(200);
            $('.error').fadeIn(200);
        }
        else{ 
		alert("stage 2");
			$.ajax({
				type: "POST",
                url: "carCall.php",
                data: $('#carForm').serialize(),
                success: function(msg){ 
					if(msg == 'success')
					{ 	alert("stage 3 "+ myCar);
						$('.success').fadeIn(200).show();
						$('.error').fadeOut(200).hide();
						$("#sel_car").val("");
					}
					else{
						alert("stage 4");
						$('.success').fadeOut(200);
						$('.error').fadeIn(200);
					}
				},
				error: function(){
					$('.success').fadeOut(200);
					$('.error').fadeIn(200);
				}
			});
			}
			return false;
    });
 });       	
  // refresh the div to show the changes
 $(document).ready(function() { 
	$('#closeDIV').on('click', function() { 
		var url = 'index.php';  
		$('#div1-wrapper').load(url + ' #div1'); 
	 }); 
});

php script that sets the session variable.

<?php 
	if (session_id() === "") {	session_start(); }	
    if(isset($_POST['sel_car']))
    {   $_SESSION['myCar'] =  $_POST['sel_car'];  		
		 echo 'success'; 
    } 
    else 
    { 	$_SESSION['myCar'] = "empty"; 
		echo 'failed';
	} 
?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.