• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Problem with dynamically created textboxes / array handling?

Hi, I have a weird problem where the inputs I key into my dynamically created textboxes get passed / processed OK if each textbox has no more than 3 chars. If either has more than 3, the data gets truncated, e.g hello becomes 'o' or in some cases it is not read at all ( this seems like my array is being treated like a string ? ). I am rather new to php / arrays so perhaps I'm overlooking some basics:

I read the posted values thus:

if($submit) {
foreach($sel_student as $value) {
echo $marks[$value];
echo $remarks[$value];
}
}
?>

and my form script is thus:<form method="post" action="<?php echo $PHP_SELF ?>"">
<table>
 <tr>
  <th></th>
  <th align="left">Student</th>
  <th></th>
  <th align="left">Marks</th>
  <th></th><th></th>
  <th align="left">Remarks</th>
 </tr>

<?

$query = "SELECT students.name,classroster.id, reportmarks FROM students, classroster, reports WHERE students.id = classroster.id AND  

classroster.classid = '$classid'  AND reports.reportid= '$reportid' ORDER BY students.name";

     $result = mysql_query($query);
           
          while ($row=mysql_fetch_row($result)) {
          $id=$row[1];
          echo "<tr>";
                echo "<td><input type=\"hidden\" name=\"sel_student[]\"   value=\"$id\"></td>";
            echo "<td>$row[0]</td>";
                echo "<td></td>";
          echo "<td><input type=\"text\" size=\"3\" name=\"marks[$id]\"></td>";
                                     //echo "<td>out of $row[2]</td>";
          echo "<td></td>";
          echo "<td><input type=\"text\" size=\"30\" name=\"remarks[$id]\"></td>";
                                  echo "<td></td>";                                    
                                         echo "</tr>";

     }
?>
</table>
<br>

<input type="Submit" name="submit" value="Enter Marks">
</form>

0
azaidi
Asked:
azaidi
  • 7
  • 6
  • 2
1 Solution
 
mik0rzCommented:

Think you meant:

echo "<td><input type=\"hidden\" name=\"$sel_student[]\"   value=\"$id\"></td>";

you left out the "$" on the original:

echo "<td><input type=\"hidden\" name=\"sel_student[]\"   value=\"$id\"></td>";


hope this helps

 
0
 
mik0rzCommented:

Oh God! Now I see. Your array is being treated like a string because it is. It's sel_student[] and that's it. HTML doesn't have array's and your PHP isnt doing anything there.

It might be as simple as this:

echo "<td><input type=\"hidden\" name=\"sel_student[$id]\" value=\"$id\"></td>";

But to be perfectly honest, I've never seen code that was written to do something so simple that is so confusing. I would scrap all this code and re-write it.

 
0
 
bobsledbobCommented:

I don't think you even need the hidden form line.  You know that each marks and remarks array will contain one line entry per student.  So, just dump the hidden input line and change your foreach line on top to something like:

$n = sizeof($marks);
for ($i=0; $i < $n; $i++) {
  echo $marks[$i];
  echo $remarks[$i];
}


Standard disclaimer.  Is register_globals On?  If not, you need to use $_POST["marks"] and $_POST["remarks"].

Also, always good to have error_reporting(E_ALL); in your code.

Is you MySQL table structure correct?  Have you inadvertantly set remarks to varchar(3) instead of varchar(30)?  Looking at your form, marks should only have 3 characters?  Just fishing here.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
azaidiAuthor Commented:
Hi Thanks bobsledbob for your advice and time.

Just to clarify, I haven't gotten to the database part of marks / remarks yet as I was facing problems so all I'm doing is setting up some blank textboxes to key marks / remarks into, then test if I can read the correct values. Once I can, then I'll do the INSERT into the marks table.

Nonetheless, remarks is varchar(50) and marks is int(3) and register_globals is ON. I have tried your code and yes it's more readable than the one I had - unfortunately the problem persists - ie, if both textboxes have <=3 chars, everything is fine otherwise truncations

I suspect the issue is somewhere in my definition of echo "<td><input type=\"text\" size=\"30\" name=\"remarks[]\"></td>"; maybe I have inadvertently setup my array as a string or perhaps I need to add something else? or perhaps the mysql_fetch_row needs to be changed although for the kind of query I have from different tables, it's easier to reference with mysql_fetch_row. Quite honestly, I'm stumped and although I have received some advice on the board, I don't have a solution
0
 
bobsledbobCommented:

Inside your while loop, comment out all of your existing code and just try doing this:

while ($row=mysql_fetch_row($result)) {
  echo "<pre>\n";
  print_r($row);
  echo "</pre>\n";
}

This should at least let you see what you're getting inside your $row array.  If you've got problems, they're coming from your SQL statement.  If at this point you don't have problems, then post one of those <pre> iterations so we can see what your $row array data looks like.

Thanks.


0
 
azaidiAuthor Commented:
Hi

have done what you suggest and the foll is the $row info from the SQL query ( which is what I expect and is correct)

First Field is name,
Second field is student's id in the classroster
Third field is the reportid for the report whose marks/remarks I'm trying to generate

Once i get this query in place, all I want to do is to for each row have the foll form in which I can enter data:

Name Marks(textbox) Remarks(textbox).

Array
(
    [0] => Ali Sarfaraz Hameed
    [1] => 51
    [2] => 50
)

Array
(
    [0] => Anjum Qureshi
    [1] => 28
    [2] => 50
)

Array
(
    [0] => Asad Zaidi
    [1] => 20
    [2] => 50
)

Array
(
    [0] => Asif Haider
    [1] => 23
    [2] => 50
)

Array
(
    [0] => Jonathan Wills
    [1] => 49
    [2] => 50
)

Array
(
    [0] => Zia Zaidi
    [1] => 47
    [2] => 50
)

0
 
azaidiAuthor Commented:
Just continuing my previous post,

the $row bit seems OK. I suspect my problem is something to do with the php/html code relating to the input type=text tags or some other reqmnt prior to setting up an array

It seems as if I'm treating a string as an array - the telltale sign is when I e.g type 'hello' - this becomes 'o' . PHP seems to be doing this:

h  e  l   l   o
0  1  2   3   4

So, it's returning $remarks[4] = 'o'. Or if a student has the id 2, the script returns 'l'.

But the question is where am I mixing up a string and an array?

My relevant php settings are:
php 4.23
all magic quotes OFF
register globals ON

Thanks for your time and assistance



0
 
bobsledbobCommented:

It looks like your problem is likely in your foreach loop.  I rewrote it as a while loop using list and each.  This is just easier for me at least.  I also cleaned up your code quite a bit.  Note, you'll have to get rid of my temporary array when you're ready to use actual data from the database.  Here's the code...

<?php
  error_reporting(E_ALL);

  if(isset($_POST["submit"])) {
    while (list ($key, $val) = each ($_POST["marks"])) {
      echo "Student Id: <b>" . $key . "</b> &nbsp; \n";
      echo "Mark: <b>" . $val . "</b> &nbsp; \n";
      echo "Remark <b>" . $_POST["remarks"][$key] . "</b><br>\n";
    }
    echo "<br>\n";
  }
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table border="0" cellpadding="7" cellspacing="0">
  <tr>
   <th align="left">Student</th>
   <th align="left">Marks</th>
   <th align="left">Remarks</th>
  </tr>
<?php
  // a temporary array to test with
  $tempArray = array (
    array ("Ali Sarfaraz Hameed", 51, 50),
    array ("Anjum Qureshi", 28, 50),
    array ("Asad Zaidi", 20, 50),
    array ("Asif Haider", 23, 50),
    array ("Jonathan Wills", 49, 50),
    array ("Zia Zaidi", 47, 50)
  );

/*
  $query = "SELECT students.name,classroster.id, reportmarks FROM students, classroster, reports WHERE students.id = classroster.id AND classroster.classid = '$classid'  AND reports.reportid= '$reportid' ORDER BY students.name";
  $result = mysql_query($query);

  while ($row=mysql_fetch_row($result)) {
*/
  for ($i=0; $i < sizeof($tempArray); $i++) {
    $row = $tempArray[$i];  // delete this line when using the while loop.
    $id = $row[1];
    echo "  <tr>\n";
    echo "    <td>$row[0]</td>\n";
    echo "    <td><input type=\"text\" size=\"3\" name=\"marks[$id]\"></td>\n";
    echo "    <td><input type=\"text\" size=\"30\" name=\"remarks[$id]\"></td>\n";
    echo "  </tr>\n";
  }
?>
</table>
<br>
<input type="Submit" name="submit" value="Enter Marks">
</form>


Try this out and let me know how it goes.

0
 
azaidiAuthor Commented:
Ok I used your entire script, with the $tempArray, so that there is no database select query or while loop at all. Also, using while (list ($key, $val) etc as you suggest instead of foreach ( actually, in my last report I had already dispensed with the foreach ).

Your script runs as expected, but I have exactly the same problem. No errors despite inclusion of error_reporting(E_ALL)etc but the same weird issue ie if either textbox has more than 3 chars, there is truncation, "hello" becomes "o" ...

I can see how your suggestions are helping to reduce the problem to a narrower focus. I guess the remaining areas to check are

a) My php / apache / windows environment - if you can run this script on your system and it works OK, then I guess I need to compare some esoteric php.ini or similar settings. I do have a lot of other php / mysql applications running on my system, such as XOOPS, EzPublish, others with absolutely no problems

b) A PHP 4.23 quirk?

c) The only other place is the 2 <input lines. Some of the previous comments on this thread seemed to indicate that these needed checking

Thank you very much
0
 
bobsledbobCommented:

I think you've got a browser problem.  It works perfectly for me.  What browswer are you using?

Can you put my script on your live server so that I can come check it out?  See if i get your problem?


I entered in some test data and got back as my result:

Student Id: 51   Mark: 1234   Remark Ali Sarfaraz Hameed is a Good Student
Student Id: 28   Mark: 5678   Remark Anjum Qureshi is a Good Student
Student Id: 20   Mark: AABBCCDD   Remark Asad Zaidi is a Bad Student
Student Id: 23   Mark: OU812   Remark Asif Haider is a Good Student
Student Id: 49   Mark: hello   Remark Jonathan Wills is a Bad Student
Student Id: 47   Mark: What's up doc?   Remark Zia Zaidi is a Good Student


I don't see where your truncation is.  Let's keep digging.

Load it up on your website and let me take a look.  Also, include a link to the php script, but as a different extension, like .phps, .txt, etc. so that I can download your script and run on my server as well.

Let me know what browser you're using.

I don't know 4.2.3 would have a bug like this.  But, no since not checking bugs.php.net anyway.
0
 
azaidiAuthor Commented:
Hi

Great to know it's explainable by the 4.23 bug. THANK YOU VERY MUCH for your help and your persistence; a lot of other guys would have walked off many comments ago.And I'll be sure to use some of your cleaner code and tips in my final code.
0
 
azaidiAuthor Commented:
bobsledbob is really helpful, and has helped me to solve my problem without being judgemental about me being a newbie  
0
 
bobsledbobCommented:

thanks for the good word!  too bad I got a B =)

that was a pretty obscure bug.  Who would have thought?  glad we were able to track it down.  =)

0
 
azaidiAuthor Commented:
Hi

bobsledbob, this is the first time I used expert exchange and I had a really productive learning experience in learning about php while getting your assistance to track down the problem. Not sure why you got a B? My regrets if I botched it up.

Thanks again
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now