Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-02-2006, 10:25 AM   #1 (permalink)
tomycon
Registered User
 
Join Date: Jul 2002
Location: Massachusetts
Posts: 6
tomycon is on a distinguished road
Table data from db as new form input

Hi all,

Ive got a page with code that generates a table populated with values from a postgresql db. My code looks like this:
PHP Code:
    $dbconn pg_Connect ("dbname=bkadata user=httpd password=httpd");

    if (!
$dbconn) {
             echo 
"</table>An error occurred.\n";
             exit;
    }

    
$result pg_Exec($dbconn,
             
"SELECT joblog.job_no, joblog.job_name, joblog.status, joblog.bill_staff
                FROM joblog
                WHERE job_no >= 205000 AND job_no < 300000
                ORDER BY job_no DESC;"
);

    if (!
$result) {
             echo 
"</table>An error occurred.\n";
             exit;
    }

    
$num pg_NumRows($result);
    
$i 0;

    while (
$i $num) {
            if (
$i ==0) {
                
$bgcolor='#f3f3f3';
                } else {
                
$bgcolor='#e3e3e3';
                }
            echo 
"<TR bgcolor=$bgcolor><TD>";
            echo 
pg_Result($result$i"job_no");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"job_name");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"status");
            echo 
"</TD><TD>";
      echo 
pg_Result($result$i"bill_staff");
      echo 
"</TD></TR>";
            
$i++;
    }

    
pg_FreeResult($result);
    
pg_Close($dbconn); 
This works fine. Now I want to make the resulting table a form, with a submit icon in each row that will pass a value from the particular row chosen when a user clicks on the icon. The submit takes the user to another page where more info about the row chosen is displayed in another table generated from a new query to a different db.

So far I've got this code:
PHP Code:
$dbconn pg_Connect ("dbname=bkadata user=httpd password=httpd");

    if (!
$dbconn) {
             echo 
"</table>An error occurred.\n";
             exit;
    }

    
$result pg_Exec($dbconn,
             
"SELECT joblog.job_no, joblog.job_name, joblog.status, joblog.bill_staff
                FROM joblog
                WHERE job_no >= 205000 AND job_no < 300000
                ORDER BY job_no DESC;"
);

    if (!
$result) {
             echo 
"</table>An error occurred.\n";
             exit;
    }

    
$num pg_NumRows($result);
    
$i 0;

    while (
$i $num) {
            if (
$i ==0) {
                
$bgcolor='#f3f3f3';
                } else {
                
$bgcolor='#e3e3e3';
                }
                
$jobno[] = pg_Result($result$i"job_no");

            echo 
"<TR bgcolor=$bgcolor><TD>";
            echo 
"<INPUT TYPE=hidden  name=cntct_jno value=";
            echo 
$jobno[$i];
            echo 
">";
            echo 
$jobno[$i];
            echo 
"</imput></TD><TD>";
            echo 
"<input type=image src=contactb2.gif border=0 width=24 height=24 alt=contactb2.gif value=submit>";
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"job_name");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"status");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"bill_staff");
            echo 
"</TD></TR>";
            
$i++;
    }

    
pg_FreeResult($result);
    
pg_Close($dbconn); 
The above code displays the initial table with the new submit icon fine. But when I click on the icon for any row, it always passes the value for the last row in the table only!

I've tried several variations on the above code usually with the same result. What am I missing here?

Any help is appreciated!

Thanks!
tomycon is offline   Reply With Quote
Old 03-02-2006, 10:41 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
the first thing i see is the closing input tag:
PHP Code:
echo "</imput></TD><TD>"
the problem is with the way you are forming your html though. the submit button, submits the entire form ( not just the last hidden input )

you could either make a new form for each table row, or just send the job id in the URL.

the first option, your html row would look like this:
PHP Code:
while ($i $num) {
            if (
$i ==0) {
                
$bgcolor='#f3f3f3';
                } else {
                
$bgcolor='#e3e3e3';
                }
                
$jobno[] = pg_Result($result$i"job_no");

            echo 
"<form method=POST action=...><TR bgcolor=$bgcolor><TD>";
            echo 
"<INPUT TYPE=hidden  name=cntct_jno value=";
            echo 
$jobno[$i];
            echo 
">";
            echo 
$jobno[$i];
            echo 
"</imput></TD><TD>";
            echo 
"<input type=image src=contactb2.gif border=0 width=24 height=24 alt=contactb2.gif value=submit>";
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"job_name");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"status");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"bill_staff");
            echo 
"</TD></TR></form>";
            
$i++;
    } 
or just pass the job variable in the url and make the image a link.

PHP Code:
while ($i $num) {
            if (
$i ==0) {
                
$bgcolor='#f3f3f3';
                } else {
                
$bgcolor='#e3e3e3';
                }
                
$jobno[] = pg_Result($result$i"job_no");

            echo 
"<TR bgcolor=$bgcolor><TD>";
            echo 
$jobno[$i];
            echo 
"</imput></TD><TD>";
            echo 
"<a href=\"nextpage?cntct_jno={$jobno[$i]}\"><img  src=contactb2.gif border=0 width=24 height=24 alt=contactb2.gif border=0></a>";
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"job_name");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"status");
            echo 
"</TD><TD>";
            echo 
pg_Result($result$i"bill_staff");
            echo 
"</TD></TR>";
            
$i++; 
the idea is that you just send the job number in the url with a normal link around your icon. i.e. http://mysite.com/nextpage.php?cntct_jno=15

then, in the next page, you use use $_GET['cntct_jno'] to get the variable.
sde is offline   Reply With Quote
Old 03-03-2006, 05:26 AM   #3 (permalink)
tomycon
Registered User
 
Join Date: Jul 2002
Location: Massachusetts
Posts: 6
tomycon is on a distinguished road
Thank You!

Very helpful and informative too! I'm fairly new at this and had not come across the $_GET['cntct_jno'] function. That alone will solve some other issues I'll be getting into!
tomycon is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert HTML form data into mysql database ? plomon PHP 5 02-06-2005 08:23 AM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 05:15 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting