Autocomplete, Convert Space(%) To + In Link

Hiii

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

View:


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

    	'name' => 'merchantName',

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

    	'options' => array(

        	'minLength'=>1,

        	'autoFill'=>false,

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

	            	$( "#autocomplete" ).val( ui.item.name );

    	        	return false;

        	}',

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

     			top.location = "write/" + ui.item.name;

		}'

     	),

    	'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>" + item.name +  "</a>" )

	        .appendTo( ul );

    	};'

)); ?>

Controller:


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);

    	Yii::app()->end();

	}

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:




ui.item.name = ui.item.name.replace('%','+');



As you see, replace function is called as a method of the String (ui.item.name) 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 ){

	ui.item.name = ui.item.name.replace('%','+');

  	top.location = "write/" + ui.item.name;

}'



Really…?

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

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

As a result your code will be:




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

	ui.item.name = ui.item.name.replace("%","+");

  	top.location = "write/" + ui.item.name;

}'



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


ui.item.name = ui.item.name.replace(/\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. :)