Redirect To Another View With Jquery $.get

Hi all,

I am trying to redirect to another view following a test on a value in a form.

I am using jquery to test the value in the form. The URL is being called and the action run in the controller but the current view is not redirected to the other view.

Can somebody please help?

Here is my jquery code in the first view:




<?php Yii::app()->getClientScript()->registerScript('tabl','$("#tabl").change(function()

	{	$.get("index.php?r=order1/Tabl", {tabl:$("#tabl").val()}); 

	}

	);');

?>



The value to be tested is $("#tabl")

Here is the code for the action Tabl of the controller order1:




	public function actionTabl()

	{

		$tabl = $_GET['tabl'];

		$order= Order1::model()->find('tabl=:id && !closed', array(':id'=>$tabl));

		if(!empty($order))

			$this->redirect(array('update','id'=>$order->id));	

	}	



The code is being run successfully, the only thing is that the view is not redirected.

Thanks beforehand

$.get is a shorthand Ajax function.

You need to pass the value ($order->id) to this function and then redirect using javascript in your view.


<?php Yii::app()->getClientScript()->registerScript('tabl','$("#tabl").change(function()

        {

            var myOrderId = false;

            myOrderId = $.get("index.php?r=order1/Tabl", {tabl:$("#tabl").val()});

            if (myOrderId) {

                window.location.replace("YOUR_URL" + myOrderId);

            } 

        }

        );');

?>

In your controller, just echo the order id.

Thank you very much Outrage. You set me on the right track, wondering why I did not think about that :)

Here is the amended javascript code:





<?php Yii::app()->getClientScript()->registerScript('tabl','$("#tabl").change(function()

	{	$.get("index.php?r=order1/Tabl", {tabl:$("#tabl").val()}, function(data) 

			{	

				var orderid = data;

				if (orderid!=0)

					{ alert("An open order exists for this table. You will be redirected to this existing order");

					window.location.replace("index.php?r=order1/update&id=" + orderid);

					}

			}); 

	}

	);');

?>



and in the controller:




		$tabl = $_GET['tabl'];

		$order= Order1::model()->find('tabl=:id && !closed', array(':id'=>$tabl));

		if(!empty($order))

			echo $order->id;

		else

			echo 0;



Excellent, well done :)