SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert using Array

    Hello Friends,

    I have created a form which has by default 4 rows and 4 columns. Once the user click on More(which as bottom of the table) one more row will be added to the form. When the user click on submit their information will be submitted. Every student will access this form and submit or save it. But when the row is empty it shuld not be entered in the database. How should i insert this information in the database.


    The table name is survey having fields
    -survey_id
    -student_id
    -local
    -duties
    -skill

    Also is it possible to make validation that if local is entered and duties and skill or vice versa is not entered than an alert message should come up.


    Here is my code for more help

    HTML Code:
    <html>
    <head><body bgcolor='#F0F0F0'>
    <script type="text/javascript">
    
    function debug_ie () 
    {  
    var body = document.getElementsByTagName('body').item(0);  
    var debug = document.createElement('textarea');  
    debug.setAttribute( 'rows', 30 );  
    debug.setAttribute( 'cols', 60 );  
    debug.setAttribute( 'wrap', 'off' );  
    debug.value = document.getElementsByTagName('html').item(0).innerHTML;  
    body.appendChild( document.createElement('br') );  
    body.appendChild( debug );
    }
    
    function delete_row ( target ) {
      do {
        if ( target.nodeName.toUpperCase() == 'TR' ) {
          target.parentNode.removeChild(target);
          break;
        }
      } while ( target = target.parentNode );
    }
    
    function add_row ( table ) {
      if( typeof document.getElementById == 'undefined' || 
        typeof document.createElement == 'undefined' ) {
        return; // Only DOM browsers
      }
      var table = document.getElementById(table) ;
    
      var pos = 0;
      var authors = [];
      for ( var i = 0; i < table.rows.length; i ++ ) {
        var row = table.rows.item(i);
        if ( /author/.test( row.className ) ) {
          authors.push(row);
          pos ++;
        }
      }
      
      var tr = document.createElement('tr');
      tr.setAttribute('class','author');
      
      var td1 = document.createElement('td');
      var field = document.createElement('select');
      field.setAttribute('name','local[]');
      field.setAttribute('id','local[]');
      field.options[0] = new Option('--Select--','');
      field.options[1] = new Option('Local','Local');
      field.options[2] = new Option('Central','Central');
      //field.setAttribute('value',pos); // testing purposes
      td1.appendChild(field);
      tr.appendChild(td1);
    
          
      var td2 = document.createElement('td');
      var field = document.createElement('textarea');
      field.setAttribute('name','duties[]');
      field.setAttribute('id','duties[]');
      //field.setAttribute('type','textarea');
      field.setAttribute('cols','25');
      field.setAttribute('rows','4');
      //field.setAttribute('value',pos); // testing purposes
      td2.appendChild(field);
      tr.appendChild(td2);
      
      var td3 = document.createElement('td');
      var field = document.createElement('textarea');
      field.setAttribute('name','skills[]');
      field.setAttribute('id','skills[]');
      //field.setAttribute('type','textarea');
      field.setAttribute('cols','25');
      field.setAttribute('rows','4');  
      //field.setAttribute('value',pos); // testing purposes
      td3.appendChild(field);
      tr.appendChild(td3);
    
    
      var td4 = document.createElement('td');
      var button = document.createElement('input');
      button.setAttribute('onclick','delete_row(this)');
      button.setAttribute('value','Delete');
      button.setAttribute('type','button');
      button.onclick = function () 
    { 
     delete_row( this );  
    };
      td4.appendChild(button);
      tr.appendChild(td4);
    
      var next_node = authors[pos -1];
      while ( next_node = next_node.nextSibling ) {
        if ( next_node.nodeName.toUpperCase() == 'TR' ) {
          break;
        }
      }
    
      if ( table.tBodies.length ) {
        if ( next_node ) {
          table.tBodies[0].insertBefore( tr, next_node );
        } else {
          table.tBodies[0].appendChild( tr );
        }
      } else {
        if ( table.tBodies.length ) {
          table.insertBefore( tr, next_node );
        } else {
          table.appendChild( tr );
        }
      }
    }
    
    </script>
    </head><br><br>
    <body>
    <form name="form1" method="post" action="add_submit.php">
      <table align="center" cellpadding="4" cellspacing="0" id="tblView" border="1" >
      <tr align="center">
    <td valign="top">Local/Central</td>
    
    <td>Duties</td>
    <td>Skill Set</td>
    <td>Action</td>
    </tr>
           <tr class="author">
          <td ><select name="local[]">
    								<option VALUE="">--Select--
    								<option VALUE="Local">Local
    								<option VALUE="Central">Central
    								</select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td></td>
        </tr>
        <tr class="author">
          <td><select name="local[]">
    								<option VALUE="">--Select--
    								<option VALUE="Local">Local
    								<option VALUE="Central">Central
    								</select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        <tr class="author">
          <td><select name="local[]">
    								<option VALUE="">--Select--
    								<option VALUE="Local">Local
    								<option VALUE="Central">Central
    								</select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        <tr class="author">
          <td><select name="local[]">
    								<option VALUE="">--Select--
    								<option VALUE="Local">Local
    								<option VALUE="Central">Central
    								</select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        
        <tr>
                                    <td valign="top" colspan="4" align="center">
    <input name="cmdAddMore" type="button" id="cmdAddMore" value="More" onClick="add_row('tblView')" />
    <input type="Submit" name="Submit" value="Submit" onclick="javascript:return submit_form();">
          </td>
          
        </tr>
      </table>
    </form>
    </body>
    </html>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for ($i 0$i count($_POST['local']); $i++) {
      
    $local $_POST['local'][$i];
      
    $duties $_POST['duties'][$i];
      
    $skills $_POST['skills'][$i];

      if (!empty(
    $local) && !empty($duties) && !empty($skills)) {
        
    //all 3 fields were filled in for this row, insert into the database
      




  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help ...I made little correction in your if condition..Instead of AND i used or...coz if the user have enter only local than it was not going in the datbase.

    Now how would i make a edit page??Even the add page will have Add more row feature....And even if the user has earlier enter 2 rows i have to add 2 more rows...as the main structure should have alteast 4 rows for the user to enter the information. If they want to add more they can click on add more feature. how to put the update query and also insert query if they enter a new row.

    Here is the code of my edit page


    PHP Code:
    $query  = "SELECT * FROM survey where student_id='$id'";

    $result = mysql_query($query);
    $n = mysql_numrows($result);
    ?>

    <form enctype="multipart/form-data" name="form"  method="post" action="edit_submit.php?i=2" name='form'>
      <table align="center" cellpadding="4" cellspacing="0" id="tblView" border="1" >
       <tr align="center">
    <td valign="top">Local/Central</td>

    <td>Duties</td>
    <td>Skill Set</td>
    <td>Action</td>
    </tr>
      <?while($row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        echo 
    '<tr class="author">';
        
    $a=$row['local'];
        
    ?>
        <td>
    <select name="local[]">
      <option <? if($a == ""){ echo 'selected'; } ?>  value="">--Select--</option>
      <option <? if($a == "Local"){ echo 'selected'; } ?>  value="Local">Local</option>
      <option <? if($a == "Central"){ echo 'selected'; } ?>  value="Central">Central</option>
     
      </select></td>
        
        <?
        
    echo "<td><textarea rows='4' cols='25' name='duties[]'>{$row['duties']}</textarea> </td>" .
             
    "<td><textarea rows='4' cols='25' name='skills[]'>{$row['skill']}</textarea> </td>" ;
     echo       
    '<td><input type="button" value="Delete" onclick="delete_row(this)"></td>';

        echo 
    '</tr>';

      
    }

    if (
    $n>=4)
    {
    }
    elseif (
    $n==3)
    {
        
    ?>
        
        <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        
        <?
        
    }
    elseif (
    $n==2)
    {
        
    ?>
        
        <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
    <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        
        <?
        
    }

    elseif (
    $n==1)
    {
        
    ?>
        
        <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
    <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
    <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        
        <?
        
    }

    elseif (
    $n==0)
    {
        
    ?>
        
        <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
        </tr>
    <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
    <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
    <tr class="author">
          <td><select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>
        
        <?
        
    }


      
    ?>    
        <tr>
                                    <td valign="top" colspan="4" align="center">
    <input name="cmdAddMore" type="button" id="cmdAddMore" value="More" onClick="add_row('tblView')" />
    <input type="Submit" name="Submit" value="Save" onClick="javascript:subit(1);">
    <input type="Submit" name="Submit" value="Submit" onclick="javascript:return submit_form();">
          </td>
          
        </tr>
      </table>
    Thanks

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Rather than duplicate the same HTML by pasting it over and over, why not put it in your code once and use a loop to print it the appropriate number of times?

    As far as editing goes, add the identifier to each row
    <input type="hidden" name="id[]" value="<?php echo $row_id; ?>" />

    And you'll know which values go with that identifier using a loop similar to before.

    $id = $_POST['id'][$i];

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you help me out with making changes in my code? I dont know how come you figure out that in less than 2-3 minutes....Please try to help me out.....This is the only portion left for my school project....

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK. This is the change to your edit page:

    PHP Code:
    <?php

    $query  
    "SELECT * FROM survey where student_id='$id'";

    $result mysql_query($query);
    $n mysql_num_rows($result);
    ?>

    <form enctype="multipart/form-data" name="form"  method="post" action="edit_submit.php?i=2" name='form'>
      <table align="center" cellpadding="4" cellspacing="0" id="tblView" border="1" >
       <tr align="center">
    <td valign="top">Local/Central</td>

    <td>Duties</td>
    <td>Skill Set</td>
    <td>Action</td>
    </tr>
      <?php
    while($row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        echo 
    '<tr class="author">';
        
    $a=$row['local'];
        
    ?>
        <td>
    <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>" />
    <select name="local[]">
      <option <? if($a == ""){ echo 'selected'; } ?>  value="">--Select--</option>
      <option <? if($a == "Local"){ echo 'selected'; } ?>  value="Local">Local</option>
      <option <? if($a == "Central"){ echo 'selected'; } ?>  value="Central">Central</option>

      </select></td>

        <?
        
    echo "<td><textarea rows='4' cols='25' name='duties[]'>{$row['duties']}</textarea> </td>" .
             
    "<td><textarea rows='4' cols='25' name='skills[]'>{$row['skill']}</textarea> </td>" ;
    echo       
    '<td><input type="button" value="Delete" onclick="delete_row(this)"></td>';
        echo 
    '</tr>';

    }

    //Display between 0 and 4 additional rows depending on $n
    for ($i 0$i < ($n); $i++) {
    ?>

        <tr class="author">
          <td>
            <input type="hidden" name="id[]" value="" />
            <select name="local[]">
                                    <option VALUE="">--Select--
                                    <option VALUE="Local">Local
                                    <option VALUE="Central">Central
                                    </select></td>
          <td><textarea rows="4" cols="25" name="duties[]"></textarea></td>
          <td><textarea rows="4" cols="25" name="skills[]"></textarea></td>
          <td><input type="button" value="Delete" onclick="delete_row(this)"></td>
        </tr>


    <?php
    }
    ?>

        <tr>
                                    <td valign="top" colspan="4" align="center">
    <input name="cmdAddMore" type="button" id="cmdAddMore" value="More" onClick="add_row('tblView')" />
    <input type="Submit" name="Submit" value="Save" onClick="javascript:subit(1);">
    <input type="Submit" name="Submit" value="Submit" onclick="javascript:return submit_form();">
          </td>
          
        </tr>
      </table>
    Now in the page that processes this form,

    PHP Code:
    for ($i 0$i count($_POST['local']); $i++) {
      
    $local $_POST['local'][$i];
      
    $duties $_POST['duties'][$i];
      
    $skills $_POST['skills'][$i];
      
    $id $_POST['id'][$i];

      if (!empty(
    $id)) {
        
    //This row corresponds to an existing entry in your database you need to UPDATE
      
    } else {
        
    //This row corresponds to a new entry that you need to INSERT
      
    }


    I made the assumption that the identifier for the rows in your surveys table is also called "id". If it isn't, then you need to change the echo in line:

    PHP Code:
    <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>" />
    To whatever it is.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot...so what about the row which shows up on clicking add more..dont we have to write their id too?

    and the update query will be like

    UPDATE isurvey SET local='$cal', duties='$val', skill='$bal' where student_id='$student_id' and survey_id='$id'

    and insert query will be like

    INSERT INTO it_survey (local,duties,skill,student_id) VALUES ('".$local."','".$duties."','".$skills."',".$student_id."')

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You have two choices:

    1. Update your JavaScript to write another hidden input as in the HTML above, having the name "id[]".

    2. Update the code that processes the form. It knows the row was created by JavaScript by the absense of an ID for that row.

    PHP Code:
    if (!isset($_POST['id'][$i])) {
      
    //This is a new row created by JavaScript

    You'll also want to change $row['id'] to $row['survey_id'] at the part I referenced in the assumption note.

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know which one to choose...as i dont know how to do both of them. Please try to help me out...

    here is my javascript

    HTML Code:
    <script type="text/javascript">
    
    function debug_ie () 
    {  
    var body = document.getElementsByTagName('body').item(0);  
    var debug = document.createElement('textarea');  
    debug.setAttribute( 'rows', 30 );  
    debug.setAttribute( 'cols', 60 );  
    debug.setAttribute( 'wrap', 'off' );  
    debug.value = document.getElementsByTagName('html').item(0).innerHTML;  
    body.appendChild( document.createElement('br') );  
    body.appendChild( debug );
    }
    
    function delete_row ( target ) {
      do {
        if ( target.nodeName.toUpperCase() == 'TR' ) {
          target.parentNode.removeChild(target);
          break;
        }
      } while ( target = target.parentNode );
    }
    
    function add_row ( table ) {
      if( typeof document.getElementById == 'undefined' || 
        typeof document.createElement == 'undefined' ) {
        return; // Only DOM browsers
      }
      var table = document.getElementById(table) ;
    
      var pos = 0;
      var authors = [];
      for ( var i = 0; i < table.rows.length; i ++ ) {
        var row = table.rows.item(i);
        if ( /author/.test( row.className ) ) {
          authors.push(row);
          pos ++;
        }
      }
      
      var tr = document.createElement('tr');
      tr.setAttribute('class','author');
      
      var td1 = document.createElement('td');
      var field = document.createElement('select');
      field.setAttribute('name','local[]');
      field.setAttribute('id','local[]');
      field.options[0] = new Option('--Select--','');
      field.options[1] = new Option('Local','Local');
      field.options[2] = new Option('Central','Central');
      //field.setAttribute('value',pos); // testing purposes
      td1.appendChild(field);
      tr.appendChild(td1);
    
          
      var td2 = document.createElement('td');
      var field = document.createElement('textarea');
      field.setAttribute('name','duties[]');
      field.setAttribute('id','duties[]');
      //field.setAttribute('type','textarea');
      field.setAttribute('cols','25');
      field.setAttribute('rows','4');
      //field.setAttribute('value',pos); // testing purposes
      td2.appendChild(field);
      tr.appendChild(td2);
      
      var td3 = document.createElement('td');
      var field = document.createElement('textarea');
      field.setAttribute('name','skills[]');
      field.setAttribute('id','skills[]');
      //field.setAttribute('type','textarea');
      field.setAttribute('cols','25');
      field.setAttribute('rows','4');  
      //field.setAttribute('value',pos); // testing purposes
      td3.appendChild(field);
      tr.appendChild(td3);
    
    
      var td4 = document.createElement('td');
      var button = document.createElement('input');
      button.setAttribute('onclick','delete_row(this)');
      button.setAttribute('value','Delete');
      button.setAttribute('type','button');
      button.onclick = function () 
    { 
     delete_row( this );  
    };
      td4.appendChild(button);
      tr.appendChild(td4);
    
      var next_node = authors[pos -1];
      while ( next_node = next_node.nextSibling ) {
        if ( next_node.nodeName.toUpperCase() == 'TR' ) {
          break;
        }
      }
    
      if ( table.tBodies.length ) {
        if ( next_node ) {
          table.tBodies[0].insertBefore( tr, next_node );
        } else {
          table.tBodies[0].appendChild( tr );
        }
      } else {
        if ( table.tBodies.length ) {
          table.insertBefore( tr, next_node );
        } else {
          table.appendChild( tr );
        }
      }
    }
    
    </script>


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
  •