Welcome to WebmasterWorld Guest from 174.129.96.175

Forum Moderators: coopster & jatar k

php and checkboxes

How do I use checkboxes and then check if the box has been ticked

   
1:15 am on Dec 1, 2002 (gmt 0)

10+ Year Member



Hey
There is loads of literature on checkboxes in php but I am having major difficulties in finding exactly the solution to my problem.
I want to allow people to choose from a selection of locations using checkboxes. Each location has an x coordinate and y coordinate. I want to work out the distance between each of the locations checked, by using the following function:

$x_distance = array ("0", "3", "1", "3");
$y_distance = array ("1", "5", "3", "3");

$distance = 0;
for($i=0; $i<3; $i++) {
$distance = sqrt((($x_distance[$i] - $x_distance[$i-1])*
($x_distance[$i] - $x_distance[$i-1])) +
(($y_distance[$i] - $y_distance[$i-1])*
($y_distance[$i] - $y_distance[$i-1])));
}

So my question is, how do i write a php script to allow people to check as many checkboxes they like (depending on what locations they are going to) and then to work out the total distance between them?!
Can anyone help me please?

PS at present I have just made 4 locations and put random numbers into the array, as you can see!

1:35 am on Dec 1, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] k8tie

I think if(isset($var)) works or you can just check if the value is set if($var == $value). Though I think the isset is what I use. I don't use checkboxes that often so I can't quite remember.

11:24 am on Dec 1, 2002 (gmt 0)

10+ Year Member



Sorry to be annoying, but I don't quite understand. Where about do I put that bit of code?
6:55 pm on Dec 1, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



well, it depends on where you do your calculations.

You have a form with checkboxes on one page, people make their selections/enter info and then click submit.

The form is then processed by posting to a script on another/the same page. I assume that is where that function you put in is. Before you do your calculations you will need to know what was selected.

so would have
if(isset($checkbox1)) calculate();
if(isset($checkbox2)) calculate();
if(isset($checkbox3)) calculate();
if(isset($checkbox4)) calculate();

That is sloppy but does it help?

<added>by the way, it's not annoying :), if you don't understand then maybe you can give me a little more info on how you process the form and the logical flow of the setup right now.

11:14 pm on Dec 1, 2002 (gmt 0)

10+ Year Member



Hey

So far I have the checkbox.php page

<HTML>
<HEAD>
<TITLE> Make your choice </TITLE>
</HEAD>

<BODY>
<FORM action="distance.php" method="post">
<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=1>Location 1<br>
<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=2>Location 2<br>
<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=3>3<br>
<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=4>4<br>
<INPUT TYPE = "submit" value="Submit">
</FORM>

</BODY>
</HTML>

Then the distance.php is the following code

<?

$LOCATION_ARRAY = $_POST['LOCATION_ARRAY'];

$x_distance = array ("0", "3", "1", "3");
$y_distance = array ("1", "5", "3", "3");

for ($i=0; $i<sizeof($LOCATION_ARRAY); $i++)
{
if (isset($LOCATION_ARRAY[$i]))
{
$distance = 0;
for($i=0; $i<3; $i++) {
$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*
($x_distance[$i] - $x_distance[$i-1])) +
(($y_distance[$i] - $y_distance[$i-1])*
($y_distance[$i] - $y_distance[$i-1])));
}
echo ("$distance");
}
}
?>

The point should be to only work out the distances between the locations that are checked, but at the moment it still works out every location in the array!- So there is something wrong with my code! Except I am not sure what!

Thanks lots for your help :)

12:17 am on Dec 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



problem is you are setting the array every time, what you need to do is build the array based on what they have checked. I took out extra code so only have left what I changed. This is still going to be messy I have some lovely alternatives but want to keep it simple. :)

This is all assuming that based on these values
$x_distance = array ("0", "3", "1", "3");
$y_distance = array ("1", "5", "3", "3");

location1 = 0,1
location2 = 3,5
location3 = 1,3
location4 = 3,3

checkbox.php page

<INPUT TYPE = "checkbox" NAME="loc1" value=1>Location 1<br>
<INPUT TYPE = "checkbox" NAME="loc2" value=2>Location 2<br>
<INPUT TYPE = "checkbox" NAME="loc3" value=3>Location 3<br>
<INPUT TYPE = "checkbox" NAME="loc4" value=4>Location 4<br>

Then the distance.php

$count = 0;
if (isset($_POST['loc1'])) {
$x_distance[$count] = 0;
$y_distance[$count] = 1;
$count++;
}
if (isset($_POST['loc2'])) {
$x_distance[$count] = 3;
$y_distance[$count] = 5;
$count++;
}
if (isset($_POST['loc3'])) {
$x_distance[$count] = 1;
$y_distance[$count] = 3;
$count++;
}
if (isset($_POST['loc4'])) {
$x_distance[$count] = 3;
$y_distance[$count] = 3;
$count++;
}

that adds the location of each point to the array only if they have checked it. I didn't test any of this so it might not be quite right but the premise is sound.

1:31 am on Dec 2, 2002 (gmt 0)

10+ Year Member



Hey, Thanks for the help but that doesn't seem to work! When I try running it through the loop to work out the distance it comes up with the wrong answer. I can't work out why this!
Sorry!
2:25 am on Dec 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I looked at the loop and couldn't figure it out quite what it was doing.

The thing I couldn't figure was exactly what the array was.

for ($i=0; $i<sizeof($LOCATION_ARRAY); $i++)
{
if (isset($LOCATION_ARRAY[$i]))
{
$distance = 0;
for($i=0; $i<3; $i++) {
$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*
($x_distance[$i] - $x_distance[$i-1])) +
(($y_distance[$i] - $y_distance[$i-1])*
($y_distance[$i] - $y_distance[$i-1])));
}
echo ("$distance");
}
}

you can take the isset if statement out of here and have to change the for statement, I was looking at taking away one of the for's and using the comparison as $i<sizeof($x_distance). Not totally sure though.

<added>oh, and quit apologizing ;)

2:50 am on Dec 2, 2002 (gmt 0)

10+ Year Member



Basically, the loop works out the distance between two points. It does this by giving each point a coordinate. Then by using pythagorus it works out the distance between them as with the coordinates you can work out two sides and pythagorus works out the remaining side which is the distance! Does this make sense!

I am unsure about your code as I do not have a LOCATION_ARRAY so it will surely not bring anything up?

Cheers :)

2:55 am on Dec 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



my bit just adds the coordinates of the checked location to the x and y arrays, if it isn't checked it isn't there.

Which means that if you use $i<sizeof($x_distance) to figure out how many coordinates are in there it should work. One question, is there a starting point of some kind? A point that should always be there?

What happens if they only selected one point?

<added>I would turn the above function into something like this

$distance = 0;
for ($i=0; $i<sizeof($x_distance); $i++)
{
$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*
($x_distance[$i] - $x_distance[$i-1])) +
(($y_distance[$i] - $y_distance[$i-1])*
($y_distance[$i] - $y_distance[$i-1])));
echo ("$distance");
}

3:00 am on Dec 2, 2002 (gmt 0)

10+ Year Member



I hadn't even thought about what would happen if there is only one point. I was just trying to get the other stuff working and then worry about that!
I am still unsure which bit of code you think that I should be putting in! Is it possible for you to paste it all together because we have talked about so much stuff I think I may be confusing bits which is why it isn't working!
At the moment I have nothing coming up- I think it is because the array LOCATION_ARRAY doesnt have anything in it
3:02 am on Dec 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



did you look at what I added to the last message?

I stickied the full version to you since it is a little long.

[edited by: jatar_k at 3:06 am (utc) on Dec. 2, 2002]

3:06 am on Dec 2, 2002 (gmt 0)

10+ Year Member



Yeah. Sorry, I didn't scroll far enough. I have tried it all again but still coming out with wrong answer! This is what i have now

<?
$count = 0;
if (isset($_POST['loc1'])) {
$x_distance[$count] = 0;
$y_distance[$count] = 1;
$count++;
}
if (isset($_POST['loc2'])) {
$x_distance[$count] = 3;
$y_distance[$count] = 5;
$count++;
}
if (isset($_POST['loc3'])) {
$x_distance[$count] = 1;
$y_distance[$count] = 3;
$count++;
}
if (isset($_POST['loc4'])) {
$x_distance[$count] = 3;
$y_distance[$count] = 3;
$count++;
}

for ($i=0; $i<3; $i++)
{
if (isset($x_distance[$i]))
{
$distance = 0;
for($i=0; $i<sizeof($x_distance); $i++) {
$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*
($x_distance[$i] - $x_distance[$i-1])) +
(($y_distance[$i] - $y_distance[$i-1])*
($y_distance[$i] - $y_distance[$i-1])));
}
echo ("$distance");
}
}
?>

3:08 am on Dec 2, 2002 (gmt 0)

10+ Year Member



Ah sorry- didn't see the sticky. Am checking it now
3:12 am on Dec 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



the last function is the problem, you have two for loops and an if.

the if should go.

why the two for loops?

3:17 am on Dec 2, 2002 (gmt 0)

10+ Year Member



Yeah, I saw that as I pasted it-I think it was because I was trying so many different things that I had added it by accident. I have now just tried the exact code you sent me but it still comes out with the wrong answer! This is mad!
3:24 am on Dec 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



what you have to do is understand what the first part is doing and then apply your loop to that.

The first part builds your two x and y arrays depending on what is checked. Therefore the loop has to work no matter how much data is in the arrays. You are having logic problems.

Your equation is right and I think the arrays are being built properly but you need to adapt the logic you were using in the first place to a dynamicly constructed array.

You have to have it understand any possible combination how many values and what they are.

3:28 am on Dec 2, 2002 (gmt 0)

10+ Year Member



Yeah you are right- I think it's too late at night to be thinking logically. I think I will continue puzzling over this tomorrow! Thanks
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month