?
Solved

php forms and unknonw variable names.

Posted on 2003-03-06
4
Medium Priority
?
194 Views
Last Modified: 2006-11-17
Hi.
im working on a page so I can order some pizzas.
I don't now much about forms or php, and im not sure if this is the right place to adress the question...

the problem: when I create a pizza, i enter the PizzaID, PizzaName price and.. then I check some chekboxes representing the ingridients.

I don't know the name of the ingridient's (they are in a database), and I don't know how many ingredients there is

it is the ingredients part there is troublesome...

Code is following:

the code output can be seen @
http://swn.adsl.dk/pizza/html/php/opretpizza.php

<----- FIRST, THIS PART IS WORKING  ----->
<form name="opretpizza" action="indsaetpizza.php" method="post">
<table width="400">
  <tr>
    <td><LABEL>nr<br></LABEL><INPUT type="text" name="PID" SIZE="2"></INPUT></td>
    <td><LABEL>navn<br></LABEL><INPUT type="text"="TEXT" name="PNAVN" SIZE="20"></INPUT></td>
    <td><LABEL>pris<br></LABEL><INPUT type="text"="TEXT" name="PVALUE" SIZE="5"></INPUT></td>
    </tr>
    <table>
<?
<----- SELECTING ALL THE INGREDIENTS  ----->
$ingredienserSql= ("select CID,NAME from COMPONENT");
$ingredienserT = mysql_query($ingredienserSql, $mysql_id);
echo('<br><table width="600"><TR>');
while ( $raekke = mysql_fetch_array($ingredienserT)):
echo('<TD width = 150><label><DIV align=right>' . $raekke
<----- !!! I BELIEVE THE FOLLOWING IS THE PROBLEM  ----->
[1] . '<INPUT TYPE=checkbox NAME=' . $raekke[1] .'></DIV></INPUT></label></TD>');

<- how do I catch this on after submit: NAME=' . $raekke->

     endwhile;
     echo('</TR></table><table width = 600><tr><td><div align="right">' .
'<INPUT TYPE=submit  value=Gem>' .
'<INPUT TYPE="reset" name=reset value="Fortryd">' .
'</div></td></TR></table></FORM>');
0
Comment
Question by:cromozon
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
splishsplash earned 150 total points
ID: 8085550
Here's one way to do it. You make all the items in the <INPUT>s


while ( $raekke = mysql_fetch_array($ingredienserT) )
{
    $i++;
    echo "<INPUT TYPE=checkbox NAME=stuff[$i]>";
}


When the user clicks on the submit button, you have the $stuff array you can use to track what ingredients were selected by the user.

So, for example, if the user checks these ingredients:
-tomat
-skinke
-gorgonzolaost

... then when the form gets submitted, the array $stuff will be defined as:
<?php

$stuff[1]='on';
$stuff[3]='on';
$stuff[8]='on';

// this returns another array, an array of
// keys, in this case 1,3 and 8 are each
// elements in the array;
// and..1,3,8 are the ingredients the user picked
array_keys($stuff);

?>
0
 
LVL 6

Expert Comment

by:carchitect
ID: 8086262
write this
'<INPUT TYPE=checkbox NAME={$raekke[1] ></DIV></INPUT> </label></TD>'

instead of this

[1] . '<INPUT TYPE=checkbox NAME=' . $raekke[1] .'></DIV></INPUT></label></TD>');


then on nest page just access $raekke[1]
regards

0
 

Author Comment

by:cromozon
ID: 8088143
thankyou for the quic response.

the page can still be seen @ http://swn.adsl.dk/pizza/html/php/opretpizza.php
after submit you can see the vars outputtet for debugging for now...

splishsplash, I have made it work with your comments.

carchitect, I don't understand tyhe benefits of the change you propose, but I haven't tried it either...

It should be noted that the count(array) only counts the elements with value, so if i tick option 1 and 11 count=2
I have solved the problem as with:

$antal = mysql_fetch_array(mysql_query("select count(CID) from COMPONENT", $mysql_id));
$antal = $antal[0]; //finds the number of ingredients

and then:

do {
     echo ('Index:'. $i.': ');
     echo ('Value:'. $COMPO[$i].'<br>');
     $i++;
} while ($i<=$antal);
---------------------------
and just for the fun, code:

while ( $raekke = mysql_fetch_array($ingredienserT)):
     if ($counter>=4): //lidt formaterings halløj....
          $counter=0;
          echo('</TR><TR>');
     endif;
     $counter++;
     echo('<TD width = 150><label><DIV align=right>' . $raekke[1] . '<INPUT TYPE=checkbox NAME=COMPO['.$i.']</DIV></INPUT></label></TD>');
     $i++;
endwhile;
0
 

Author Comment

by:cromozon
ID: 8088151
the last comment of mine should perhaps have ben added here !
0

Featured Post

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!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

616 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