Passing additional variables in options = { script: } AJAX

Hi there.

I’m using a javascript application that works like a charm, it works up until I touch it to make any mods to it. Here is the declaration of the script:

var options = {
		script: "test.php?json=true&limit=6&",
		varname:"input",
		json:true,
		shownoresults:false,
		maxresults:6,
		callback: function (obj) { document.getElementById('testid').value = obj.id; }
	};
	var as_json = new bsn.AutoSuggest('testinput', options);

I’m trying to pass an additional value to the test.php file though I’m uncertain about how to do this. First and foremost the variable is dynamic so adding in a get parameter like the others won’t do.

I have tried on the other hand to add this variable as I have read I can integrate PHP in javascript

Tried doing:

script: <?php echo "test.php?usr={$id}&json=true&limit=6&"; ?>,

with no luck. Now my question is how could I pass this $id variable through to my test.php? I’m out of ideas with my minimal knowledge of javascript.

Thanks

A potentially more reliable way could be as:


var id = <?php echo intval($id); ?>;
var options = {
    script: "test.php?usr=" + id + "&json=true&limit=6&",
    ...

If you continue to have issues, you can investigate the source code of the web page to find out what PHP is echoing out for the id.

Thanks very much. I did not know about this method. Very helpful.