facarroll 0 Junior Poster in Training

Here is my form.It works fine.Because it is a radio button form, only one variable of the six available is chosen. This variable is echoed in the relevant hidden field. Then when the variable from the hidden field is posted, it is supposed to be converted to one of six specified strings and then posted to the database into column "egroup". The problem I have is that it always goes to the last variable and posts the string "egroup6". I want to break out of the str_replace routine and post the replacement name which matches the sequence of the hidden field selection.
How can I rewrite the str_replace routine so that it breaks when it finds the selected variable?

The code for the form;

<table width="700" align="center" cellpadding="5">
                    <form action="adduser2.php" method="post" enctype="multipart/form-data">
                    
                    <input name="egroup1" type="hidden" value="<?php echo "$group1"; ?>">
                    <input name="egroup2" type="hidden" value="<?php echo "$group2"; ?>">
                    <input name="egroup3" type="hidden" value="<?php echo "$group3"; ?>">
                    <input name="egroup4" type="hidden" value="<?php echo "$group4"; ?>">
                    <input name="egroup5" type="hidden" value="<?php echo "$group5"; ?>">
                    <input name="egroup6" type="hidden" value="<?php echo "$group6"; ?>">
                      <tr>
                        <td colspan="3"><font color="#OO99CC"><?php echo "$errorMsg"; ?> </font></td>
                      </tr>
                      <tr>
                        <td width="200"><div align="right">
                            <p><br />
                              Enter Student Name: 
                          </div></td>
                        <td width="125"><p><br />
                            <input name="userId" type="text" value="<?php echo "$studName"; ?>" />
                        </td>
                        <td width="350">&nbsp;
                          <input name="userPwd" type="hidden" value="<?php echo "$activatecode"; ?>" />
                          <font size="-1" color="#006600">(The password </font><font size="+2" color="#0000FF"><?php echo "$activatecode"; ?></font><font size="-1" color="#006600"> has been allocated to this next student. Print out your full list of student passwords <a href="../reports/reports_page.php">here</a>.)</font></td>
                      </tr>
                      <tr>
                        <td></td>
                      </tr>
                      <tr>
                        <td width="200"><div align="right">Allocate this student to <br />
                            one of these quiz groups:</div></td>
                        <td width="125" colspan="1"><input type="radio" name="userGroup" value="'<?php echo "$group1"; ?>'" />
                          <?php echo "$group1"; ?><br />
                          <input type="radio" name="userGroup" value="'<?php echo "$group2"; ?>'" />
                          <?php echo "$group2"; ?><br />
                          <input type="radio" name="userGroup" value="'<?php echo "$group3"; ?>'" />
                          <?php echo "$group3"; ?><br />
                          <input type="radio" name="userGroup" value="'<?php echo "$group4"; ?>'" />
                          <?php echo "$group4"; ?><br />
                          <input type="radio" name="userGroup" value="'<?php echo "$group5"; ?>'" />
                          <?php echo "$group5"; ?><br />
                          <input type="radio" name="userGroup" value="'<?php echo "$group6"; ?>'" />
                          <?php echo "$group6"; ?><br /></td>
                        <td width="350" align="right" ><p><?php echo $bottlinks; ?></td>
                      </tr>
                      <tr>
                        <td><input name="userCollege" type="hidden" value="<?php echo "$school"; ?>" />
                          <input name="managerId" type="hidden" value="<?php echo "$userid"; ?>" />
                          <div align="right"></div></td>
                        <td><input type="submit" name="Submit" value="Submit Form" /></td>
                      </tr>
                    </form>
                  </table>

When I post the data I use this code;

// Filter the posted variables
    $studName = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['userId']); 
    $activatecode = preg_replace("/^A-Z a-z0-9]/", "", $_POST['userPwd']);     
    $school = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['userCollege']); 
    $userGroup = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['userGroup']); 
    // variables posted from hidden fields
    $egroup1 = $_POST['egroup1'];
    $egroup = str_replace($egroup1, "egroup1", $egroup1);
    $egroup2 = $_POST['egroup2'];
    $egroup = str_replace($egroup2, "egroup2", $egroup2);
    $egroup3 = $_POST['egroup3'];
    $egroup = str_replace($egroup3, "egroup3", $egroup3);
    $egroup4 = $_POST['egroup4'];
    $egroup = str_replace($egroup4, "egroup4", $egroup4);
    $egroup5 = $_POST['egroup5'];
    $egroup = str_replace($egroup5, "egroup5", $egroup5);
    $egroup6 = $_POST['egroup6'];
    $egroup = str_replace($egroup6, "egroup6", $egroup6);
    $managerid = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['managerId']); // filter everything but spaces, numbers, and letters

My insert statement;

$sql = mysql_query("INSERT INTO users (userId, userPwd, userCollege, userGroup, egroup, managerId, doe) 
        VALUES('$studName','$activatecode','$school', '$userGroup', '$egroup', '$managerid', now())") or die (mysql_error());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.