SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to submit data in multiple combobox

    I am developing student attendance system.... I got stuck in the following code... can anyone please help me how to submit multiple values in database

    require_once('../mysql_connect.php');
    $query="SELECT * FROM stud_info where stream='$st' and sec='$se' and class='$c' ORDER BY name ASC";
    $result=@mysql_query($query);
    if($result)
    {
    echo '<table border="1" align="center" cellspacing="2" cellpadding="2">';
    echo '<caption><font color="blue"><strong>Students of '.$st." Class:".$c." Sec:".$se;'</strong></font></caption>';
    echo' <tr>
    <td align="left"><b>Roll Number</b></td>
    <td align="left"><b>Name</b></td>
    </tr>';
    $i=1;
    while($row=mysql_fetch_array($result,MYSQL_NUM))
    {
    echo "<tr>
    <td align=\"left\">$i</td>
    <td align=\"left\">$row[1]</td>
    <td align=\"left\">
    <select name='ps'>
    <option>Present</option>
    <option>Absent</option>
    </select>
    </td>
    </tr>\n";
    $i++;
    }
    echo '<tr colspan="3" align="center">
    <td> <input type="submit" name="record" value="Submit Attendence"/></td>
    </tr>';
    echo '</table>';

    mysql_free_result($result);
    }

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Okay, from what I gather, you are trying to create a list of students, with a drop down next to each student name that contains "Present" or "Absent", correct?
    Then you want to post that data back to either this page or another page so the instructor can quickly update the status of all students?

    With those assumptions there are two things you need to your existing code before you can read any posted data.
    1) Make name="ps" to read as name="ps[$row[0]]" (I am assuming row[0] is the student id in the table)
    2) Add a value to each <option> tag, ex: <option value="Present">Present</option> (although the browser may do that automatically)

    Now when you submit your form, you will have an array in your $_POST variable for 'ps'.
    You will want to use a foreach loop to read the array, so you can grab the student id from the key, and the Present/Absent from the value.

    Your code will be similar to the following
    PHP Code:
    <?php
      
    if (isset($_POST['record']))
      {
        if (isset(
    $_POST['ps']) && is_array($_POST['ps']))
        {
          foreach (
    $_POST['ps'] as $key => $value)
          {
            
    $studentId intval($key);
            
    $studentPresence = ($value === 'Absent') ? 'Absent' 'Present';
            
    $query="UPDATE <TABLE> SET <PRESENCE COLUMN> = '$studentPresence' WHERE <STUDENT_ID_COLUMN> = $studentId";
            
    $result=@mysql_query($query);
          }
        }
      }
    ?>
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Location
    Novi Sad
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to enclose your form elemets into <form> tag!
    Give them name, like you entered in your <select name="ps"> tag, example:
    Code:
      <form action="yoursubmitfile.php" method="post">
        <select name='ps'>
          <option>Present</option>
          <option>Absent</option>
        </select>
        <input type="submit" name="record" value="Submit Attendence"/>
      </form>
    and process your data in the yoursubmitfile.php
    PHP Code:
    if(isset($_POST)) {
      
    $ps $_POST['ps']; // info about attendance
      
    $query "insert into [tablename] (id, ps) values ([id], $ps)";
      
    // process query into database etc.


  4. #4
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks cpradio ......... I will try to implement and ask you guys if any problem.......


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
  •