Question with AJAX

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sheldonlg

    Question with AJAX

    I am still improving my AJAX and I ran into something that I'm not sure
    how to do.

    I will have a page called where I will have a dropdown list at the top
    and a large display at the bottom. The display depends upon what is
    currently selected in the dropdown. This is readily done via AJAX which
    is activated with an onchange in the dropdown list. The contents of the
    bottom half of the page are created in the php script which is sent back
    to the response handler which changed the bottom part via an innerHTML.

    So far OK. What I would also like to happen is that when the page is
    called initially, that the bottom half shows for the first entry of the
    dropdown list. I don't want to repeat code in two places.

    One solution I have is to have the entire selection and bottom building
    part be an include file that I put into both my AJAX server php script
    and into this display page. I would just use different values for the
    selected item. It would be on the display page as a require_once before
    I do any html display.

    My question is whether there is some better way.
  • after9

    #2
    Re: Question with AJAX

    On Jun 3, 11:45 am, sheldonlg <sheldonlgwrote :
    I am still improving my AJAX and I ran into something that I'm not sure
    how to do.
    >
    I will have a page called where I will have a dropdown list at the top
    and a large display at the bottom.  The display depends upon what is
    currently selected in the dropdown.  This is readily done via AJAX which
    is activated with an onchange in the dropdown list.  The contents of the
    bottom half of the page are created in the php script which is sent back
    to the response handler which changed the bottom part via an innerHTML.
    >
    So far OK.  What I would also like to happen is that when the page is
    called initially, that the bottom half shows for the first entry of the
    dropdown list.  I don't want to repeat code in two places.
    >
    One solution I have is to have the entire selection and bottom building
    part be an include file that I put into both my AJAX server php script
    and into this display page.  I would just use different values for the
    selected item.  It would be on the display page as a require_once before
    I do any html display.
    >
    My question is whether there is some better way.
    Is the dropdown list dynamic or static?

    If it is static and you are using a GET method (which seems logical, I
    don't believe you are manipulating any data, merely retrieving it)
    then why not just initiate the page using your script passing it the
    desired value?

    Comment

    • sheldonlg

      #3
      Re: Question with AJAX

      after9 wrote:
      On Jun 3, 11:45 am, sheldonlg <sheldonlgwrote :
      >I am still improving my AJAX and I ran into something that I'm not sure
      >how to do.
      >>
      >I will have a page called where I will have a dropdown list at the top
      >and a large display at the bottom. The display depends upon what is
      >currently selected in the dropdown. This is readily done via AJAX which
      >is activated with an onchange in the dropdown list. The contents of the
      >bottom half of the page are created in the php script which is sent back
      >to the response handler which changed the bottom part via an innerHTML.
      >>
      >So far OK. What I would also like to happen is that when the page is
      >called initially, that the bottom half shows for the first entry of the
      >dropdown list. I don't want to repeat code in two places.
      >>
      >One solution I have is to have the entire selection and bottom building
      >part be an include file that I put into both my AJAX server php script
      >and into this display page. I would just use different values for the
      >selected item. It would be on the display page as a require_once before
      >I do any html display.
      >>
      >My question is whether there is some better way.
      >
      Is the dropdown list dynamic or static?
      >
      If it is static and you are using a GET method (which seems logical, I
      don't believe you are manipulating any data, merely retrieving it)
      then why not just initiate the page using your script passing it the
      desired value?
      >
      It is dynamic. Once in the page, a list is build from the category
      passed in. It is that list that is then used to display what is in the
      bottom of the page. The first llist is a db call. The second list is
      also a db call using the value from the first list. Coming into the
      page, we have no idea as to what the id will be for the first member on
      the first list.

      The more I think of it, the more the require_once of the building code
      looks like the way to go. There is no duplication of code (one include
      file) and it is just exercised in two different places.

      Comment

      • after9

        #4
        Re: Question with AJAX

        On Jun 3, 12:31 pm, sheldonlg <sheldonlgwrote :
        after9 wrote:
        On Jun 3, 11:45 am, sheldonlg <sheldonlgwrote :
        I am still improving my AJAX and I ran into something that I'm not sure
        how to do.
        >
        I will have a page called where I will have a dropdown list at the top
        and a large display at the bottom.  The display depends upon what is
        currently selected in the dropdown.  This is readily done via AJAX which
        is activated with an onchange in the dropdown list.  The contents of the
        bottom half of the page are created in the php script which is sent back
        to the response handler which changed the bottom part via an innerHTML.
        >
        So far OK.  What I would also like to happen is that when the page is
        called initially, that the bottom half shows for the first entry of the
        dropdown list.  I don't want to repeat code in two places.
        >
        One solution I have is to have the entire selection and bottom building
        part be an include file that I put into both my AJAX server php script
        and into this display page.  I would just use different values for the
        selected item.  It would be on the display page as a require_once before
        I do any html display.
        >
        My question is whether there is some better way.
        >
        Is the dropdown list dynamic or static?
        >
        If it is static and you are using a GET method (which seems logical, I
        don't  believe you are manipulating any data, merely retrieving it)
        then why not just initiate the page using your script passing it the
        desired value?
        >
        It is dynamic.  Once in the page, a list is build from the category
        passed in.  It is that list that is then used to display what is in the
        bottom of the page.  The first llist is a db call.  The second list is
        also a db call using the value from the first list.  Coming into the
        page, we have no idea as to what the id will be for the first member on
        the first list.
        >
        The more I think of it, the more the require_once of the building code
        looks like the way to go.  There is no duplication of code (one include
        file) and it is just exercised in two different places.
        So let me get the workflow straight.

        1. A user selects a category.
        2. This category is passed to the display page.
        3. On display page, a dropdown list is populated via a database query
        of category that was passed.
        4. On a dropdown change event a request is passed to a php script.
        5. The php script returns database information to display page.

        When #3 happens you want the display section to automatically load the
        information of the first dropdown option of the category.

        One solution involves the steps prior to #3. Along with passing the
        category, why not pass along the first item in category's database?
        Then you could have a GET request of something like display.php?
        category=foo&fi rst=bar. In the display page you could check if
        $_GET["first"] is set and then handle it appropriately.

        Comment

        • Tom Cole

          #5
          Re: Question with AJAX

          On Jun 3, 12:31 pm, sheldonlg <sheldonlgwrote :
          after9 wrote:
          On Jun 3, 11:45 am, sheldonlg <sheldonlgwrote :
          I am still improving my AJAX and I ran into something that I'm not sure
          how to do.
          >
          I will have a page called where I will have a dropdown list at the top
          and a large display at the bottom.  The display depends upon what is
          currently selected in the dropdown.  This is readily done via AJAX which
          is activated with an onchange in the dropdown list.  The contents of the
          bottom half of the page are created in the php script which is sent back
          to the response handler which changed the bottom part via an innerHTML.
          >
          So far OK.  What I would also like to happen is that when the page is
          called initially, that the bottom half shows for the first entry of the
          dropdown list.  I don't want to repeat code in two places.
          >
          One solution I have is to have the entire selection and bottom building
          part be an include file that I put into both my AJAX server php script
          and into this display page.  I would just use different values for the
          selected item.  It would be on the display page as a require_once before
          I do any html display.
          >
          My question is whether there is some better way.
          >
          Is the dropdown list dynamic or static?
          >
          If it is static and you are using a GET method (which seems logical, I
          don't  believe you are manipulating any data, merely retrieving it)
          then why not just initiate the page using your script passing it the
          desired value?
          >
          It is dynamic.  Once in the page, a list is build from the category
          passed in.  It is that list that is then used to display what is in the
          bottom of the page.  The first llist is a db call.  The second list is
          also a db call using the value from the first list.  Coming into the
          page, we have no idea as to what the id will be for the first member on
          the first list.
          >
          The more I think of it, the more the require_once of the building code
          looks like the way to go.  There is no duplication of code (one include
          file) and it is just exercised in two different places.- Hide quoted text -
          >
          - Show quoted text -

          In the code that populates your dropdown, after populating the
          dropdown simply set the selectedIndex of it to 0 and then call the
          function that is called by the onchange handler, or you can even call
          onchange() of the element.

          function loadMyDropDown( ) {
          var select = document.getEle mentById("categ ories");
          //...populate the dropdown...
          if (select.options .length 0) {
          select.selected Index = 0;
          select.onchange ();
          }
          }

          HTH.

          Comment

          • martinpauly@web.de

            #6
            Re: Question with AJAX

            the bad thing about toms method is, if javascript is deactivated the
            user will see nothing. I favoured your solution when I had the problem
            first, for me it worked well. One tip about it: i would ad an ?ajax to
            all ajax calls, e.g. includes/ajax3.php?ajax
            by using if(isset($_GET['ajax'])) you can check if your file is called
            by ajax or if it is included. So if you have differences in some
            pathes or anything else you can avoid problems with this method

            Comment

            • sheldonlg

              #7
              Re: Question with AJAX

              after9 wrote:
              On Jun 3, 12:31 pm, sheldonlg <sheldonlgwrote :
              >after9 wrote:
              >>On Jun 3, 11:45 am, sheldonlg <sheldonlgwrote :
              >>>I am still improving my AJAX and I ran into something that I'm not sure
              >>>how to do.
              >>>I will have a page called where I will have a dropdown list at the top
              >>>and a large display at the bottom. The display depends upon what is
              >>>currently selected in the dropdown. This is readily done via AJAX which
              >>>is activated with an onchange in the dropdown list. The contents of the
              >>>bottom half of the page are created in the php script which is sent back
              >>>to the response handler which changed the bottom part via an innerHTML.
              >>>So far OK. What I would also like to happen is that when the page is
              >>>called initially, that the bottom half shows for the first entry of the
              >>>dropdown list. I don't want to repeat code in two places.
              >>>One solution I have is to have the entire selection and bottom building
              >>>part be an include file that I put into both my AJAX server php script
              >>>and into this display page. I would just use different values for the
              >>>selected item. It would be on the display page as a require_once before
              >>>I do any html display.
              >>>My question is whether there is some better way.
              >>Is the dropdown list dynamic or static?
              >>If it is static and you are using a GET method (which seems logical, I
              >>don't believe you are manipulating any data, merely retrieving it)
              >>then why not just initiate the page using your script passing it the
              >>desired value?
              >It is dynamic. Once in the page, a list is build from the category
              >passed in. It is that list that is then used to display what is in the
              >bottom of the page. The first llist is a db call. The second list is
              >also a db call using the value from the first list. Coming into the
              >page, we have no idea as to what the id will be for the first member on
              >the first list.
              >>
              >The more I think of it, the more the require_once of the building code
              >looks like the way to go. There is no duplication of code (one include
              >file) and it is just exercised in two different places.
              >
              So let me get the workflow straight.
              >
              1. A user selects a category.
              2. This category is passed to the display page.
              3. On display page, a dropdown list is populated via a database query
              of category that was passed.
              4. On a dropdown change event a request is passed to a php script.
              5. The php script returns database information to display page.
              >
              When #3 happens you want the display section to automatically load the
              information of the first dropdown option of the category.
              >
              One solution involves the steps prior to #3. Along with passing the
              category, why not pass along the first item in category's database?
              Then you could have a GET request of something like display.php?
              category=foo&fi rst=bar. In the display page you could check if
              $_GET["first"] is set and then handle it appropriately.
              ....because I don't KNOW the first item before getting to the page and
              running the first db query to populate the list of items. The items are
              identified by an ID which is obtained from the db based up the category ID.

              Comment

              • sheldonlg

                #8
                Re: Question with AJAX

                Tom Cole wrote:
                On Jun 3, 12:31 pm, sheldonlg <sheldonlgwrote :
                >after9 wrote:
                >>On Jun 3, 11:45 am, sheldonlg <sheldonlgwrote :
                >>>I am still improving my AJAX and I ran into something that I'm not sure
                >>>how to do.
                >>>I will have a page called where I will have a dropdown list at the top
                >>>and a large display at the bottom. The display depends upon what is
                >>>currently selected in the dropdown. This is readily done via AJAX which
                >>>is activated with an onchange in the dropdown list. The contents of the
                >>>bottom half of the page are created in the php script which is sent back
                >>>to the response handler which changed the bottom part via an innerHTML.
                >>>So far OK. What I would also like to happen is that when the page is
                >>>called initially, that the bottom half shows for the first entry of the
                >>>dropdown list. I don't want to repeat code in two places.
                >>>One solution I have is to have the entire selection and bottom building
                >>>part be an include file that I put into both my AJAX server php script
                >>>and into this display page. I would just use different values for the
                >>>selected item. It would be on the display page as a require_once before
                >>>I do any html display.
                >>>My question is whether there is some better way.
                >>Is the dropdown list dynamic or static?
                >>If it is static and you are using a GET method (which seems logical, I
                >>don't believe you are manipulating any data, merely retrieving it)
                >>then why not just initiate the page using your script passing it the
                >>desired value?
                >It is dynamic. Once in the page, a list is build from the category
                >passed in. It is that list that is then used to display what is in the
                >bottom of the page. The first llist is a db call. The second list is
                >also a db call using the value from the first list. Coming into the
                >page, we have no idea as to what the id will be for the first member on
                >the first list.
                >>
                >The more I think of it, the more the require_once of the building code
                >looks like the way to go. There is no duplication of code (one include
                >file) and it is just exercised in two different places.- Hide quoted text -
                >>
                >- Show quoted text -
                >
                >
                In the code that populates your dropdown, after populating the
                dropdown simply set the selectedIndex of it to 0 and then call the
                function that is called by the onchange handler, or you can even call
                onchange() of the element.
                That is essentially to the way I now handle it. I took the bulk of the
                called function and put it in a separate php file that is included in
                the called function. What is not in that include file is the grabbing
                of the category ID. The category ID is obtained from a $_GET from the
                AJAX call. In the display page I also include that included file, but
                use as the category the first entry from the dropdown list. That way I
                have no duplication of code.
                >
                function loadMyDropDown( ) {
                var select = document.getEle mentById("categ ories");
                //...populate the dropdown...
                if (select.options .length 0) {
                select.selected Index = 0;
                select.onchange ();
                }
                }
                >
                HTH.

                Comment

                • sheldonlg

                  #9
                  Re: Question with AJAX

                  martinpauly@web .de wrote:
                  the bad thing about toms method is, if javascript is deactivated the
                  user will see nothing. I favoured your solution when I had the problem
                  first, for me it worked well. One tip about it: i would ad an ?ajax to
                  all ajax calls, e.g. includes/ajax3.php?ajax
                  by using if(isset($_GET['ajax'])) you can check if your file is called
                  by ajax or if it is included. So if you have differences in some
                  pathes or anything else you can avoid problems with this method
                  Great suggestion.

                  Comment

                  Working...