Skip to content Skip to sidebar Skip to footer

How To Pass Value Of For Loop From One Page To Next Page In Php?

hi everyone my problem is this that i dynamically creted rows of table on the basis user input. Now each row contain textbox,comboxbox on selcting value from combobox n entering va

Solution 1:

You need to be passing an array, you can deal with it as an array in the PHP side.

To do this, in each input, change the name="X" to name="X[]"

Example:

        <input type="text" name="t2" id="in1-<?php echo $i; ?>" onblur="getText3(<?php echo $i; ?>)" />

to:

        <input type="text" name="t2[<?php echo $i; ?>]" id="in1-<?php echo $i; ?>" onblur="getText3(<?php echo $i; ?>)" />

Also, it's generally a bad idea to use @'s in php to suppress errors.

Try something like:

  var_dump($_POST);

and you will see how the data comes through to you.

Hint:

if (isset($_POST['t1']) {
   $t1_data = $_POST['t1'];
}

** EDIT **

Try something like this:

<?php

if (isset($_POST['submit'])) {

    $data_t1 = $_POST['t1'];
    foreach ($data_t1 as $key => $value) {

        echo 'T1: ' . $value . '<br />';
        echo 'T2: ' . $_POST['t2'][$key] . '<br />';
        echo 'Username: ' . $_POST['username'][$key] . '<br /><br />';

    }

}
?>
<html>
<head>
    <script>
        function getText3(row){
            var in1=document.getElementById('in1-' + row).value;
            var in2=document.getElementById('in2-' + row).value;
            var in4=document.getElementById('in4-' + row).value;
            var in3=(in1*in2*in4*30)/1000;
            document.getElementById('in3-' + row).value=in3.toFixed(2 );

        }
    </script>
</head>
<body>
<form action="" method="POST">
<table border="1" align="center" id="wr123">
    <tr>
        <th>WAS</th>
        <th>NO.</th>
        <th>AVERAGE</th>
        <th>APPROX</th>
    </tr>
    <?php

    for ($i = 1; $i < 4; $i++) {
    ?>
    <tr>
        <td>Tube</td>
        <td>
            <select id="in4-<?php echo $i; ?>" name="t1[<?php echo $i; ?>] onclick="getText3(<?php echo $i; ?>)" >
                <option value="0">0</option>
                <option value="12">12</option>
                <option value="18">18</option>
                <option value="24">24</option>
                <option value="75">75</option>
            </select>
        </td>
        <td>
            <input type="text" name="t2[<?php echo $i; ?>]" id="in1-<?php echo $i; ?>" onblur="getText3(<?php echo $i; ?>)" />
        </td>
        <td>
            <select name="a1[<?php echo $i; ?>]" id="in2-<?php echo $i; ?>" onclick="getText3(<?php echo $i; ?>)" >
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>
        </td>
        <td>
            <input type="text" id="in3-<?php echo $i; ?>" name="username[<?php echo $i; ?>]" readonly="readonly" />
        </td>
        <td>
    </tr>
    <?php } ?>
    </table>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Post a Comment for "How To Pass Value Of For Loop From One Page To Next Page In Php?"