?
Solved

php forms and unknonw variable names.

Posted on 2003-03-06
4
Medium Priority
?
185 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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