Autocomplete, Convert Space(%) To + In Link


I have an autocomplete script which search for merchants in my system, and sends the user to a new page…


<?php $this->widget('ext.myAutoComplete', array(

    	'name' => 'merchantName',

    	'source' => $this->createUrl('review/autocomplete'),

    	'options' => array(



        	'focus'=> 'js:function( event, ui ) {

	            	$( "#autocomplete" ).val( );

    	        	return false;


		'select' => 'js:function( event, ui ){

     			top.location = "write/" +;



    	'htmlOptions'=>array('class'=>'input-1', 'autocomplete'=>'off', 'size'=>80,),

    	'methodChain'=>'.data( "autocomplete" )._renderItem = function( ul, item ) {

	        return $( "<li></li>" )

	        .data( "item.autocomplete", item )

	        .append( "<a>" + +  "</a>" )

	        .appendTo( ul );


)); ?>


public function actionAutocomplete(){

    	$res = array();

    	$term = Yii::app()->getRequest()->getParam('term', false);

    	if ($term)


     	 	$sql = 'SELECT id, name FROM merchants where LCASE(name) LIKE :name';

      		$cmd = Yii::app()->db->createCommand($sql);

      		$cmd->bindValue(":name","%".strtolower($term)."%", PDO::PARAM_STR);

      		$res = $cmd->queryAll();


    	echo CJSON::encode($res);



This works all fine… But what happens when i pick a merchant named, etc. "Merchant Casper"…

It sends me to: "Write/Merchant%Casper"

But I would like all the % to be switched out with + instead…

How can i do this??

// Casper

Parse string in js with :

replace(’%’, ‘+’)

Could you maybe give me an example on how to implement that in my script??

I’ve tried just to put it in the Select before top.location, with and without a ; at the end, and ’ and " around %+, and all different tries just makes my link stop working… :confused:

Isn’t there anyone else with some javascript skills who can help me with this one…? :)

You can change substring with this JavaScript piece: ='%','+');

As you see, replace function is called as a method of the String ( object.

Hmm, I got an error 500 (Division by zero) error when i implemented it in my code… :confused:

i put it in here:

'select' => 'js:function( event, ui ){ ='%','+');

  	top.location = "write/" +;



Can someone please help me with this problem…? :confused:

ahaha. Just be more carefully with quotation marks. Change « =’%’,’+’);» to « ="%","+");».

As a result your code will be:

'select' => 'js:function( event, ui ){ ="%","+");

  	top.location = "write/" +;


Always keep in mind, you can freely use «"» quotation mark inside «’» quote, and you can use «’» quotation mark inside «"» quote. But if you will use either «’» quotation mark inside «’» quote or «"» inside «"» (without escaping — \" or \’), you will catch errors.

okay guys be serious what would happen if he has a item with more than 2 words he is gonna have problem again here is one for all =\s/g, '+');

thanks a lot for the help guys…

I ended up using alirz23 solution due it sounded like it’s a bit more secure…!

But thanks to you too Sergey

casperskou don’t you think I deserve something for that how about a vote

I’m just humble syntax helper. But the global replacing, rather than replacing first occurrence, is better idea, yes. :)