-3

im new here. i have problem and in great hurry :( scenario : i want to make an dynamic input form. user input a number of columns and rows they want. then, it generate the table as the number inputted in previous form. after that, user input a random number to the table, and click submit button. system will record the data and process it. finally, system will show the average (just the example).

table generate succesfully. the problem is how to save/read the data inputted in input form.

this is input.php

<tr> 
<td width="30%" align="left" valign="top">Input the number of columns     :<td      height="37" align="left" valign="top"><input type="text" name="colums" value="" /></td> 
</tr> 
<tr> 
<td align="left" valign="top">Input the number of rows :</td> 
<td width="70%" align="left" valign="top"><input type="text" name="rows" value="" /></td> 
</tr> 
<tr> 
<td align="left" valign="top">&nbsp;</td> 
<td align="left" valign="top"><input type="submit" name="button" id="button" value="Submit" /></td> 
</tr> 
</table> 
</form> 

and the result php like this..

<?php 
$columns = $_POST['columns']; 
$rows = $_POST['rows']; 

echo "<form method='post' action='process.php'>"; 
echo "<table>"; 

//made the rows 
for ($i= 0; $i <= $rows-1; $i++){ 

//and the colums 
echo "<tr>"; 
for ($j = 0; $j <= $colums-1; $j++) { 

//here is the input form, and each of data inputed here that i want to save it. 
$sum = array('[$i][$j]'); 
echo "<td> </td> 
    <td><input size='5' type='text' name='data".$sum."' /></td> 
"; 

}
echo "</tr>";  
} 


echo "<tr><td></td><td><input type='submit' name='submit' values='Submit' /></td></tr>"; 
//im not sure here in value='$sum' 
echo "<tr><td></td><td><input type='hidden' name='banyak' value='$sum' /></td></tr>"; 
echo "</table>"; 
echo "</form>"; 

// here,which one should i $_GET[''] ? 

?>

and the process.php show the average from data. i made it totally wrong in process.php

4
  • 2
    Oh, you're "in great hurry"! it means you'll finally pay my salary! Commented Jul 8, 2012 at 13:56
  • if I have enough money, I would not ask here. Commented Jul 8, 2012 at 14:04
  • I understand, but this is not the right place to ask this kind of questions. Anyway, if I were you, I would read the manual, here and here Commented Jul 8, 2012 at 14:09
  • im sorry,i always do search before asking. and for now i don't see any solution. thanks for the link. Commented Jul 8, 2012 at 14:12

3 Answers 3

3

Check this:

<?php 
$columns = 3; //test value
$rows    = 2; //test value

echo "<form method='post' action='process.php'>"; 
echo "<table>"; 

//made the columns 
for ($row_idx= 0; $row_idx <=$rows-1; $row_idx++){ 

    //and the rows 
    echo "<tr>"; 
    for ($col_idx = 0; $col_idx <= $columns-1; $col_idx++) { 

    //here is the input form, and each of data inputed here that i want to save it. 
    $sum = array('[$row_idx][$col_idx]'); //??? What's for that?
    echo "<td></td> 
        <td><input size='5' type='text' name='data[{$row_idx}][{$col_idx}]' /></td> 
    "; 

    } 
}

echo "</tr>"; 
echo "<tr><td></td><td><input type='submit' name='submit' values='Submit' /></td></tr>"; 
//im not sure here in value='$sum' 
echo "<tr><td></td><td><input type='hidden' name='banyak' value='$sum' /></td></tr>"; 
echo "</table>"; 
echo "</form>"; 

// here,which one should i $_GET[''] ? 

echo "<PRE>";
print_r($_REUQEST['data']);
echo "</PRE>";

?>
  1. You sould name your variables to be understandable.
  2. You should name your input fileld like an array and it will be parsed by php, so <input name='data[1][2]' value="foo"> will "arrive" to by like $data = array('1' => array('2' => "foo"));
1
  • 1
    I moved your code from CodePad into the answer itself. Remember that SO is meant to be a repository of useful answers that other people can find later. Links to places like CodePad can eventually go stale, rendering your code inaccessible to future readers. Commented Jul 8, 2012 at 14:22
0

As @Braiba said replace

$sum = array('[$i][$j]');

with

$sum = "[$i][$j]";

and then you use

$my_data = $_GET['data']; 

Which will be a two dimensional array.

3
  • yes,i wanna make it two dimensional array, but i don't now how to get it :D Commented Jul 8, 2012 at 14:20
  • I just said. Use $_GET['data']. You can print_r($_GET['data']); to see how it looks. Commented Jul 8, 2012 at 14:53
  • ah I see now that you use post in the form. So use $_POST['data'] Commented Jul 8, 2012 at 14:56
-2

Replace

$sum = array('[$i][$j]');

with

$sum = "[$i][$j]";

(note the double quotes)

Also move the closing tr tag up a line so its actually inside the loop and remove the row that contains the hidden input. Even if you needed that value, you shouldn't create a row for it; it will just look confusing.

edit: I should probably try to read what the question actually is, rather than just seeing broken code and trying to fix it. As other people have now said, once you do all that it will submit as $_GET['data']

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.