?
Solved

Problem with dynamically created textboxes / array handling?

Posted on 2003-03-16
15
Medium Priority
?
225 Views
Last Modified: 2013-12-12
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
Comment
Question by:azaidi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 

Expert Comment

by:mik0rz
ID: 8149948

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
 

Expert Comment

by:mik0rz
ID: 8150055

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
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8164756

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
Industry Leaders: 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!

 

Author Comment

by:azaidi
ID: 8167467
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
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8168569

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
 

Author Comment

by:azaidi
ID: 8175427
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
 

Author Comment

by:azaidi
ID: 8175610
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
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8176684

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
 

Author Comment

by:azaidi
ID: 8183093
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
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8183286

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
 
LVL 2

Accepted Solution

by:
bobsledbob earned 225 total points
ID: 8183324
0
 

Author Comment

by:azaidi
ID: 8185829
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
 

Author Comment

by:azaidi
ID: 8185836
bobsledbob is really helpful, and has helped me to solve my problem without being judgemental about me being a newbie  
0
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8187270

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
 

Author Comment

by:azaidi
ID: 8219826
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question