Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

redirect the page from javascript Ajax

Hi,

In cakephp, I have need to redirect a page to itself when  changing the value of a drop down box.
The reason is the Url contains an ID. When changing a field the ID in the URL needs to change as well. You can only do this by redirecting to the same page with new ID.

The JS gets called  like this
           echo $this -> Form -> input('tutor_id', array('label' => false,'type' => 'select','options'=>$tutors,'selected' => $tutorId,'onchange'=>'selecttutoravalibality(this.value)'));
                        
once the tutor changes I need to redirect the page with new tutorID .This way I can send this webaoge onto another page with the correct  paramamters.


   function selecttutoravalibality(tutorId){
	    
		 $("#ajaxformatediv").fadeIn(1000);
		 var studentid= document.getElementById('LessonStudentId').value;
			//alert(studentid);
			$.ajax({
				type: 'POST',
				url: siteurl+'lessons/select_tutor_avalability',
				//dataType: 'json',
				data: {'tutorId': tutorId},
				success: function(msg) {
				 $('#tutoravaliablityresposnse').html(msg);
				}
			  });  
	  
		  
		   $.ajax({
			type: 'POST',
			url: siteurl+'lessons/select_tutor_book_schedule',
			//dataType: 'json',
			data: {'tutorId': tutorId,'studentid': studentid},
			success: function(bookschedulresposne) {
			 //alert(bookschedulresposne);
			 $('#ajaxtutorbookschedule').html(bookschedulresposne);
			}
		  });
			  
		  
		  $.ajax({
			type: 'POST',
			url: siteurl+'lessons/select_tutor_oneoff_classes',
			//dataType: 'json',
			data: {'tutorId': tutorId,'studentid': studentid},
			success: function(oneoffclassresponse) {
			 //alert(bookschedulresposne);
			 $('#ajaxtutoroneoffclasses').html(oneoffclassresponse);
			}
		  });
		  
		   $("#ajaxformatediv").fadeOut(1000);
		



		  
	}

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

To redirect a page in Javascript, you'd use windows.location():

window.location = "somepage.php?id=xxx";

Open in new window


Your design pattern does seem a little strange though. You're making an AJAX call to update the DOM, and then immediately refreshing the page, which would undo all the DOM changes that the AJAX call made. Seems kind of pointless.
Avatar of jagguy

ASKER

no refreshing the page with the new change is what is needed. This gets URL with new ID fields and I can pass the query string on to another page with correct details.

I dont need the DOM changed, the redirect will do this for me
Avatar of jagguy

ASKER

Where do I place redirect  window.location = "somepage.php?id=xxx"; with student and new tutorID?
Sorry jagguy. I'm a little confused. You say you don't need the DOM changed, but that's exactly what your AJAX requests do. All those html() calls in the success of your AJAX are changing the DOM.

As for where to place the window.location line - that depends on when you want to call it. As I said before, currently it doesn't make a lot of sense.

Your page seems to be set up so that when you change the dropdown, you make 3 AJAX requests back to your server and update the DOM with the results. If you then immediately reload the page with a different querystring in the URL, you'll lose all those changes.

Probably easier if you can explain what you're trying to achieve in simpler terms.
Avatar of jagguy

ASKER

I think your thinking too hard about it.

I want a change a value in a drop down box  (list of names ) and then just reload the page with the new id from the drop down box.

That will solve the query string problems I have
OK. Here you go:

function selecttutoravalibality(tutorId) {
   window.location = window.location.pathname + "?tutorId=" + tutorId;
}

Open in new window


If that's all you need, I can't see the relevance of ANY of your AJAX code, or the reference to AJAX in the question. If you're just redirecting the page, your AJAX is completely meaningless and ineffective.
greetings jagguy, , I can NOT understand what you need to do in your AJAX code work? ? ? You change three HTML page sections with code like -
        $('#tutoravaliablityresposnse').html(msg);

and you are are asking about - "once the tutor changes I need to redirect the page with new tutorID", , and later you say -
     "I want a change a value in a drop down box  (list of names ) and then just reload the page with the new id from the drop down box."

I imagine that with your code like -
     $('#tutoravaliablityresposnse').html(msg);

you are filling a select box options, and you now need the onchange of this select to change the page that the user is viewing to a page that has the tutorID in the address? ?

Am I any where close to what you need to do, if NOT, then can you tell us WHAT event (select change, button click, icon touch) that will change the page to a page that has the tutorID in the address?
Avatar of jagguy

ASKER

i dont want the ajax code at all
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial