SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do i slove this problem

    i dont know much about js

    any way i am uisng ajax to dynamically generate second drop down based on value selected on first drop down
    ie
    form file
    <script src="getsome.js"></script>
    <select name="fieldname " onChange="showUser(this.value)">

    and in js file i have this

    f (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }
    var url="get.php"
    url=url+"?q="+str
    url=url+"&sid="+Math.random()


    so the problem with that is every file i use this ajax (with some modification) i have to 2 news files... .js and .php to get second drop down

    so there has been many files....
    so is there any way to solve this problem and make use of only two files in all cases...
    i am thinking of passing some varibale from form php to .js file and make some condition ..to chose the get.php file....
    ie
    if somevalue
    var url="get.php"
    else if somevalue
    var url="get1.php"
    and so on

    but even that way i need to have separate get page in all

    any way to eliminate need to two new file every thing i use this ajax in new file...
    thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ajax-reply.php?command=getTime&args=foo
    ajax-reply.php?command=makeBagels&args=bar
    etc...

  3. #3
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Code:
    ajax-reply.php?command=getTime&args=foo
    ajax-reply.php?command=makeBagels&args=bar
    etc...
    i think my question is not understood any way sorry for that
    here scenario is

    form.php
    which should send variable to js file

    ok may be we can do test.js?var1=value

    but i dont know how can js file handle variable sent from php file

    then in js file based on the variable taken from form.php (first php file)
    we should call another file(variable passing is easy here from js to php...url=url+"?q="+str)
    the problem here is passing variable value from php to .js file and make decision in js file based on that variable value (may be using if...)

    thanks

  4. #4
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok my my intermediate solution

    i have included js file script with in .php file now
    so when i have two files which should use ajax drop down...(ok for eg first one shows second drop down of subcategories based on value of first dropdown...)
    and second get username taking username start variable from one drop down

    if my old case i had
    for 1st case
    form1.php
    getcategories.js(called by form1.php)
    getcategories.php (called by js file)

    and again
    form2.php
    getusers.js(called by form1.php)
    getusers.php (called by js file)

    but now i have
    form1.php
    getajax.php (now it has both js and php)
    more over now
    in getajax.php i use variable name $block
    if ($block==1)
    {
    getcategories
    }
    else if ($block==2)
    {
    getusers;
    }

    and in form1 i include
    <?php
    $block=1;
    include('getajax.php');
    ....

    in form 2 i do
    $block=2;
    include('getajax.php');
    ....

    and i onclick of both form's select (first select which gives value to second drop down...)
    onChange="showvalues(this.value)"...
    which is defined in <script> part of getajax.php

    any way it has been working fine so far...
    if any suggestion or ...i may get problem in future...please suggest...

    any way as it has been complicated....if not understood sorry for that..

    any way thanks

    any improvements...?

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use crmalibu's suggestion:

    Code php:
    switch($_GET['command']) {
        case 'block1' :
            getcategories();
        break;
        case 'block2' :
            getusers();
        break;
        // etc...
    }

    I'd also use post variables instead of get. This way you can pass more data and handle it easier (just incase your passed variables contain something that can't be sent via GET)



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
  •