beerLantern
beerLantern

Reputation: 492

Codeigniter routing. AJAX get with parameters using javascript

I want this working with Codeigniter:

name = encodeURIComponent( document.getElementById("myName").value);

xmlHttp.open("GET", "quickstart.php?name=" + name, true);

xmlHttp.onreadystatechange = handleServerResponse; //not relevant for question

xmlHttp.send(null);

I have create a controller with a function with a parameter and changed the previous code:

xmlHttp.open("GET", "quickstart.php?name=" + name, true);

to

xmlHttp.open("GET", "ajax/quickstart/"+name, true);

I use this routes(but doesn't work):

$route['ajax'] = 'ajax';
$route['ajax/quickstart'] = 'ajax/quickstart';
$route['ajax/quickstart/([A-Za-z0-9])+'] = 'ajax/quickstart/$1';

The problem I'm having it's I only get the last letter written. For example if I write "name", only "e" it's passed as argument. But all the word was sent. My controller function looks like:

public function quickstart($name='')
{
// we'll generate XML output
header('Content-Type: text/xml');
// generate XML header
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
// create the <response> element
echo '<response>';
// retrieve the user name
//$name = $this->input->get('name');

// generate output depending on the user name received from client
$userNames = array('YODA', 'AUDRA', 'BOGDAN');
if (in_array(strtoupper($name), $userNames))
    echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
    echo 'Stranger, please tell me your name!';
else

echo htmlentities($name) . ', I don\'t know you!';
// close the <response> element
echo '</response>';
}

Upvotes: 1

Views: 1596

Answers (1)

The Alpha
The Alpha

Reputation: 146191

Use only

$route['ajax/quickstart/(:any)'] = "ajax/quickstart/$1";

Documentation.

Upvotes: 1

Related Questions