Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Passing checkbox status to a report page
Old 09-05-2007, 07:13 AM Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Hi

I have a questionnaire that contains mainly checkboxes (and a few radio buttons).

Each checkbox has a generic name (name=choice) and a unique ID.

I wish to pass the ticked status of each checkbox to a report page, which will echo some text accordingly:
PHP Code:
if (array_key_exists('uniqueID',$_POST)) { 
echo 
"some text"
This does not work, unless I switch id and name around for the checkbox. Ie
name=uniqueID and id=genericnameforall checkboxes.

This would be fine if unconventional. Except that the radio buttons need to have the same name for the whole set.

So, why can't my report page pick up on a unique id when it is set as id=uniqueID, but it can when it is set as name=uniqueID ?? And is there any way round this?

I have also tried on the report page
PHP Code:
if (isset($uniqueID)) { echo "some text" 
and that works identically to the other code.

Thanks for any wisdom

Tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
 
Register now for full access!
Old 09-05-2007, 09:23 AM Re: Passing checkbox status to a report page
Ultra Talker

Posts: 483
Trades: 0
Basically because that's how HTML works

'name' refers to the fields that are sent on a form submission. 'id' refers to particular elements of HTML that sometimes just happen to be part of a form. Essentially 'id' is one way of accessing areas of HTML from a client perspective. 'name' is used in the form submission.

I don't know how to say it any easier, sorry

EDIT: I just wanted to add... because this is how it works, it's definitely not 'unconventional'. Also, because 'name' and 'id' are totally unrelated, there's nothing stopping you setting the name and the id both to the same uniqueID for each checkbox (though the only reason you would want unique IDs on each item would be for accessing it via JS or if you wanted each one styled differently, really).
__________________

Please login or register to view this content. Registration is FREE

Last edited by TwistMyArm; 09-05-2007 at 09:25 AM..
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 11:01 AM Re: Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Great, thanks for your advice. I guess it leaves me trying to do two incompatical things with radio buttons, so I'll have to work round that!

Best wishes

tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
Old 09-05-2007, 11:26 AM Re: Passing checkbox status to a report page
Ultra Talker

Posts: 483
Trades: 0
I guess so... I don't really know what you're trying to do so I'll have to take your word for it (or, if you can tell us what you mean by trying to do two incompatible things with radio buttons, maybe we can help...).
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-07-2007, 07:52 AM Re: Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Thanks so much. I am trying to pass to a report page using a form, whether checkboxes and radio buttons are ticked or not. Each checkbox and radio button also has a numerical value and inevitably this is also being passed the report page, though it is not needed there on an indivdidual basis:
http://ied.gospelcom.net/church-site-designnew.php

Each checkbox and radio button has a name and an ID. But the report form will only echo whether a checkbox has been ticked if the name is the unique ID and the ID is a generic name!! (Someone has wondered if a different doctype would allow an id=uniqueID info to be passed.)

The report form picks up the status of eg checkbox with name="appearance"
PHP Code:
if (array_key_exists('appearance',$_POST)) { 
echo 
"appearance ticked"
You will see on the report page that there is a temporary visible display at the top of what is being passed. Since the numerical value of each checkbox and radio button IS being passed to the report page, I am guessing that I can achieve what I want to with a slightly different PHP logic thus:

if the value of checkbox named 'uniqueID' is 20,
do something

Since I know that if is ticked, its preset value is 20, the result of that PHP argument is equivalent to confirming that it is ticked.

So I am wondering wht the PHP would be for that if statement?

Best wishes and thanks


Tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
Old 09-07-2007, 08:14 AM Re: Passing checkbox status to a report page
Ultra Talker

Posts: 483
Trades: 0
You don't even need to have it set to a separate value, really. On checkboxes, using:
if ( isset( $_POST[ 'uniqueID' ] ) ) {

would be sufficient.

Depending on exactly what you're trying to do, it may pay to have your 'unique IDs' to actually be something like 'checkbox[1]', 'checkbox[2]' etc. That way, in PHP you can use the $checkbox variable as an array. That is to say you could then use:
$checkbox = $_POST[ 'checkbox' ];
if ( isset( $checkbox[1] ) ) {

or even better would be to have your checkboxes all named that way and give each of them a value of '1'. That way (depending on your error level settings) you would just be able to do:
$checkbox = $_POST[ 'checkbox' ];
if ( $checkbox[1] ) {

Note, too, that you don't have to have numbers as the index. You could use field names like 'checkbox["UserHasACar"]', 'checkbox["userIsMale"]' etc. (I believe!).

For what it's worth, I don't see any way of moving variables based on their ID (as opposed to the field name) as that's just not how it works

Anyway, hopefully that will give you some ideas. Apologies if it doesn't: I'm only on my third coffee
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-07-2007, 11:15 AM Re: Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Thanks so much for further thoughts. I have experimented some more, but the bottom line is - the form cannot pass a unique ID if it is id=uniqueID. It can pass numerical values from the form, when selected. But it does not tie them in to any ID. It merely numbers them in a list, if they are selected. So, if say someone only selected checkbox 12 on the list, that would appear in the information being transmitted as number zero.

The only way I can so far tie a specific status to a specific ID is to use name=uniqueID

So unless there is some way of making it recognise an ID=uniqueID in preference to name=uniqueID, I'll just have to work with that, and do some sort of workaround with the radio buttons.

thanks for any more thoughts!

Tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
Old 09-07-2007, 01:02 PM Re: Passing checkbox status to a report page
Ultra Talker

Posts: 483
Trades: 0
I'm still really confused as to what exactly you are talking about. What is the problem with radio buttons that require a 'workaround'?

You do know that you can set a 'value' to each checkbox and each radio button, right?

So something like:
<input type="checkbox" name="the_checkbox" value="4" />

will (if clicked) make $_POST[ 'the_checkbox' ] = '4'.

For radio buttons, if you have:
<input type="radio" name="the_radio" value="1" />
<input type="radio" name="the_radio" value="2" />
<input type="radio" name="the_radio" value="3" />
<input type="radio" name="the_radio" value="4" />

and you select the third radio button, the PHP code will see $_POST[ 'the_radio' ] as being equal to '3'.

I'm sorry if I'm misunderstanding the problem, but like I say, I don't understand what you are trying to workaround in the first place...
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-07-2007, 01:39 PM Re: Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Sorry to be confusing!

The situation is:
yes, each check box and radio button does have a numerical value. However, I am not needing to pass that value to the report page on an individual basis! (It gets added up on the first page by a javascript instead.) All I want to pass to the report page is whether someone has checked each checkbox or radio button. So, if checkbox number2 has been ticked, a certain comment will be printed on the report form. If it is not ticked, a different comment is printed.

So report.php will receive this information (ie ticked or not) only if I assign its unique ID to name=uniqueID.

Hope that makes sense!!

thanks

tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
Old 09-07-2007, 02:59 PM Re: Passing checkbox status to a report page
Ultra Talker

Posts: 483
Trades: 0
OK. I understand that... but you're saying you have to workaround this issue with radio buttons.

What exactly do you have to workaround? I can see that you want to know exactly which radio button is selected (out of the group) BUT what I can't see is how you think having the ID of that radio button would help, when having the VALUE of that radio button would not.

Hopefully you can see what I'm getting at. You say you have a problem and imply that it would work if you could just pass along the uniqueID assigned to the 'ID' of the radio button: how would that solve your issue but the VALUE would not?
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-08-2007, 05:21 AM Re: Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Hi, thanks. I'm finally understanding a bit more about arrays now, and realise that my form IS passing the information I need to the report page. By displaying it visibly on my report form using print_r($_POST) I now find that if for example checkbox 2 within the array 'choice' has been selected and has therefore passed its preset numerical value of 6, I get this:

Array
(
[total] => 0
[choice] => Array
(
[2] => 6
)
I am hoping therefore that there will be some PHP that says:

"In array 'choice', if item [2] value is 6, write something"

Any idea how I can write that?

Mega thanks

tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
Old 09-08-2007, 06:45 AM Re: Passing checkbox status to a report page
Ultra Talker

Posts: 483
Trades: 0
If that's the output of print_r(_$POST), you should be able to use:

if ( $_POST[ 'choice' ][ 2 ] == 6 ) {
echo 'something';
}
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-08-2007, 06:58 AM Re: Passing checkbox status to a report page
Skilled Talker

Posts: 81
Trades: 0
Wonderful. Works great. Thanks so much for helping me on this!!

tony
soon is offline
Reply With Quote
View Public Profile Visit soon's homepage!
 
Reply     « Reply to Passing checkbox status to a report page
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.43454 seconds with 12 queries