Using Onchange or Onclick

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessy
    New Member
    • Oct 2006
    • 106

    #1

    Using Onchange or Onclick

    I have a form which contains a combo box and a table ..i need that whenever the user selects an option from the combo box the page is reloaded but with the table data related to that client he choosed from the box .. i really need help with this and im very confused whether to use <select onchange= > or <option onclick>
    and also how to tell him to reload same page
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    --Moved to Javascript.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      typically you use the onchange-event of the select-node. and for a submit you may call a function that uses the submit()-method

      Comment

      • bnashenas1984
        Contributor
        • Sep 2007
        • 257

        #4
        Here's what you can do:

        Code:
        <select onchange="this.form.submit()">
        Don't forget to put your combo box in a form.
        Each time the user changes the value of your combo box the form gets submited

        Hope it helps

        Comment

        • jessy
          New Member
          • Oct 2006
          • 106

          #5
          Okay now i managed to make the data changes with the user's selection BUT i faced another huge problem which is
          the user is presented with many options(clients ) which are generated in a while loop and an option which is not in the while loop which states ALL clients
          when the page loads for the first time the data in the table are for all clients but once i select any option and then try to select ALL clients again Nothing appears so i was asking if their anyway i could handle this
          here's the code:
          Code:
          <select onChange=this.form.submit(); name="clients"> 
          <option >All Clients </option>
          <?$query=mysql_query("select * from clients");
              while($row=mysql_fetch_array($query))
              {   
              	
             echo"<option value=$row[c_id] >$row[c_name]</option>";	
              	
              }
          $sql="select sum(order_price) from orders";
              
               if($_POST[clients])
                  { 
                  	$clients=$_POST[clients];
                
                  $sql="select sum(order_price) from orders where client_id=$clients";
                   }
               
                    $query=mysql_query($sql);
                     
                         $row=mysql_fetch_array($query);

          Comment

          • bnashenas1984
            Contributor
            • Sep 2007
            • 257

            #6
            That's because you don't have a VALUE attribute for your first option which is ALL CLIENTS

            When your page loads for the first time , the code recieves no value from your combo box but when you choose ALL CLIENTS for the second time then your PHP code recieves ALL CLIENT.

            What you need to do is to set a NULL value for your first option like this

            Code:
            <option value="">All clients</option>
            Post back if there is any other problem

            Comment

            • jessy
              New Member
              • Oct 2006
              • 106

              #7
              Okay i made what u say But still facing the same problem nothing happens when i press ALL CLIENTS :(

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Your onchange submits the form. Check that your PHP code displays all clients when the value of clients is empty.

                Comment

                • jessy
                  New Member
                  • Oct 2006
                  • 106

                  #9
                  Yeah thats exactly what i need to do !!
                  display all clients when value is empty ? all clients are displayed only once when the page loads for the very first time but then nothing appears again

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Your PHP code needs a bit of alteration too. On line 11, you have:
                    Code:
                    if($_POST[clients])
                    you need to add a condition for when the POST value is empty.

                    Note that you can also avoid a page reload using JavaScript/Ajax.

                    Comment

                    • jessy
                      New Member
                      • Oct 2006
                      • 106

                      #11
                      Okay, now i added a condition but nothing changes

                      Any help on how to use AJax for this or any tutorial ...!

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        What condition did you add?

                        Ajax is useful if you want to avoid the page reload. If that's not a requirement, then it's not necessary. If you want a tutorial, check out the links in the Off-site Links thread.

                        Comment

                        • jessy
                          New Member
                          • Oct 2006
                          • 106

                          #13
                          :( i added the Condition

                          Code:
                           if($_POST[clients]&&!empty($_POST[clients]))
                          also i checked some ajax tutorial but felt very depressed things r not that easy i thought

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Shouldn't $_POST[clients] be $_POST['clients']? If you have more problems with the PHP code, start a new thread in the PHP forum.

                            Comment

                            Working...