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

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2004, 03:04 PM   #1 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
EMERGENCY: Dynamic form processing

AAAAAAAAAAAAAAAAAAAAAGHHHH!!!

I'm currently suffering from not carefully making sure a client knew what they wanted before bidding on this job.

He is a 'website reviewer guy' who offers a free website evaluation service. Client email him and he fills in my 'review form'. There will be about 25-35 form 'components'.

Each 'component' will have a title, a fixed paragraph, a choice of 1-5 star rating and an optional comment box.

The 'thankyou.php' page should take these inputs and save them to a database.

Not a problem for me...except he won't tell me how many of the 'components' I will be coding in!!

This means setting up a 'base set' components of about 10 components.

I done this in a db OK. The review form page should then loop the form's code for each component and dynamically pull them from the db to allow him to fill in the data.

The table 'components' has below columns:
cID (auto inc)
headertxt - this is used in the review form and elsewhere
fixedtxt - this will be used on the client's page when they view thier review form results by pulling it 'dynamically' *cringe* from the db. I won't be covering this yet this should be simple.

I've done the form 'pulling' like this:
PHP Code:
    $i 1//sets the counter
    
$sql mysql_query("SELECT * FROM components"); // this is where those 'components' reside.

    
while ($row mysql_fetch_array($sql))
    {
    echo 
"<tr> 
      <td colspan='2'><BR /><b>"
.$row['headertxt']."</b></td>
    </tr>
    <tr> 
      <td class='bottomborder' colspan='2'> 
        <input type='radio' name='q"
.$i."_star' value='1' class='formborder'>
        1 
        <input type='radio' name='q"
.$i."_star' value='2' class='formborder'>
        2 
        <input type='radio' name='q"
.$i."_star' value='3' class='formborder'>
        3 
        <input type='radio' name='q"
.$i."_star' value='4' class='formborder'>
        4 
        <input type='radio' name='q"
.$i."_star' value='5' class='formborder'>
        5 out of 5</td> // these are radios for the rating
    <tr>
      <td colspan='2' height='22'>
      Offer own service / further action<br />
        <textarea name='recAction"
.$i."' cols='90' class='formborder'></textarea>
      </td>
    <tr> 
      <td colspan='2' height='22' class='bottomBorder'>
      Further comments <font size='-1'>(optional)</font><BR />
        <textarea name='comments"
.$i."' cols='90' class='formborder'></textarea>
      </td>"
;
     
$i++; // increments loops number so every time, the variable is plus one until
          // while condition is false. 
        
// end while loop 
This code displays the form on review.php with dynamic numbering for the elements every time a new component is 'pulled'.

This way, if he was to add a 'component' to the db, it would be called in this page.


The next part is the bit that is doing my head in. For testing I am just trying to ECHO the data used to store to db. If you cna help me then that is all I would like you to do for me is ECHO the results.

What I need to do is loop through each component and echo the returned value. I have tried to think of a solution but I haven't even got that far!

EDIT:To start with I initially thought using a variable inside another variable was God's solution to hellish programming head ferks:
PHP Code:
$count 1
while ($count <= $total_rows)
{
echo 
$_POST['q$count_star'];
$count++

But this obviously isn't possible, so how can I loop through the q1_star, q2_star, etc?

I thought of maybe going through the $_POST[''] array and assigning the value to an increasing variable. After every loop the variable NAME would increment so the next time it would be empty for a new value. But thinking about it now it isn't possible...SH*T!!!

Please help me oh grand masters of CodeNewbie

Great overhaul btw...me likey very much!!

EDIT2: To anyone, if you think it would be easier to DO it rather then explain it then I Can provide all code through email or MSN.

My deadline is 3 days and I'm brickin it all cus this stupid b4st4rd won't tell me how many 'component's I need to code in. If I knew the amount then the cosding would be a piece of cake with next to no loops needed.
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a

Last edited by DavH27; 10-27-2004 at 03:25 PM. Reason: Adding new info / the code I thought worked
DavH27 is offline   Reply With Quote
Old 10-27-2004, 03:35 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
your example is confusing, when you are displaying multiple radio boxes, they should all be NAMED the same, but have different values. i.e.:
HTML Code:
<form method=post action=index.php>
Please select a rating:
<br>
  1 <input type=radio name=rating value=1>
  2 <input type=radio name=rating value=2>
  3 <input type=radio name=rating value=3> <br> <input type=submit> </form>
now, i may be misunderstanding you, but if you did want to offer a rating for every componenet based on a component id, i'd do something like this:
PHP Code:
<form method=post action=index.php>
<?
$result 
mysql_query("select * from components order by headertxt");


while(
$row mysql_fetch_array($result)){
  echo 
$row['headertxt']."<br>";
  echo 
"1 <input type=radio name=c[".$row['cID']."] value=1>
  2 <input type=radio name=c["
.$row['cID']."] value=2>
  3 <input type=radio name=c["
.$row['cID']."] value=3>
  4 <input type=radio name=c["
.$row['cID']."] value=4>
  5 <input type=radio name=c["
.$row['cID']."] value=5><br><br>\n";
}

echo 
"<input type=submit name=submit value=submit></form>";

// if submit, print ( or write to db the values )
if($submit){
  if(
is_array($c)){
    foreach(
$c as $key => $value){
      echo 
"cID=".$key." / value=".$value."<br>\n";
    }
  }
}
hmm .. yeah, i think that is what you're trying to do.
__________________
Mike
sde is offline   Reply With Quote
Old 10-27-2004, 03:38 PM   #3 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
Oh man if this works I could KISS you!

btw the rating name's are different as they are differently named for each 'component' There are 5 radio buttons for each 'component'.

look here http://www.spectre256.com/php/bmdb/d...wer/review.php to see what I'm talking about.

user: admin
pass: guest

Oh god I hope this works. I didn't know you could change a variable name with usign other variables...but I hope this works.
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Old 10-27-2004, 03:46 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
exactly, notice that each row of 5 ratings are the same exact name. i think it will work for ya.
__________________
Mike
sde is offline   Reply With Quote
Old 10-27-2004, 04:15 PM   #5 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
WOOOO!!

I had been putting this page off for liek 2 days now and the dealdine is in another 3!

I had to make 2 changes:

1. The SQL statement can't be ordered as they all come out all over the place in a different order to how they are int he db. I removed the ORDER BY part of the SQL statement to have them ordered like the db orders them.
2. The $c variable isn't global so I change all $c occurences to $_POST['c']

But other then that - it works perfectly!

Thanks, buddy!
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Old 10-27-2004, 04:44 PM   #6 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
Woah hold the phones!

What's happening to my page now?! :'(

It seems to be throwing in a million <BR> that don't exist in the page's code. I could not find any answers from a view source so...being a Firefox user I dragged the cursor from the last letter of the above paragraph to the first letter of the next paragraph and did a 'view selection source' and I saw loadsa <BR>s all lined up looking menacing.

Why are they not there but there when I select them / view selection source?

This is really getting on my nerves. After this job I think I better work on getting a new girlfriend instead!
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Old 10-27-2004, 04:46 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
i don't think php randomly prints br tags, so it must be a loop you have going on somewhere in your script. dont know without seeing it.
__________________
Mike
sde is offline   Reply With Quote
Old 10-27-2004, 04:53 PM   #8 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
I wrapped your code into a table cell but left two <BR>s on the outside of a table row tag like </tr><br> and ti was scarewing everythign else up.

Phew. What a dunce. Duhhh

Ok I gotta expand on the echo code to save it to the db, then code a page that pulls all the info for the user with a certain email address. A page will ask for thier email ad so that it can then fetch all the data in association with it. I might be back for some of that later!

Thanks, again.
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Old 10-27-2004, 08:52 PM   #9 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
And the annoyances continue...

Why can't programming languages just do as I want them to?!

I wanted to INSERT the email address into the 'email' field of 3 tables.

Then inside the loop you generously coded for me, I wanted to UPDATE the db table it associates with with the value. It would do this over n over. Updating each component one after the other in a loop. The code is below. The only part that work is if I typed in an integer into the form.

PHP Code:
<?php

  
if(is_array($_POST['c']))
  {
    foreach(
$_POST['c'] as $key => $value)
    {
      
$query2="
      UPDATE ratings 
      SET q$key = $value"
;
      
mysql_query($query2); 
    }
  }
 

  if(
is_array($_POST['s']))
  {
    foreach(
$_POST['s'] as $key => $value)
    {
      
$value str_replace("\n","<br />",$value);
      
$query3="
      UPDATE service
      SET s$key = $value"
;
      
mysql_query($query3);
    }
  }

  if(
is_array($_POST['msg']))
  {
    foreach(
$_POST['msg'] as $key => $value)
    {
      
$value str_replace("\n","<br />",$value);
       if (
$value == 0)
       {
       
$value == "-";
       }
      
$query3="
      UPDATE comments 
      SET msg$key = $value"
;
      
mysql_query($query3); 
    }
  }
?>
What's happening is the email address is being saved using previous SQL statements but when it comes to the 'meat', it doesn't save anything. No errors show on the thankyou.php page that do this code.

The table fields are set to 'TEXT' so that isn't the problem.

I don't know why it won't work but as soon as I get this job done I swear on my broken CD collection that I'll go find a girlfriend and leave PHP alone to the pros...
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a

Last edited by DavH27; 10-27-2004 at 08:59 PM. Reason: Forgot to say why this isn't working
DavH27 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
HTML form preview then INSERT using PHP & MySQL SteveSoler PHP 16 09-22-2008 12:59 AM
populate form field virtualGeorge PHP 4 08-23-2004 09:05 AM
breaking down an array from a form metazai PHP 12 07-09-2004 07:18 AM
dynamic select menues sde HTML, XML, Javascript, AJAX 5 02-15-2003 10:05 AM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 11:18 AM


All times are GMT -8. The time now is 07:31 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





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