Web Analytics Made Easy -
StatCounter Auto Checkbox when text field is changed. - CodingForum

Announcement

Collapse
No announcement yet.

Auto Checkbox when text field is changed.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Auto Checkbox when text field is changed.

    I have a PHP script where a person can update multiple items from one page. Now part of what that script does is takes multiple items but only those that have their checkboxes check. now while this is good and an effective way of doing multiple updates at 1 time it does seem kind of tedious to have to check alll those boxes if there is more than 50 results, which another part of the site does have. Is their anyway way for the check box to be automatically check when when a change has occurred in a textfield, dropdown menu, or even another checkbox?

    Here is a sample of what I am using. Thanks in advance


    PHP Code:
    <?php
        mysql_connect
    ('localhost''user''pass');
        
    mysql_select_db('test');
        
        if(isset(
    $_POST['submit'])) {
            
    $updatedrows 0;
            foreach(
    $_POST['changed'] as $id) {
                
    $name $_POST["name_$id"];
                
    $quantity $_POST["quantity_$id"];
                
    $query "UPDATE products SET name='$name', "
                       
    "quantity='$quantity' WHERE id=$id";
                
    mysql_query($query);
                
    $updatedrows += mysql_affected_rows();            
            }
        }

        
    //grab data from database
        
        
    $result mysql_query('SELECT * FROM products ORDER BY id');
        
    $products = array();
        while(
    $row mysql_fetch_assoc($result)) {
            
    $products[$row['id']] = array('name'=>$row['name'], 
                                      
    'quantity'=>$row['quantity']);
        }
        
    mysql_close();
    ?>
    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" 
    content="text/html; charset=utf-8" />
    <title>Update Inventory</title>
    </head>
    <body>
    <?php if(isset($updatedrows)) 
            echo 
    $updatedrows " row(s) updated.<br /><br />\n"
    ?>
    <form method="post" 
          action="<?php echo $_SERVER['PHP_SELF']; ?>"
          name="updateform">
    <table border="1">
    <tr>
        <th>Product Name</th>
        <th>Quantity</th>
        <th>Update Row</th>
    </tr>
    <?php foreach($products as $id=>$product) { ?>
    <tr>
        <td><input type="text" size="25" 
                   value="<?php echo $product['name']; ?>"
                   name="name_<?php echo $id?>" />
        </td>
        <td><input type="text" size="4"
                   value="<?php echo $product['quantity']; ?>"
                   name="quantity_<?php echo $id?>" />
        </td>
        <td><input type="checkbox"
                   value="<?php echo $id?>"
                   name="changed[]" />
        </td>
    </tr>
    <?php //end for loop ?>
    </table>
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>
    <?

  • #2
    yes, use boolean values of checked property.

    obj.checked

    something like:

    if(...condition here...){
    document.form_name.checkbox_name.checked = true;
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      I know this sounds silly. Can you give more of an in depth example? I understand the form name part of it, just not the condition.

      Comment


      • #4
        Not the condition... The condition is up to you...
        ...when when a change has occurred in a textfield, dropdown menu, or even another checkbox...
        Here's a simple example:
        PHP Code:
        <html>
        <
        head>
        <
        script>
        function 
        bla(){
        d=document.forms[0];
        if((
        d.ra[0].checked)||(d.te.value == 'yes')){
        d.ch.checked true;
        }
        else if((
        d.ra[1].checked)||(d.te.value == 'no')){
        d.ch.checked false;
        }
        }
        </
        script>
        </
        head>
        <
        body>
        <
        form>
        <
        input name="ch" type="checkbox"><br>
        The above checkbox will be checked/unchecked if:<br>
        <
        br>
          - 
        chose a one of the correspondent radio button<br>
        check<input name="ra" type="radio" onclick="bla()"><br>
        uncheck<input name="ra" type="radio" onclick="bla()"><br>
        input "yes" or "no" in text field bellow<br>
        <
        input name="te" type="text" size="10" onkeyup="bla()">
        <
        br>
        </
        form>
        </
        body>
        </
        html
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          Use onchange

          PHP Code:
          <html>
          <head>
          <script type="text/javascript">
            function check(productID){
               var chk = document.updateform.elements['changed[]'];
               if (typeof chk.length!='undefined'){ //more than 1 record
                  for (var j=0;j<chk.length;j++) {
                     if (chk[j].value==productID){ //search corresponding checkbox
                        chk[j].checked = true;
                        break;
                     }
                  }
               }
               else { //only 1 record
                  if (chk.value==productID){
                     chk.checked = true;
                  }
               }
            }
          </script>
          </head>
          <body>
          ...
          <?php foreach($products as $id=>$product) { ?>
          <tr>
              <td><input type="text" size="25" 
                         value="<?php echo $product['name']; ?>"
                         name="name_<?php echo $id?>" [b]onchange="check('<?php echo $id?>')"[/b] />
              </td>
              <td><input type="text" size="4"
                         value="<?php echo $product['quantity']; ?>"
                         name="quantity_<?php echo $id?>" [b]onchange="check('<?php echo $id?>')"[/b] />
              </td>
              <td><input type="checkbox"
                         value="<?php echo $id?>"
                         name="changed[]" />
              </td>
          </tr>
          <?php //end for loop ?>
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            Re: Use onchange

            Originally posted by glenngv
            PHP Code:
            <html>
            <head>
            <script type="text/javascript">
              function check(productID){
                 var chk = document.updateform.elements['changed[]'];
                 if (typeof chk.length!='undefined'){ //more than 1 record
                    for (var j=0;j<chk.length;j++) {
                       if (chk[j].value==productID){ //search corresponding checkbox
                          chk[j].checked = true;
                          break;
                       }
                    }
                 }
                 else { //only 1 record
                    if (chk.value==productID){
                       chk.checked = true;
                    }
                 }
              }
            </script>
            </head>
            <body>
            ...
            <?php foreach($products as $id=>$product) { ?>
            <tr>
                <td><input type="text" size="25" 
                           value="<?php echo $product['name']; ?>"
                           name="name_<?php echo $id?>" [b]onchange="check('<?php echo $id?>')" />
                </td>
                <td><input type="text" size="4"
                           value="<?php echo $product['quantity']; ?>"
                           name="quantity_<?php echo $id?>" [b]onchange="check('<?php echo $id?>')"[/b] />
                </td>
                <td><input type="checkbox"
                           value="<?php echo $id?>"
                           name="changed[]" />
                </td>
            </tr>
            <?php //end for loop ?>
            [/B]

            WORKS AMAZING!! IS there any good Javascripts you can recommend SO I can learn Javascript?

            Comment


            • #7
              Refer to the references from the sticky thread that liorean posted. And to learn quickly, always visit this forum and participate in any way you can.
              Glenn
              vBulletin Mods That Rock!

              Comment

              Working...
              X