SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Location
    Limbo
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code JavaScript:
    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:
    Code JavaScript:
    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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Jaipai View Post
    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.
    A potentially more reliable way could be as:

    Code JavaScript:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Location
    Limbo
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much. I did not know about this method. Very helpful.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •