Testing checkbox settings
Moderator: General Moderators
Testing checkbox settings
Struggling with checkboxes. Here's a cutdown version of a form, situated in a table row with three columns:
<tr><form method="post" action="<?php echo $PHP_SELF;?>">
<td><a href="http://www......./index.php?marc=<?php echo $_POST['leagan'];?>&ceart=<?php echo $_POST['leagan'];"?>Text in column one</a></td>
<td>
<input type="checkbox" value="Myvalue1" name="leagan[]"> box one<br />
<input type="checkbox" value="Myvalue2" name="leagan[]"> box two<br /></td>
<td align="center"><img src="anuas4.jpg" width="89" height="33" alt="" onclick="alert(<?php echo $_POST['leagan'].' '.$_POST['leagan'];?>)" /></td>
</form></tr>
There are two checkboxes in the second column. I can see them, and I can check and uncheck them. But I don't know how to detect whether they are checked or not. I make an attempt to detect this in column 1, and what was supposed to be a simpler attempt in column three, but neither works.
In column 1, I am trying to detect the settings and use them to set parameters to index.php The href is followed and index.php is run, but the checkbox settings have no effect.
In column 3, I am trying only to display the settings in response to clicking on a button. No alert happens on clicking the button.
My questions are how do I detect checkbox settings? But also, what values should I expect to detect for a box which is checked, and for a box which is unchecked, and for a box which has not been displayed at all.
Thanks for any help.
<tr><form method="post" action="<?php echo $PHP_SELF;?>">
<td><a href="http://www......./index.php?marc=<?php echo $_POST['leagan'];?>&ceart=<?php echo $_POST['leagan'];"?>Text in column one</a></td>
<td>
<input type="checkbox" value="Myvalue1" name="leagan[]"> box one<br />
<input type="checkbox" value="Myvalue2" name="leagan[]"> box two<br /></td>
<td align="center"><img src="anuas4.jpg" width="89" height="33" alt="" onclick="alert(<?php echo $_POST['leagan'].' '.$_POST['leagan'];?>)" /></td>
</form></tr>
There are two checkboxes in the second column. I can see them, and I can check and uncheck them. But I don't know how to detect whether they are checked or not. I make an attempt to detect this in column 1, and what was supposed to be a simpler attempt in column three, but neither works.
In column 1, I am trying to detect the settings and use them to set parameters to index.php The href is followed and index.php is run, but the checkbox settings have no effect.
In column 3, I am trying only to display the settings in response to clicking on a button. No alert happens on clicking the button.
My questions are how do I detect checkbox settings? But also, what values should I expect to detect for a box which is checked, and for a box which is unchecked, and for a box which has not been displayed at all.
Thanks for any help.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
First, please surround your code with [ syntax=php][/syntax] tags when posting here. Second, you have several problems with your code. Here it is fixed (syntax-wise):
The formatting on the closing tag on the second bit of PHP was messed up, the values being placed in the link URL were not encoded, and the text being placed in the alert() method was not being quoted (fixed with json_encode()). Try this code now, then tell us what's wrong.
Code: Select all
<tr><form method="post" action="<?php echo $PHP_SELF;?>">
<td><a href="http://www......./index.php?marc=<?php echo urlencode($_POST['leagan']); ?>&ceart=<?php echo urlencode($_POST['leagan']); ?>">Text in column one</a></td>
<td>
<input type="checkbox" value="Myvalue1" name="leagan[]"> box one
<input type="checkbox" value="Myvalue2" name="leagan[]"> box two
</td>
<td align="center"><img src="anuas4.jpg" width="89" height="33" alt="" onclick="alert(<?php echo json_encode($_POST['leagan'].' '.$_POST['leagan']); ?>);" /></td>
</form></tr>Re: Testing checkbox settings
Many thanks for your help. I think I've copied it as you wrote it. Here's what I've got now.
There's still no alert when I click the image in column 3 however. If I could get the alert, or find some other way of displaying the values,I think I could see the way ahead. I hope the answer (or part of it) is not to add a submit button and click it — clicking a checkbox changes its appearance and hopefully the appearance is testable.
Code: Select all
<tr><form method="post" action="<?php echo $PHP_SELF; ?>">
<td><a href="http://www...filter.php?marc=<?php echo urlencode($_POST['leagan2']); ?>&ceart=<?php echo urlencode($_POST['leagan1']);" ?>Text in column 1</a></td>
<td>
<input type="checkbox" value="2" name="leagan1"> box one<br />
<input type="checkbox" value="2" name="leagan2"> box two<br /></td>
<td align="center"><img src="anuas4.jpg" width="89" height="33" alt="" onclick="alert(<?php echo json_encode($_POST['leagan1'].' '.$_POST['leagan2']); ?>);" /></td>
</form></tr>- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
I don't see a submit button in your form. Do you submit it at any point?
Re: Testing checkbox settings
Many thanks for replying.
I may need to submit the form later but at this stage I want to be able to check some things about its appearance and possibly change that appearance.
The two checkboxes for example are not independent: whenever box 1 is ticked, box 2 does not apply. So I'd like to redisplay without box 2 in reaction to the user ticking box 1.
To do this, two things about a form that I need to be able to check are whether or not a named box is ticked; and whether or not a named box is displayed at all.
This wouldn't be a problem in another programming language, but maybe php has a different way of thinking, though I hope not.
giomach
I may need to submit the form later but at this stage I want to be able to check some things about its appearance and possibly change that appearance.
The two checkboxes for example are not independent: whenever box 1 is ticked, box 2 does not apply. So I'd like to redisplay without box 2 in reaction to the user ticking box 1.
To do this, two things about a form that I need to be able to check are whether or not a named box is ticked; and whether or not a named box is displayed at all.
This wouldn't be a problem in another programming language, but maybe php has a different way of thinking, though I hope not.
giomach
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
It sounds like you don't understand the difference between a client-side language (e.g. Javascript) and a server-side language (e.g. PHP). Read these:
http://en.wikipedia.org/wiki/Server-side_scripting
http://en.wikipedia.org/wiki/Client-side_scripting
You are trying to merge the two in a way that's not possible. If I understand you what you're saying, you need to do it with Javascript.
http://en.wikipedia.org/wiki/Server-side_scripting
http://en.wikipedia.org/wiki/Client-side_scripting
You are trying to merge the two in a way that's not possible. If I understand you what you're saying, you need to do it with Javascript.
Re: Testing checkbox settings
You might have an easier time using jquery, and possibly ajax to pass to and from the server scriptgiomach wrote:Many thanks for your help. I think I've copied it as you wrote it. Here's what I've got now.
There's still no alert when I click the image in column 3 however. If I could get the alert, or find some other way of displaying the values,I think I could see the way ahead. I hope the answer (or part of it) is not to add a submit button and click it — clicking a checkbox changes its appearance and hopefully the appearance is testable.Code: Select all
<tr><form method="post" action="<?php echo $PHP_SELF; ?>"> <td><a href="http://www...filter.php?marc=<?php echo urlencode($_POST['leagan2']); ?>&ceart=<?php echo urlencode($_POST['leagan1']);" ?>Text in column 1</a></td> <td> <input type="checkbox" value="2" name="leagan1"> box one<br /> <input type="checkbox" value="2" name="leagan2"> box two<br /></td> <td align="center"><img src="anuas4.jpg" width="89" height="33" alt="" onclick="alert(<?php echo json_encode($_POST['leagan1'].' '.$_POST['leagan2']); ?>);" /></td> </form></tr>
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
@Squegg: if he's just displaying the values of the checkboxes, AJAX is not necessary.
Re: Testing checkbox settings
Excellent advice, thank you, to look at the client side. I've got the checkboxes enabling and disabling fine now with javascript. But my server side problems remain, as I'm obviously missing something basic about how to handle the checkbox values. Please bear with me one more time.
Here's the table again, reduced to one row with three columns:
Column 2 has the two checkboxes (leagan1, leagan2), which the user should tick or not, and then I try to move on, trying out two alternative ideas in columns 1 and 3.
In column 1, I have an <a> tag. If clicked, control passes to the url on the href, but unfortunately the checkbox values have not been inserted as intended, but the two bits of php each come up as empty. Maybe this is what must happen without a submit button — if so, please tell me and I can stop trying to make it work!
In column 3, I have an <img> tag with an onclick= handler, intended to issue an alert to tell me the values of the checkboxes (though I'm fairly sure by now their value is empty). Clicking the image however does not produce an alert or anything else. Though puzzling, this is only a peripheral problem to me.
Also I have an alternative content for column 3: a submit button!
Clicking this causes control to pass to the form's action url viz where I find I can access the values of the checkboxes leagan1 and leagan2 and construct the url I really want to go to, the one you can see in column 1 above but with the right values of leagan1 and leagan2 now rather than empty. I can construct the target url in a string but... how do I go straight there, without making the user click any more buttons?
Thanks once again, Giomach
Here's the table again, reduced to one row with three columns:
Code: Select all
<tr><form method="post" action="http://www.../checkboxform.php">
<td><a href="http://www.../filter.php?marc=<?php echo urlencode($_POST['leagan2']) ?>&ceart=<?php echo urlencode($_POST['leagan1']) ?>">Text in column one</a></td>
<td><input type="checkbox" value="2" name="leagan1"> box one<br />
<input type="checkbox" value="2" name="leagan2"> box two<br />
</td>
<td align="center"><img src="anuas4.jpg" width="89" height="33" alt="" onclick="alert(<?php echo 'Oh '.json_encode($_POST['leagan1'].' '.$_POST['leagan2']); ?>);" /></td>
</form></tr>
In column 1, I have an <a> tag. If clicked, control passes to the url on the href, but unfortunately the checkbox values have not been inserted as intended, but the two bits of php each come up as empty. Maybe this is what must happen without a submit button — if so, please tell me and I can stop trying to make it work!
In column 3, I have an <img> tag with an onclick= handler, intended to issue an alert to tell me the values of the checkboxes (though I'm fairly sure by now their value is empty). Clicking the image however does not produce an alert or anything else. Though puzzling, this is only a peripheral problem to me.
Also I have an alternative content for column 3: a submit button!
Code: Select all
<td align="center"><input type="submit" name="formSubmit" value="Anuas" /></td>
Code: Select all
checkboxform.phpCode: Select all
filter.php?marc=VALUE_OF_LEAGAN2&ceart=VALUE_OF_LEAGAN1Thanks once again, Giomach
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
Set the action of the form to the path to filter.php, and set the method as "get". Rename the "leagan2" checkbox to "marc", and rename the "leagan1" checkbox to "ceart". Try it, and post it here again if it doesn't work.
Re: Testing checkbox settings
Not sure if I understand exactly what you're suggesting, but that seems to leave the browser looking at the subdirectory containing filter.php.
I found another way, by having checkboxform do a
after setting $ceartaithe and $marcailte from the values of $_POST['leagan1'] and $_POST['leagan2'] resp.
I'm still interested in alternatives, if you like to explain the thinking behind yours again, otherwise this subject might possibly be considered closed. In that case, many thanks again.
I found another way, by having checkboxform do a
Code: Select all
header('Location:http://www.../ceart='.$ceartaithe.'&marc='.$marcailte);I'm still interested in alternatives, if you like to explain the thinking behind yours again, otherwise this subject might possibly be considered closed. In that case, many thanks again.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
Did you follow the instructions? You don't need a redirect here. Post what you tried.
Re: Testing checkbox settings
Here's what I tried:
Clicking the submit button just shows the directory listing of http://www.../phptest (where filter.php is one of the files).
I've also tried adding "/filter.php" to the "action" parameter but the result is the same.
Code: Select all
<tr><form method="get" action="http://www.../phptest">
<td>Text in column one</td>
<td><input type="checkbox" value="2" name="ceart"> box one<br />
<input type="checkbox" value="2" name="marc"> box two<br />
</td>
<td align="center"><input type="submit" name="formSubmit" value="Anuas" /></td>
</form></tr>I've also tried adding "/filter.php" to the "action" parameter but the result is the same.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Testing checkbox settings
Sorry I wasn't clear, I mean actually point it to filter.php (http://www.../phptest/filter.php). By the way, unless you're pointing to another domain, I suggest you leave out the http://www.../, and just start with /. That way the site will still work if you transfer it to another domain, or test it on localhost.
Re: Testing checkbox settings
Thanks again, but even with
the result is the same, in the browser I just see the directory of phptest. I don't understand that at all. The script filter.php is not entered. I've tried both IE and Firefox.
The url displayed at the top of the browser window is
http://www.../phptest/filter.php?formSubmit=Anuas
Code: Select all
<tr><form method="get" action="http://www.../phptest/filter.php">The url displayed at the top of the browser window is
http://www.../phptest/filter.php?formSubmit=Anuas