Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to store a value returned by Ajax into a session

Avatar of leppis75
leppis75 asked on
PHPAJAX
5 Comments1 Solution307 ViewsLast Modified:
I'm making an online booking form, where I use Ajax to retrieve the total price based on arrival date, departure date and number of guests (see image below). The form has 5 pages, and I would like to store the total price in a session, so that I could display it on all pages.

My Ajax call looks like this:
mygetrequest.open("GET", "get_price.php?arrival="+arrivalvalue+"&departure="+departurevalue+"&guests="+guestsvalue+"&apartment_id="+apartmentidvalue, true)
mygetrequest.send(null)

I tried to store the total price in a PHP session inside get_price.php in the end of the file before echoing the price for the booking form, but it didn't work. Should I be using Javascript instead, and how would that be done? I've pasted the full Ajax code below.
function showPrice() {
	
  if(document.getElementById("departure").value.length!=0) {
   	
	function ajaxRequest(){
		 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
		 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		  for (var i=0; i<activexmodes.length; i++){
		   try{
			return new ActiveXObject(activexmodes[i])
		   }
		   catch(e){
			//suppress error
		   }
		  }
		 }
		 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		  return new XMLHttpRequest()
		 else
		  return false
	}
	
	var mygetrequest=new ajaxRequest()
	mygetrequest.onreadystatechange=function(){
	 if (mygetrequest.readyState==4){
	  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
	   document.getElementById("priceTotal").innerHTML=mygetrequest.responseText
	  }
	  else{
	   alert("An error has occured making the request")
	  }
	 }
	}
	var apartmentidvalue=encodeURIComponent(document.getElementById("apartment_id").value)
	var arrivalvalue=encodeURIComponent(document.getElementById("arrival").value)
	var departurevalue=encodeURIComponent(document.getElementById("departure").value)
	var guests = document.getElementById("guests")
	var guestsvalue = guests.options[guests.selectedIndex].value
	mygetrequest.open("GET", "get_price.php?arrival="+arrivalvalue+"&departure="+departurevalue+"&guests="+guestsvalue+"&apartment_id="+apartmentidvalue, true)
	mygetrequest.send(null)
}

}
screenshot-booking-step1.gif
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971Flag of Guadeloupe imageMultitechnician
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers