SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with form - fixed! Solution Enclosed!! Enjoy!

    I created a form that I use to change some things in my database with drop downs:

    html code the php page produces:
    Code:
    <form name="myForm" id="myForm" action="material2.php" method="post">
    
    <p>
    <input type="hidden" name="ID" id="ID" value="00001" />
    
      01 - 0055 <b>ALUMINUM CHLORIDE HEXAHYDRATE</b>
    
      Material Type: 
      <select type="text" name="materialType" tabindex="3">
        <option value="01" selected="selected">Solid</option>
        <option value="02">Liquid</option>
    
        <option value="03">Eaches to 3</option>
        <option value="04">8 Pad Pack</option>
        <option value="05">Book (4) &amp; Pack (5)</option>
    
        <option value="06">Squares</option>
      </select>
    </p>
    
    <p>
    <input type="hidden" name="ID" id="ID" value="00002" />
    
      01 - 0075 <b>ALUMINUM POTASSIUM SULFATE</b>
      Material Type: 
      <select type="text" name="materialType" tabindex="3">
    
        <option value="01" selected="selected">Solid</option>
        <option value="02">Liquid</option>
    
        <option value="03">Eaches to 3</option>
        <option value="04">8 Pad Pack</option>
        <option value="05">Book (4) &amp; Pack (5)</option>
    
        <option value="06">Squares</option>
      </select>
    </p>
    
    <p>
    <input type="hidden" name="ID" id="ID" value="00003" />
    
      01 - 0081 <b>BORAX</b>
      Material Type: 
      <select type="text" name="materialType" tabindex="3">
    
        <option value="01" selected="selected">Solid</option>
        <option value="02">Liquid</option>
    
        <option value="03">Eaches to 3</option>
        <option value="04">8 Pad Pack</option>
        <option value="05">Book (4) &amp; Pack (5)</option>
    
        <option value="06">Squares</option>
      </select>
    </p>
    
    <p><input type="submit" name="submit" value="submit" tabindex="4" />
    </form></p>
    I was wondering how I can go thru to retreive the values of the ID and materialType in the page posted to. When I submit it and do a var_dump of $_POST, I get:

    array(3) {
    ["ID"]=>
    string(5) "00008"
    ["materialType"]=>
    string(2) "03"
    ["submit"]=>
    string(6) "submit"
    }

    it only looks like the last value is stored.

    help would be great!

    thanks much!
    Last edited by gero1369; Jul 23, 2007 at 23:02. Reason: finished. fixed the problems.

  2. #2
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can u explain a little more ur prob.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would like to update my database using the form that I provided above in the HTML. I am using PHP to try to go thru, get the ID and materialType, then update the database using the ID and materialType.

    query would be:
    "update products set materialType = $materialType where ProdID = ID"

    using php to iterate thru the post data in order to update the database.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can someone help?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. I figured that I need to add brackets to to all of the values to be required (ie ID[] and materialType[] ). I now need to know how to loop thru both arrays at the same time in order to create my sql statement as follows:

    update products set materialType = $materialType where ID = $id

    help would be great. i think it's a foreach loop, but i don't know exactly how to do it.

    thanks much!

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    the most recent development

    I got it to work. For all to enjoy, here is the form:

    Code PHP:
    <form name="myForm" id="myForm" action="material2.php" method="post">
     
    <?php
     
    while ($row = mysql_fetch_array($result)) {
      $id = $row['ProdID'];
      $cat1 = $row['cat_no1'];
      $cat2 = $row['cat_no2'];
      $prodName = $row['prod_name'];
    ?>
    <p>
    <input type="hidden" name="ID[]" id="ID[]" value="<?php echo $id; ?>" />
     
      <?php echo $cat1; ?> - <?php echo $cat2; ?> <b><?php echo $prodName; ?></b>
      Material Type: 
      <select type="text" name="materialType[]" tabindex="3">
        <option value="01" selected="selected">Solid</option>
        <option value="02">Liquid</option>
        <option value="03">Eaches to 3</option>
        <option value="04">8 Pad Pack</option>
        <option value="05">Book (4) &amp; Pack (5)</option>
        <option value="06">Squares</option>
      </select>
    </p>
     
    <?php
    }
    ?>
     
    <p><input type="submit" name="submit" value="submit" tabindex="4" />
    </form></p>


    Here is the page posted to:


    Code PHP:
    <?php
     
    if (isset($_POST['submit'])){
     
    $ID = $_POST['ID'];
    $nowMat = $_POST['materialType'];
     
    foreach ($ID as $k => $v) {
      $sql = "update products set materialType = '$nowMat[$k]' where ProdID = '$v'";
      $ok = mysql_query($sql);
     
      if ($ok) {
        echo '<p>',$v,'updated to ',$nowMat[$k],'<br /></p>';
      }
     
      if (!$ok) {
        echo '<p>ERROR IN YOUR CODE!', mysql_error() ,'</p>';
      } 
    }
     
    $count = count($ID);
    echo $count;
     
    }
     
    ?>
    Last edited by gero1369; Jul 23, 2007 at 23:01. Reason: showing all work


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
  •