check box IF checked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wilko18
Forum Newbie
Posts: 4
Joined: Fri Apr 21, 2006 6:37 am

check box IF checked

Post by wilko18 »

Anyone know why this really basic IF loop doesn't work?

I just need the code to chenge the value of the variable IF the box is ticked...which it is by default, however the values output is "Not Working" and not "it works".

Code: Select all

<input name="tick1" type="checkbox" id="tick1" value="checkbox" checked="checked" />

<?php

 
	
	$price = "not working";	
	if (isset($_POST['tick1']))
	{
	$price = "it works";
	}
	echo $price;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is the form the checkbox apart of use the post method?
wilko18
Forum Newbie
Posts: 4
Joined: Fri Apr 21, 2006 6:37 am

Post by wilko18 »

I don't quite understand what you mean? :?

The code I posted is the whole code...we're just trying to get the

if (isset($_POST['tick1']))

to return true...which currently it never does.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have to have a form to submit via post (ignoring Ajax.)
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

page1.php:

Code: Select all

<form action="page2.php" method="POST">
<input name="tick1" type="checkbox" id="tick1" value="checkbox" checked="checked" />
<input name="submit" type="submit" value="Test!" />
</form>
page2.php:

Code: Select all

<?php
$price = "not working";   
if ($_POST['tick1']) {
 $price = "it works";
}
echo $price;
?>
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

shouldn't

<input name="tick1" type="checkbox" id="tick1" value="checkbox" checked="checked" /

be checked='true"?

im not an html master. thats why i use an html editor.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

i dont think it should be either :)

I'm pretty sure it should be:

Code: Select all

<input type="checkbox" name="NAME" checked />
My bad for not noticing (correct me if im wrong... but im damn sure this is right)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What it should be depends on what (X)HTML standard you are following. In HTML 4, just "checked," but in XHTML, 'checked="checked".'
Post Reply