This is a discussion on form button names as variables within the alt.comp.lang.php forums, part of the PHP Programming Forums category; perhaps I am just a little tired, but I am having trouble with my form buttons. Firstly I name them ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
perhaps I am just a little tired, but I am having trouble with my form
buttons. Firstly I name them like this name=\"round".$counter."heats\" which results in variables $round1heats $round2heats $round3heats etc... now, when I select button named ... "round1heats" ... I want to use the variable $round1heats to remove the button "round1heats", and display heats set for the round. To do that I assumed that I would have to:- IF(isset($round1heats)){ show other stuff instead etc }else{ generate and print html for button "round1heats" } My problem is, since all this is dynamic, since the number of heats is variable, and is dependant on DB entries, how do I generate the $round1heats used in the isset() so that it is recognised as a variable by the isset(). I have tried, to concat the bits as I have done in the naming of the button, but, unsurprisingly, it spits out errors ;) [note, $counter is still available, and is limited to the number of rounds registered in DB.] OR, have I been barking up the wrong tree altogether? I hope that made a bit of sense . PhilM |
|
|||
|
"PhilM" <philm@nospam.com.am> wrote in message
news:41066686$0$18191$afc38c87@news.optusnet.com.a u... > perhaps I am just a little tired, but I am having trouble with my form > buttons. > > Firstly I name them like this > name=\"round".$counter."heats\" > which results in variables > $round1heats > $round2heats > $round3heats > etc... > now, when I select button named ... "round1heats" ... I want to use the > variable $round1heats to remove the button "round1heats", and display heats > set for the round. To do that I assumed that I would have to:- > IF(isset($round1heats)){ > show other stuff instead etc > }else{ > generate and print html for button "round1heats" > } > > My problem is, since all this is dynamic, since the number of heats is > variable, and is dependant on DB entries, how do I generate the $round1heats > used in the isset() so that it is recognised as a variable by the isset(). > I have tried, to concat the bits as I have done in the naming of the button, > but, unsurprisingly, it spits out errors ;) > [note, $counter is still available, and is limited to the number of rounds > registered in DB.] > OR, have I been barking up the wrong tree altogether? > > I hope that made a bit of sense . > > PhilM > Try using arrays and then working through the arrays in order. name=\"round[$counter]\" so you get name="round[1]" name="round[2]" etc. Then you can set a loop to work through $round[$loopnumber] to see which is set etc. See http://www.php.net/manual/en/ref.array.php Nel. |
|
|||
|
There are a few things you could try to improve the routine.
One is to use $_GET or $_POST instead of using registered global variables, as these are considered much more secure. For example, if your form tag is method="post" then you would do IF(array_key_exists("round{$count}heats",$_POST)){ show other stuff instead etc }else{ generate and print html for button "round1heats" } One thing to note is that if the submit buttons are type="image" then the form will post round1heats.x, round1heats.y or round1heats_x, round1heats_y to correspond to the x and y pos that the user clicked on the image, so that'd need to be taken into consideration as well. Finally if these buttons need to stay the same if the database changes a lot, then you should load all the database info you need into a session and then draw the buttons from that rather then from the database each time. -- Regards, Andrew Crowe |
|
|||
|
"Andrew Crowe" <andrewcrowe_uk@yahoo.co.uk> wrote in message news:41066e8d$0$26230$afc38c87@news.easynet.co.uk. .. > There are a few things you could try to improve the routine. > > One is to use $_GET or $_POST instead of using registered global variables, > as these are considered much more secure. > > For example, if your form tag is method="post" then you would do > > IF(array_key_exists("round{$count}heats",$_POST)){ > show other stuff instead etc > }else{ > generate and print html for button "round1heats" > } > > > > One thing to note is that if the submit buttons are type="image" then the > form will post round1heats.x, round1heats.y or round1heats_x, > round1heats_y to correspond to the x and y pos that the user clicked on the > image, so that'd need to be taken into consideration as well. > > > Finally if these buttons need to stay the same if the database changes a > lot, then you should load all the database info you need into a session and > then draw the buttons from that rather then from the database each time. > > -- > Regards, > Andrew Crowe > > Many thanks. My head doesn't hurt as much now ;) I am using method="post". I am not using images, tho that is something to remember. (Am using styles to make hrefs and form buttons look identical.) these buttons are generated when certain other conditions are met, and are only on display when a competition is edited. ie not a full time presence as part of any template. When there is the need to edit the heats in a round, the button is redundant when the heats are displayed, as I have another that will pass info back. Tried the code, and works a treat. Once again, many thanks. PhilM |
|
|||
|
you could do simply as you are doing putting in html : name=round1heats ...
and then makin in the php code something like: for ($i=0;$i<$max_rows;$i++) { if (isset($_POST["round".$i."heats"]) && $_POST["round".$i."heats"] != "") { //do something else //do something else } if you use register global ON this will work too: .... $name = "round".$i."heats"; if (isset($$name) && $$name != "") { .... or you can use array in the form... or a lot of other things. but i suggest to you to spend some more time in it and build a good object to work with your forms or look at something like this: http://pear.php.net/package/HTML_QuickForm bye Luciano PhilM wrote: > perhaps I am just a little tired, but I am having trouble with my form > buttons. > > Firstly I name them like this > name=\"round".$counter."heats\" > which results in variables > $round1heats > $round2heats > $round3heats > etc... > now, when I select button named ... "round1heats" ... I want to use the > variable $round1heats to remove the button "round1heats", and display heats > set for the round. To do that I assumed that I would have to:- > IF(isset($round1heats)){ > show other stuff instead etc > }else{ > generate and print html for button "round1heats" > } > > My problem is, since all this is dynamic, since the number of heats is > variable, and is dependant on DB entries, how do I generate the $round1heats > used in the isset() so that it is recognised as a variable by the isset(). > I have tried, to concat the bits as I have done in the naming of the button, > but, unsurprisingly, it spits out errors ;) > [note, $counter is still available, and is limited to the number of rounds > registered in DB.] > OR, have I been barking up the wrong tree altogether? > > I hope that made a bit of sense . > > PhilM > > |
|
|||
|
"Luciano Tolomei" <tolomei@newmediatrio.it> wrote in message news:8hzNc.100871$G%.77635@tornado.fastwebnet.it.. . > you could do simply as you are doing putting in html : name=round1heats .... > and then makin in the php code something like: > > for ($i=0;$i<$max_rows;$i++) { > if (isset($_POST["round".$i."heats"]) && $_POST["round".$i."heats"] != > "") { > //do something > else //do something else > } > > if you use register global ON this will work too: > ... > $name = "round".$i."heats"; > if (isset($$name) && $$name != "") { > ... > > > or you can use array in the form... > or a lot of other things. > but i suggest to you to spend some more time in it and build a good > object to work with your forms or look at something like this: > http://pear.php.net/package/HTML_QuickForm > > > bye > Luciano > > thanks for this. I almost started to look at variable variables. I suppose that is really where I was heading. regards, PhilM > PhilM wrote: > > > perhaps I am just a little tired, but I am having trouble with my form > > buttons. > > > > Firstly I name them like this > > name=\"round".$counter."heats\" > > which results in variables > > $round1heats > > $round2heats > > $round3heats > > etc... > > now, when I select button named ... "round1heats" ... I want to use the > > variable $round1heats to remove the button "round1heats", and display heats > > set for the round. To do that I assumed that I would have to:- > > IF(isset($round1heats)){ > > show other stuff instead etc > > }else{ > > generate and print html for button "round1heats" > > } > > > > My problem is, since all this is dynamic, since the number of heats is > > variable, and is dependant on DB entries, how do I generate the $round1heats > > used in the isset() so that it is recognised as a variable by the isset(). > > I have tried, to concat the bits as I have done in the naming of the button, > > but, unsurprisingly, it spits out errors ;) > > [note, $counter is still available, and is limited to the number of rounds > > registered in DB.] > > OR, have I been barking up the wrong tree altogether? > > > > I hope that made a bit of sense . > > > > PhilM > > > > |
|
|||
|
"PhilM" wrote:
> "Luciano Tolomei" <tolomei@newmediatrio.it> wrote in message > news:8hzNc.100871$G%.77635@tornado.fastwebnet.it.. . > > you could do simply as you are doing putting in html : > name=round1heats > .... > > and then makin in the php code something like: > > > > for ($i=0;$i<$max_rows;$i++) { > > if (isset($_POST["round".$i."heats"]) && > $_POST["round".$i."heats"] != > > "") { > > //do something > > else //do something else > > } > > > > if you use register global ON this will work too: > > ... > > $name = "round".$i."heats"; > > if (isset($$name) && $$name != "") { > > ... > > > > > > or you can use array in the form... > > or a lot of other things. > > but i suggest to you to spend some more time in it and build a > good > > object to work with your forms or look at something like this: > > http://pear.php.net/package/HTML_QuickForm > > > > > > bye > > Luciano > > > > > thanks for this. I almost started to look at variable variables. > I suppose that is really where I was heading. > regards, > PhilM > > > > > > PhilM wrote: > > > > > perhaps I am just a little tired, but I am having trouble > with my form > > > buttons. > > > > > > Firstly I name them like this > > > name=\"round".$counter."heats\" > > > which results in variables > > > $round1heats > > > $round2heats > > > $round3heats > > > etc... > > > now, when I select button named ... "round1heats" ... I want > to use the > > > variable $round1heats to remove the button "round1heats", > and display > heats > > > set for the round. To do that I assumed that I would have > to:- > > > IF(isset($round1heats)){ > > > show other stuff instead etc > > > }else{ > > > generate and print html for button "round1heats" > > > } > > > > > > My problem is, since all this is dynamic, since the number > of heats is > > > variable, and is dependant on DB entries, how do I generate > the > $round1heats > > > used in the isset() so that it is recognised as a variable > by the > isset(). > > > I have tried, to concat the bits as I have done in the > naming of the > button, > > > but, unsurprisingly, it spits out errors > > > [note, $counter is still available, and is limited to the > number of > rounds > > > registered in DB.] > > > OR, have I been barking up the wrong tree altogether? > > > > > > I hope that made a bit of sense . > > > > > > PhilM > > > > > ></font> Phil, register_globals is going away, and is a security threat. I would not turn it on. Use $_POST. Nel’s solution IMHO is the most elegant. No need to have many variable names or variable variables. Take a 2nd look at Nel’s. -- http://www.dbForumz.com/ This article was posted by author's request Articles individually checked for conformance to usenet standards Topic URL: http://www.dbForumz.com/PHP-form-but...ict133546.html Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=448738 |
|
|||
|
"PhilM" wrote:
> "Luciano Tolomei" <tolomei@newmediatrio.it> wrote in message > news:8hzNc.100871$G%.77635@tornado.fastwebnet.it.. . > > you could do simply as you are doing putting in html : > name=round1heats > .... > > and then makin in the php code something like: > > > > for ($i=0;$i<$max_rows;$i++) { > > if (isset($_POST["round".$i."heats"]) && > $_POST["round".$i."heats"] != > > "") { > > //do something > > else //do something else > > } > > > > if you use register global ON this will work too: > > ... > > $name = "round".$i."heats"; > > if (isset($$name) && $$name != "") { > > ... > > > > > > or you can use array in the form... > > or a lot of other things. > > but i suggest to you to spend some more time in it and build a > good > > object to work with your forms or look at something like this: > > http://pear.php.net/package/HTML_QuickForm > > > > > > bye > > Luciano > > > > > thanks for this. I almost started to look at variable variables. > I suppose that is really where I was heading. > regards, > PhilM > > > > > > PhilM wrote: > > > > > perhaps I am just a little tired, but I am having trouble > with my form > > > buttons. > > > > > > Firstly I name them like this > > > name=\"round".$counter."heats\" > > > which results in variables > > > $round1heats > > > $round2heats > > > $round3heats > > > etc... > > > now, when I select button named ... "round1heats" ... I want > to use the > > > variable $round1heats to remove the button "round1heats", > and display > heats > > > set for the round. To do that I assumed that I would have > to:- > > > IF(isset($round1heats)){ > > > show other stuff instead etc > > > }else{ > > > generate and print html for button "round1heats" > > > } > > > > > > My problem is, since all this is dynamic, since the number > of heats is > > > variable, and is dependant on DB entries, how do I generate > the > $round1heats > > > used in the isset() so that it is recognised as a variable > by the > isset(). > > > I have tried, to concat the bits as I have done in the > naming of the > button, > > > but, unsurprisingly, it spits out errors > > > [note, $counter is still available, and is limited to the > number of > rounds > > > registered in DB.] > > > OR, have I been barking up the wrong tree altogether? > > > > > > I hope that made a bit of sense . > > > > > > PhilM > > > > > ></font> Phil, register_globals is going away, and is a security threat. I would not turn it on. Use $_POST. Nel’s solution IMHO is the most elegant. No need to have many variable names or variable variables. Take a 2nd look at Nel’s. -- http://www.dbForumz.com/ This article was posted by author's request Articles individually checked for conformance to usenet standards Topic URL: http://www.dbForumz.com/PHP-form-but...ict133546.html Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=448738 |