Current location: Hot Scripts Forums » Programming Languages » PHP » Submitting a "generated" form


Submitting a "generated" form

Reply
  #1 (permalink)  
Old 02-27-07, 01:28 PM
needforhelp needforhelp is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Submitting a "generated" form

Hello,
I'm "generating" a form from the database,So the number of fields is always different.
Is there some way to submit such form?
Thanks in advance for any help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-27-07, 02:34 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Do you have the code thats generates the form from the database?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-28-07, 05:01 AM
needforhelp needforhelp is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your response!
Here's the source code:
PHP Code:

<?php

if (!isset($_POST['btnsubmit'])) {
echo 
"
<form name='formmenu' method='post' action='navmenu.php'>
"
;
$count 0
$query 
mysql_query("SELECT * FROM navmnu ORDER BY position ASC") or die(mysql_error());
while (
$row mysql_fetch_object($query)) 
  { 
echo 
"
Link 
$count
Name <input type='text' name='field[]' value='
$row->desc'
<input type='hidden' name='hfield[]' value='
$row->desc'

<p>
URL <input type='text' name='field2[]' value='
$row->link'
 <input type='hidden' name='hfield2[]' value='
$row->link'

</p>
<hr>
"
;
    
$count++;
  }
echo 
"<input type='submit' name='btnsubmit' value='submit!'>";
I've figured out how to insert,But now I'm attempting to figure out how to update the fields.
Here's a code to insert:
PHP Code:

foreach ($_POST['field'] as $field)
{
$value htmlspecialchars($field);

mysql_query("INSERT INTO navmnu (name) VALUES ('" $value "')") or die();

As you may see,I've added some hidden fields with the current values & attempted to do the update like that:
PHP Code:

    $value htmlspecialchars($field);
    
$value2 htmlspecialchars($field2);
    
mysql_query("UPDATE menu set name= VALUES ('" $value "') WHERE name= VALUES('" $value2 ."')") or die(); 
But that doesn't seem to work.
Any help would be appreciated.
Thanks in advance!

Last edited by Nico; 02-28-07 at 05:36 AM. Reason: Please use [php] wrappers when posting PHP code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-28-07, 05:57 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
to update a database field, you have to use this query:
PHP Code:

mysql_query("UPDATE menu set name='" $value "' WHERE name= '" $value2 ."'") or die(); 

UnrealEd
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-28-07, 07:07 AM
needforhelp needforhelp is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot for your reply!
I have the following code:
PHP Code:

foreach ($_POST['field'] as $field)


{
$value htmlspecialchars($field);

}  
foreach (
$_POST['hfield'] as $currentfield)

{
$currentvalue htmlspecialchars($currentfield); 

mysql_query("UPDATE menu set name='" $value "' WHERE name= '" $currentvalue ."'") or die();  

it gives me the following error message:
Quote:
Warning: Invalid argument supplied for foreach() in /public_html/admin/navmenu.php on line 101
I can't figure out what's wrong.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 02-28-07, 07:32 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
that's because $_POST['hfield'] is not an array, but a string. and neither is $_POST['field']

you have to replace field2[] and hfield2[] with field[] and hfield[].
than it will work

UnrealEd
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 02-28-07, 07:48 AM
needforhelp needforhelp is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!
But now there's another problem:
It'll update all the rows to the value of the last field in the form.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 02-28-07, 08:04 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
if i were you, i would just remove the hfield from your form, and just grab them on the form processing page.
then store the hfields in an array, where the keys matches the keys of the $_POST['field'] array. Then loop over the $_POST['field'] array and update the record in the database.

UnrealEd
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-04-07, 08:42 AM
needforhelp needforhelp is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry for such a late response (I've made something else),But I can't seem to find a valid function for the updating part.
I've tried
PHP Code:

foreach ($_POST['field'] as $field
foreach (
$_POST['hfield'] as $currentfield

$value123 htmlspecialchars($field);
$currentvalue htmlspecialchars($currentfield); 
mysql_query("UPDATE menu set name='" $value123 "' WHERE id= '" $currentvalue ."'") or die();  

But that didn't worked because it's adding the same value multiply times to all the rows.
So what's the correct PHP function to use?
Thanks in advance & sorry for so many troubles.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 03-05-07, 06:44 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
try this:
PHP Code:

foreach($_POST['field'] as $k => $field){
  
$value23 htmlspecialchars($field);
  
$currentvalue htmlspecialchars($_POST['hfield'][$k]);
  
mysql_query("UPDATE menu set name='" $value123 "' WHERE id= '" $currentvalue ."'") or die(mysql_error());

i'm not sure if it's gonna work though. just give it a try

UnrealEd
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Submitting a form that is in a frame drky JavaScript 8 08-21-06 01:41 AM
Submitting form error Tiffany677 PHP 4 05-16-06 05:47 PM
Help; Submitting Form Mephistopheles Script Requests 3 03-14-06 06:44 PM
PHP pages - form submitting 1jetsam PHP 4 11-08-04 10:28 PM
formmail problem gscraper Perl 12 08-27-04 04:06 AM


All times are GMT -5. The time now is 06:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.