Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,388 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,131 people online right now. Registration is fast and FREE... Join Now!




Handling Multiple Textboxes with same Name

 
Reply to this topicStart new topic

Handling Multiple Textboxes with same Name

aco99
9 Oct, 2008 - 12:38 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 9

OK... I'ts been so long since I asked for help and yet Im still once again here asking for another...

There is this case of mine, a quiz program, wherein questions will come from a certain table, which is called by certain explode functions and foreach statements. But my problem now is what will I do to save all the answers to the database? Each input box or textarea that emerges from the explode statement is set to a single name. (which I had added [] to my luck that it will support it)... BTW the following code is UP with the help of one fellow around here, wait ill search for his name....

CODE

<form  method="GET" action="submitexam.php" onSubmit="if(!confirm('Are you sure you wanted to submit your E-TEST Paper?'))return false;">
<font face="Verdana, Arial, Helvetica" size="-2">

<?php
    
    if(isset($_GET['ExamID']) && isset($_GET['Subject'])) {
        if($_GET['Subject'] == 'VB') {
            $database = 'vbexambank';
        }
        elseif($_GET['Subject'] == 'CPP') {
            $database = 'cppexambank';
        }
        elseif($_GET['Subject'] == 'SQL') {
            $database = 'sqlexambank';
        }
        elseif($_GET['Subject'] == 'JAVA') {
            $database = 'javaexambank';
        }
        else {
            echo "Invalid Exam ID...";
        }
        $IDe = $_GET['ExamID'];
    $queryfirst = "Select ExamContent from schedexam WHERE ID = '$IDe'";
    $resultset = mysql_query($queryfirst) or die('Unable to Connect to the Exam Database. Please report this to the administrator.');
    $row = mysql_fetch_row($resultset);
$QuestionSet = $row[0];
echo "<font face=\"tahoma\" size=4><b>Do not add any unecessary tabs, spaces, or unwanted symbols<BR>
with your answers because this might lead your answers to be wrong.</b></font><BR><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=4>
<strong>Also, AVOID hitting the ENTER KEY unless you're in the debugging type of exam.<br>
TO AVOID Early SUBMISSION of E-TEST PAPER..</strong><bR>
</font>";
$sections = explode(',',$QuestionSet);
echo "<table>";

foreach($sections as $idx => $val){
  $ids = explode(":",$val);
  $QuestionItems[$ids[0]] = explode('-',$ids[1]);
}

foreach($QuestionItems as $idx => $val) {
    if($idx == 1) {
        $titleexam = '<tr><Td><B><font face=tahoma size=3>Identification</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Identification</font><br>';
    }
    elseif($idx == 2) {
        $titleexam = '<tr><Td><B><font face=tahoma size=3>Modified True or False</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Modified True or False</font><br>';
    }
    elseif($idx == 3) {
        $titleexam = '<tr><Td><B><font face=tahoma size=3>Debugging</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Debugging</font><br>';
    };
    
    echo $titleexam;
    shuffle($val);
        
    foreach($val as $id => $value) {    
    
          $query = "Select QuestionType, Question, ID from ".$database." WHERE ID = '".$value."'";
           $result = mysql_query($query) or die('There was an error fetching the questions from the database. Please try again. Please report to the administrator if this error still exist.');
        $row = mysql_fetch_row($result);
        echo "<tr><td><li></td><td>"; ?>
        <?php
        if ($row[0] == 'DEBUGGING') {
        echo "<textarea rows=10 cols=50 name=ExamContent[".$row[2]."]>$row[1]</textarea><input type=hidden value=$row[2] name=ID[$row[2]]></td>";
        } else {
            echo $row[1];
        } ?>
             <tD>
        <?php
        if ($row[0] == 'DEBUGGING') {
            echo "<textarea rows=10 cols=50 name=ExamContent[".$row[2]."]></textarea><input type=hidden value=$row[2] name=ID>";
        } elseif($row[0] == 'TRUEORFALSE') {
            echo "<select name=ExamContent[".$row[2]."]>
                    <option selected >------</option>
                    <option value=TRUE>True</option>
                    <option value=FALSE>False</option>
                 </select><input type=hidden value=$row[2] name=ID[$row[2]]><input type=text name=Answer[".$row[2]."] size=20>";            
            
        }
        else {
            echo "<input type=text name=ExamContent[".$row[2]."] size=50><input type=hidden value=$row[2] name=ID[$row[2]]> ";
        } ?>            
                     
             </td><p>
               <?php    }
        
}


echo "<input type=hidden value=$_GET[Subject] name=Subject>";
echo "</tr><TR><TD><input type=\"submit\" name=\"Submit\" id=\"submit\" value=\"Submit\"></TD></TR></table>";



} // close if statement isset

else
{
echo "<font size=4><B>You have not selected an exam properly! Click back and select an exam!</B></font>";
}

?>


Now I made this code of mine to call each of entries of each textbox but my problem now is the index of the Array I used... and another fact that every RELOAD / REFRESH of the page randomizes the items makes me more wonder how...

CODE

<?php

    session_start();
    require_once('db.php');
    include('functions.php');
    include('settings.php');
    checkLogin('1 2');
    
    if(isset($_GET['Submit'])) {
        foreach($_GET['ExamContent'] as $value) {
            echo $value.'<BR>';
            $searchValue = $value;
            $key = array_search($searchValue, $_GET['ExamContent']);
            echo $key.'<BR>';
        }
    
    }
    

$array = array("apple", "banana", "cherry"); // DUMMY
$searchValue = "banana";
$key = array_search($searchValue, $array);

echo $key;

?>


As you can see, I made some dummy data about searching for each Index Key of the Array from the List of Items Submitted but, whenever it encounters entries that are the same, LIKE the TRUE OR FALSE section, (of course the TRUE or False Section can have items that have similar items) it returns same keys which is not I expected....

