0

Hi ,
I am using following script to get the values of the language details.

<FORM action="final.php" method="post">
<div align="center">
  <table width="434" border="0">
    <tr>
      <td>Languages Known</td>
      <td>Speak</td>
      <td>Read</td>
      <td>Write</td>
      <td>Delete</td>
      </tr>
    <tr>
      <td>
        <select name="lang[]" id="select">
          <option value="">-Select-</option>
          <option value=1>Hindi</option>
          <option value=2>English</option>
          <option value=3>Tamil</option>
          <option value=4>Telugu</option>
          <option value=5>Kannada</option>

        </select></td>
      <td><input name="speak[]" type="checkbox" value="yes" /></td>
      <td><input name="read[]" type="checkbox" value="yes" /></td>
      <td><input name="write[]" type="checkbox" value="yes" /></td>
      <td><input type="button" name="button" id="button" value="Delete" onclick="delete(this)" /></td>
      </tr>
    <tr>
      <td>
        <select name="lang[]" id="select">
          <option value="">-Select-</option>
          <option value=1>Hindi</option>
          <option value=2>English</option>
          <option value=3>Tamil</option>
          <option value=4>Telugu</option>
          <option value=5>Kannada</option>

        </select></td>
      <td><input name="speak[]" type="checkbox" value="yes" /></td>
      <td><input name="read[]" type="checkbox" value="yes" /></td>
      <td><input name="write[]" type="checkbox" value="yes" /></td>
      <td><input type="button" name="button2" id="button2" value="Delete" onclick="delete(this)" /></td>
      </tr>
    <tr>
      <td>
        <select name="lang[]" id="select">
          <option value="">-Select-</option>
          <option value=1>Hindi</option>
          <option value=2>English</option>
          <option value=3>Tamil</option>
          <option value=4>Telugu</option>
          <option value=5>Kannada</option>

        </select></td>
      <td><input name="speak[]" type="checkbox" value="yes" /></td>
      <td><input name="read[]" type="checkbox" value="yes" /></td>
      <td><input name="write[]" type="checkbox" value="yes" /></td>
      <td><input type="button" name="button3" id="button3" value="Delete" onclick="delete(this)" /></td>
      </tr>
    <tr>
      <td>
        <select name="lang[]" id="select">
          <option value="">-Select-</option>
          <option value=1>Hindi</option>
          <option value=2>English</option>
          <option value=3>Tamil</option>
          <option value=4>Telugu</option>
          <option value=5>Kannada</option>

        </select></td>
      <td><input name="speak[]" type="checkbox" value="yes" /></td>
      <td><input name="read[]" type="checkbox" value="yes" /></td>
      <td><input name="write[]" type="checkbox" value="yes" /></td>
      <td><input type="button" name="button4" id="button4" value="Delete" onclick="delete(this)" /></td>
      </tr>
    <tr>
      <td>
        <select name="lang[]" id="select">
          <option value="">-Select-</option>
          <option value=1>Hindi</option>
          <option value=2>English</option>
          <option value=3>Tamil</option>
          <option value=4>Telugu</option>
          <option value=5>Kannada</option>

        </select></td>
      <td><input name="speak[]" type="checkbox" value="yes" /></td>
      <td><input name="read[]" type="checkbox" value="yes" /></td>
      <td><input name="write[]" type="checkbox" value="yes" /></td>
      <td><input type="button" name="button5" id="button5" value="Delete" onclick="delete(this)" /></td>
      </tr>
  </table>

</div>
<div align="center">
  <input name="s" type="submit" />
</div>
</FORM>
</body>

I have selected form like this

English Speak
Hindi Read
Tamil Write
*

I am getting the $_POST array as

Array ( [lang] => Array ( [0] => 2 [1] => 1 [2] => 3 [3] => [4] => ) [speak] => Array ( [0] => yes ) [read] => Array ( [0] => yes ) [write] => Array ( [0] => yes ) [s] => Submit Query )

From this I am getting result for first row as

Languages Known Speak Read Write Delete
English yes yes Yes
Hindi
Tamil

I have tried by changing the speak,read and write array as
speak1[],read1[],write1[] for first row
speak2[],read2[],write2[] for second row
But It will affect the result while deleting rows.

Please help me to find a solution for this

Edited by divyakrishnan

3
Contributors
3
Replies
21
Views
3 Years
Discussion Span
Last Post by p@user
1

You have 5 rows of select element and checkboxes so you have to keep track of which row a user has selceted / checked. You can do this if you add row number to the name attributes. So for row No. 1 the attribute would be:

<select name="lang[1]">
<input name="speak[1]" type="checkbox" value="yes" />
...

The easiest way to keep the code manageable is to store languages into an array and then loop through this array and construct all the elements. here is my version of code:

// array of languages
$languages = array(1 => 'Hindi', 2 => 'English', 3 => 'Tamil', 4 => 'Telugu', 5 => 'Kannada');

// the form
// note the action has been set to # to test the output
// adapt this to your needs
echo '<form method="post" action="#">';

// table head
echo '<div align="center">
  <table width="434" border="0">
    <tr>
      <td>Languages Known</td>
      <td>Speak</td>
      <td>Read</td>
      <td>Write</td>
      <td>Delete</td>
      </tr>';

// table rows
for($i = 1; $i <= count($languages); $i++) {
    echo '<tr><td>';

    // select element
    echo '<select name="lang[' . $i . ']">';
    echo '<option value="0">- Select -</option>';
    foreach($languages as $key => $lang) {
        echo '<option value="' . $key . '">' . $lang . '</option>';
    }
    echo '</select></td>';

    // checkboxes
    echo '<td><input name="speak[' . $i . ']" type="checkbox" value="yes" /></td>';
    echo '<td><input name="read[' . $i . ']" type="checkbox" value="yes" /></td>';
    echo '<td><input name="write[' . $i . ']" type="checkbox" value="yes" /></td>';

    // delete button
    echo '<td><input type="button" name="button5" id="button5" value="Delete" onclick="delete(this)" /></td>';

    echo '</tr>';
}
echo '</table>';
// submit butoon
echo '<div align="center">
  <input name="s" type="submit" />
</div>';
echo '</form>';

// if form was submited read and display the data you can check if it is OK
// this is for testing only, you adapt this code to use the data the way you want
if(isset($_POST['s'])) {
    foreach($_POST['lang'] as $rowNo => $l) {
        // if language was selected        
        if($l > 0) {
            echo "Language: " . $languages[$l];
            echo isset($_POST['speak'][$rowNo]) ? ' speak ' : '';
            echo isset($_POST['read'][$rowNo]) ? ' read ' : '';
            echo isset($_POST['write'][$rowNo]) ? ' write ' : '';
            echo '<br>';
        }
    }   
}

My opinion is that you do not need languages in a dropdown since user might select same language more than one time and check different checkboxes.

Also note, that select elements should have unique ID elements (not the same as in your case with id="select" code). The id attributes are not really required here unless you use them in Javascript. For styling you can use class attributes (which do not have to be unique).

0

if i want my answer in this way by your method...please suggest me

Hindi    Yes No Yes

with checkbox value

Edited by p@user

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.