i m new to php , i want to know how get data from multiple textboxes ,can i name text boxes as like a array, is it correct ,and how to display them
i return them like this echo $_POST[txtName[0]]
but it is not working , plz help me

this is the code

<html>
<head><title>Student Marks Calculator </title></head>
<body>
<form name ="My Data" method ="POST">
<table>
<tr>
<td>Name</td><td>Maths</td><td>Science</td><td>English</td>
</tr>
<tr
<td><input type="text" name=txtName[0]/></td>
<td><input type="text" name=txtMaths[0] /></td>
<td><input type="text" name=txtScience[0] /></td>
<td><input type="text" name=txtEnglish[0] /></td>
</tr>
<tr>
<td><input type="text" name=txtName[1]" /></td>
<td><input type="text" name=txtMaths[1]" /></td>
<td><input type="text" name=txtScience[1]" /></td>
<td><input type="text" name=txtEnglish[1]" /></td>
</tr>
<tr>
<td><input type="text" name=txtName[2] /></td>
<td><input type="text" name=txtMaths[2] /></td>
<td><input type="text" name=txtScience[2] /></td>
<td><input type="text" name=txtEnglish[2] /></td>
</tr>
<tr>
<td><input type="text" name=txtName[3] /></td>
<td><input type="text" name=txtMaths[3] /></td>
<td><input type="text" name=txtScience[3] /></td>
<td><input type="text" name=txtEnglish[3] /></td>
</tr>
<tr>
<td><input type="Submit" name="Report" value="Gen Report" /></td>
<td><input type="Submit" name="Reset" value="Reset" /></td>
</tr>

<?php

?>

</body>
</html>

Recommended Answers

All 9 Replies

Member Avatar for diafol
<td><input type="text" name=txtName[]/></td>
<td><input type="text" name=txtMaths[] /></td>
<td><input type="text" name=txtScience[] /></td>
<td><input type="text" name=txtEnglish[] /></td>

Just name each series like this. You pick them up like this:

$_POST etc.

This will contain the array.

BTW place quotes around your name attribute values (name="txtName[]"), otherwise you fail xhtml validation.

<td><input type="text" name=txtName[]/></td>
<td><input type="text" name=txtMaths[] /></td>
<td><input type="text" name=txtScience[] /></td>
<td><input type="text" name=txtEnglish[] /></td>

Just name each series like this. You pick them up like this:

$_POST etc.

This will contain the array.

BTW place quotes around your name attribute values (name="txtName[]"), otherwise you fail xhtml validation.

thank you very much for your help !!!!!!!!:)

Member Avatar for diafol

Are we solved?

may be Yes He said thank you hahaha!

Member Avatar for diafol

may be Yes He said thank you hahaha!

What? I know he said thank you, but it's good practice to mark a thread solved so that contributors do not waste their time trying to help thread starters when the issue has been resolved.

call the police! :-) or the moderator hahaha!

Are we solved?

Thanks a lot ardav, i have another question , plz tell me if we enter two valuse to the 2 textboxes in a same array
<td><input type="text" name=txtName[]/></td>
<td><input type="text" name=txtName[]/></td>
how do it store the two values. as a example we enter 1)Lewis to first textbox
2)jenny to the second textbox

we cant return the 2 values like echo $_POST'];like that ,
how do we return these 2 values
plz help me

Member Avatar for diafol

OK. Note my point about quotes around the attribute value.
Say you have this sort of thing going on:

<td><input type="text" name="txtName[]"/></td>
<td><input type="text" name="txtMaths[]" /></td>
<td><input type="text" name="txtScience[]" /></td>
<td><input type="text" name="txtEnglish[]" /></td>

<td><input type="text" name="txtName[]"/></td>
<td><input type="text" name="txtMaths[]" /></td>
<td><input type="text" name="txtScience[]" /></td>
<td><input type="text" name="txtEnglish[]" /></td>

<td><input type="text" name="txtName[]"/></td>
<td><input type="text" name="txtMaths[]" /></td>
<td><input type="text" name="txtScience[]" /></td>
<td><input type="text" name="txtEnglish[]" /></td>

I assume from this you're placing a student's name in txtName with grades or marks in the others.

You can have as many of these inputs as you like in the form, but perhaps use and 'add more rows' or 'delete this row' javascript functionality to avoid having loads of empty rows. Even if the textbox is empty, the resulting post array will have a value for it (=""). However, back to the problem:

Each "record" (a student's name and grades) will be given a unique position within each of the four post arrays. Remember that they always start with position '0', unless coded otherwise.

//these are all arrays!
$name = $_POST['txtName'];
$maths = $_POST['txtMaths'];
$sci = $_POST['txtScience'];
$eng = $_POST['txtEnglish'];

$i = 0;
while($i < count($name)){
  if(trim($name[$i]) != ""){
     ... clean the data...
     $rs = mysql_query("INSERT INTO mytable SET student='{$name[$i]}', maths='{$maths[$i]}',science='{$sci[$i]}',english='{$eng[$i]}'");
     ... check the data has been added...
     $i = $i + 1;
  }
}

As well as inserting new records you could as easily use UPDATE - however for this you'd use the student id to filter the recordset.

Recap:

student #1
name = $name[0]
maths grade = $maths[0]
science grade = $sci[0]
English grade = $eng[0]

student #2
name = $name[1]
maths grade = $maths[1]
science grade = $sci[1]
English grade = $eng[1]

ETC, ETC.

OK. Note my point about quotes around the attribute value.
Say you have this sort of thing going on:

<td><input type="text" name="txtName[]"/></td>
<td><input type="text" name="txtMaths[]" /></td>
<td><input type="text" name="txtScience[]" /></td>
<td><input type="text" name="txtEnglish[]" /></td>

<td><input type="text" name="txtName[]"/></td>
<td><input type="text" name="txtMaths[]" /></td>
<td><input type="text" name="txtScience[]" /></td>
<td><input type="text" name="txtEnglish[]" /></td>

<td><input type="text" name="txtName[]"/></td>
<td><input type="text" name="txtMaths[]" /></td>
<td><input type="text" name="txtScience[]" /></td>
<td><input type="text" name="txtEnglish[]" /></td>

I assume from this you're placing a student's name in txtName with grades or marks in the others.

You can have as many of these inputs as you like in the form, but perhaps use and 'add more rows' or 'delete this row' javascript functionality to avoid having loads of empty rows. Even if the textbox is empty, the resulting post array will have a value for it (=""). However, back to the problem:

Each "record" (a student's name and grades) will be given a unique position within each of the four post arrays. Remember that they always start with position '0', unless coded otherwise.

//these are all arrays!
$name = $_POST['txtName'];
$maths = $_POST['txtMaths'];
$sci = $_POST['txtScience'];
$eng = $_POST['txtEnglish'];

$i = 0;
while($i < count($name)){
  if(trim($name[$i]) != ""){
     ... clean the data...
     $rs = mysql_query("INSERT INTO mytable SET student='{$name[$i]}', maths='{$maths[$i]}',science='{$sci[$i]}',english='{$eng[$i]}'");
     ... check the data has been added...
     $i = $i + 1;
  }
}

As well as inserting new records you could as easily use UPDATE - however for this you'd use the student id to filter the recordset.

Recap:

student #1
name = $name[0]
maths grade = $maths[0]
science grade = $sci[0]
English grade = $eng[0]

student #2
name = $name[1]
maths grade = $maths[1]
science grade = $sci[1]
English grade = $eng[1]

ETC, ETC.

wooow cool , thnx a lot friend ,i m relly happy with your answer

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.