Sorry if I had a hard time writing this thread of mine and how bad my English is.. but if ever someone knows how to deal with this, please?

OK I repeat for more clarification... *Laughs*

I have the first code above to call questions from some table in the database. When I press the submit button, the script file should evaluate each input the user has to give and store each on the database together with his/her user ID...

PS:
The Random Thingy is an essential feature... to minimize the you-know-what thingy

This post has been edited by aco99: 9 Oct, 2008 - 12:49 PM
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Handling Multiple Textboxes With Same Name
10 Oct, 2008 - 12:21 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 396



Thanked: 37 times
Dream Kudos: 75
My Contributions
Give this a shot, and see if it helps:

CODE

<form  method="GET" action="submitexam.php" onSubmit="if(!confirm('Are you sure you wanted to submit your E-TEST Paper?'))return false;">
<font face="Verdana, Arial, Helvetica" size="-2">

<?php
    
    if(isset($_GET['ExamID']) && isset($_GET['Subject'])) {
        
        /* ADDED THIS CODE */
        /* THIS WILL JUST ECHO OUT ALL THE CONTENT FROM EACH ExamContent TEXTAREA THEN exit() THE SCRIPT */
        $examcontent = $_GET['ExamContent'];
        foreach($examcontent as $ec){
            echo $ec.'<br />';
        }
        exit();
        /* END OF ADDED CODE */
        
        if($_GET['Subject'] == 'VB') {
            $database = 'vbexambank';
        }
        elseif($_GET['Subject'] == 'CPP') {
            $database = 'cppexambank';
        }
        elseif($_GET['Subject'] == 'SQL') {
            $database = 'sqlexambank';
        }
        elseif($_GET['Subject'] == 'JAVA') {
            $database = 'javaexambank';
        }
        else {
            echo "Invalid Exam ID...";
        }
        $IDe = $_GET['ExamID'];
    $queryfirst = "Select ExamContent from schedexam WHERE ID = '$IDe'";
    $resultset = mysql_query($queryfirst) or die('Unable to Connect to the Exam Database. Please report this to the administrator.');
    $row = mysql_fetch_row($resultset);
$QuestionSet = $row[0];
echo "<font face=\"tahoma\" size=4><b>Do not add any unecessary tabs, spaces, or unwanted symbols<BR>
with your answers because this might lead your answers to be wrong.</b></font><BR><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=4>
<strong>Also, AVOID hitting the ENTER KEY unless you're in the debugging type of exam.<br>
TO AVOID Early SUBMISSION of E-TEST PAPER..</strong><bR>
</font>";
$sections = explode(',',$QuestionSet);
echo "<table>";

foreach($sections as $idx => $val){
  $ids = explode(":",$val);
  $QuestionItems[$ids[0]] = explode('-',$ids[1]);
}

foreach($QuestionItems as $idx => $val) {
    if($idx == 1) {
        $titleexam = '<tr><Td><B><font face=tahoma size=3>Identification</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Identification</font><br>';
    }
    elseif($idx == 2) {
        $titleexam = '<tr><Td><B><font face=tahoma size=3>Modified True or False</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Modified True or False</font><br>';
    }
    elseif($idx == 3) {
        $titleexam = '<tr><Td><B><font face=tahoma size=3>Debugging</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Debugging</font><br>';
    };
    
    echo $titleexam;
    shuffle($val);
        
    foreach($val as $id => $value) {    
    
          $query = "Select QuestionType, Question, ID from ".$database." WHERE ID = '".$value."'";
           $result = mysql_query($query) or die('There was an error fetching the questions from the database. Please try again. Please report to the administrator if this error still exist.');
        $row = mysql_fetch_row($result);
        echo "<tr><td><li></td><td>"; ?>
        <?php
        if ($row[0] == 'DEBUGGING') {
        echo "<textarea rows=10 cols=50 name=ExamContent[]>$row[1]</textarea><input type=hidden value=$row[2] name=ID[$row[2]]></td>";
        } else {
            echo $row[1];
        } ?>
             <tD>
        <?php
        if ($row[0] == 'DEBUGGING') {
            echo "<textarea rows=10 cols=50 name=ExamContent[]></textarea><input type=hidden value=$row[2] name=ID>";
        } elseif($row[0] == 'TRUEORFALSE') {
            echo "<select name=ExamContent[".$row[2]."]>
                    <option selected >------</option>
                    <option value=TRUE>True</option>
                    <option value=FALSE>False</option>
                 </select><input type=hidden value=$row[2] name=ID[$row[2]]><input type=text name=Answer[".$row[2]."] size=20>";            
            
        }
        else {
            echo "<input type=text name=ExamContent[] size=50><input type=hidden value=$row[2] name=ID[$row[2]]> ";
        } ?>            
                    
             </td><p>
               <?php    }
        
}


echo "<input type=hidden value=$_GET[Subject] name=Subject>";
echo "</tr><TR><TD><input type=\"submit\" name=\"Submit\" id=\"submit\" value=\"Submit\"></TD></TR></table>";



} // close if statement isset

else

{
echo "<font size=4><B>You have not selected an exam properly! Click back and select an exam!</B></font>";
}

?>

User is offlineProfile CardPM
+Quote Post

aco99
RE: Handling Multiple Textboxes With Same Name
11 Oct, 2008 - 02:56 AM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 9

Actually I made that code already

CODE
foreach($_GET['ExamContent'] as $value) {
            echo $value.'<BR>';



But thanks for your effort...

I really need to know what particular QuestionID it is so that I can know how to save the proper answer with the proper question ID that is associated with that answer...


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 02:17AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month