...

View Full Version : Checkbox Checked



stophon4
05-06-2004, 12:14 PM
I cannot seem to get the value for an if statement if a checkbox is checked,
I have tried:


if($_POST['allowbb'] == 'checked')

and


if($_POST['allowbb']) //a "true" value

and


if($_POST['allowbb'] == 'on') //someone suggested it in the forum

except none of them seem to work, I know this seems like a really easy thing to do, but for some reason I cant get it to work :eek:

All help is apreciated :thumbsup:

missing-score
05-06-2004, 12:55 PM
Try this:



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="check">Check</label>
<input type="checkbox" value="1" name="check" />
</form>


Then to recieve:



if(isset($_POST['check'])){
// do whatever
}


If the checkbox is not checked the value will not be set.

firepages
05-06-2004, 03:57 PM
as per missing-score's post though


<?
if(!empty($_POST['check'])){ }
?>

would probably be better

stophon4
05-06-2004, 08:06 PM
Thanks :) It works great :o

And I have a question, what does the <label> tag do?
and also
Is it possible to get the contents of a div with php?

sad69
05-06-2004, 08:36 PM
The label tag associates some text with an element by the element's id. Typically this is used for radio buttons and checkboxes, so that clicking the label will also check the radio or checkbox..

Here's an example of that:
http://www.htmlcodetutorial.com/forms/_LABEL.html


As for PHP getting the contents of a div... I'm not sure how you mean. Do you mean getting the contents of a div of the previous page (like a page gets submitted to PHP, so you're hoping to the view the contents of a div on that page)? or do you mean a div on the page you're currently constructing with PHP?

In the first case, it's not possible unless there were form elements in that div so that you posted those form elements to the PHP page. The form has to know what's part of the form and what's part of the HTML, so it knows what stuff to post. If you're displaying a total or something in that div, you should put the total in a hidden form input with a unique name as well, so that it will get posted to PHP and you can grab its value by referencing its name.

In the second case, if you store the contents of div in a string variable, then you can reference the contents through that string variable, and when it comes time for the div, you can just echo the string variable there.

It's maybe a bit hard to explain, but if I haven't answered your question, maybe you'd like to clarify which case you're referring to (or if there's another case I haven't thought of..) and also explain a bit about why you'd like to get the content of the div through PHP, and maybe we can present a better solution.

Hope that helps,
Sadiq.

sidney
05-06-2004, 09:04 PM
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="check">Check</label>
<input type="checkbox" value="1" name="check" />
</form>


i would just like to add that label will not work in above code as it needs the id of input field to be set to check



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="check">Check
<input type="checkbox" value="1" name="check" id="check" /></label>
</form>

missing-score
05-06-2004, 09:08 PM
of course :o , my mistake :rolleyes:

aspdude2004
05-06-2004, 10:48 PM
http://www.htmlcodetutorial.com/forms/_LABEL_FOR.html

That is a better example of <label> in this case as label cannot work without the 'for="????"' ;)

stophon4
05-06-2004, 11:30 PM
I am trying to make a user-freindly edit function for my guestbook, so I want to get the div of ONE line only, I want to refer to the line first, (so it searches the line for the div) and then I want it to record a div into a variable, and somehow I am going to need it to clear the line, write back in the other stuff, write in the improved div, and then write the rest of the line, I know who to do alot of this, but the recording into the div part I am clueless :p

stophon4
05-09-2004, 01:12 PM
I am trying to make a user-freindly edit function for my guestbook, so I want to get the div of ONE line only, I want to refer to the line first, (so it searches the line for the div) and then I want it to record a div into a variable, and somehow I am going to need it to clear the line, write back in the other stuff, write in the improved div, and then write the rest of the line, I know who to do alot of this, but the recording into the div part I am clueless :p

So does anybody know the answer? Or at least an idea on what to do?

sidney
05-09-2004, 01:45 PM
i think people are unsure what your after could you explain more and may be some examples of what you need to grab

stophon4
05-09-2004, 02:11 PM
OK, I got some of it done, but it doesn't work at all, but here are the parts of the source that due my edit function (that doesn't work):


else if(isset($_POST['edit0']))
{
if($authorized)
{
$post = $_POST['edit0-post'];
$Name = $_POST['edit0-name'];
echo ("<form action='gb.php' method='post'><b>Name:</b><INPUT type='text' Name='edit-name' Value='$Name'><b>POST:</b><INPUT type='text' Name='edit-post' Value='$post'><INPUT type='Submit' Name='edit-posted0'></form>");
}
else if(!$authorized)
{
echo ("<H1><b><font size='7' color'red'>You Do Not Have Permission To Access This Function</font></b></H1><br>");
}
}

That takes information from the edit function, also the Name and Post will be hidden values on the form made when the post is posted


Now this does the actual updating:


else if(isset($_POST['edit-posted0']))
{
$post0 = '1';
$lines = count(file("guestbook.php"));
$array = file("guestbook.php");
$Name = $_POST['edit-name'];
$Post = $_POST['edit-post'];
$file = fopen("guestbook.php", "w");
fputs($file, '<table class="post" Name=$_POST["type"] border=0><tr><td>');
fputs($file, $_POST['edit-name']);
fputs($file,'</td><td align="right">');
fputs($file, $time);
fputs($file, '</td></tr></table><table class="post" Name=$_POST["type"] border=0><tr><td>');
fputs($file, '<center>');
fputs($file, $_POST['edit-post']);
fputs($file, '<br>');
fputs($file, '<font color="CDCDCD" size=2>');
fputs($file, $IP);
fputs($file, '</font><hr>');
fputs($file, "<?php ");
fputs($file, '$IPP = getenv (REMOTE_ADDR);');
fputs($file, "\$admin = 'authorized.dat';\$IP = getenv (REMOTE_ADDR);if (!file_exists (\$admin)) { die('Error: \$admin does not exist.');}\$h = fopen (\$admin, 'r');while (!feof (\$h)) { \$line = fgets (\$h, 4096); \$line = trim (\$line); if (\$line == \$IP){ \$authorized=true;}else{ \$authroized=false; }}fclose(\$h);");
fputs($file, "if(\$authorized){ ");
fputs($file, "echo ('<center><form style=float:left action=gb.php method=post><input type=Submit Name=delete$lines Value=Delete></form></center>'); }");
fputs($file, "?>");
fputs($file, '<br><hr></td></tr></table>');
fputs($file, $newline);
if($lines < '2') // an attempt to put every line after it as the array line
{
fputs($file, $array[1]);
}
else if($lines < '3') // puts post 2 after it, should use the "for" function
{
fputs($file, $array[2]);
}
fclose($file);
}


So that is all I got and it doesn't work at all :o

sidney
05-09-2004, 02:36 PM
if you are trying to give people the ability to edit there posts in your guestbook would it not be simpler to edit the file where posts are being stored

it looks to me you are trying to edit the script itself

stophon4
05-09-2004, 03:11 PM
That would require the user to use html, anyway I made a improvement after spending about 20 minutes working on it the edit function now works fine, the only problem is getting the post back into the file without screwing up anything....

I really cant seem to get this to work, I think I need a while loop, but I am not quite sure how to do it, this is my current code and in the edit tags is my idea on what to do:


else if(isset($_POST['edit-posted0']))
{
$_POST['comments'] = stripslashes($_POST['comments']);
// $posts = '1';
// $lines = count(file("guestbook.php"));
// $array = file("guestbook.php");
$file = fopen("guestbook.php", "w");
fputs($file, '<table class="post" Name=$_POST["type"] border=0><tr><td>');
fputs($file, $_POST['edit-name']);
fputs($file,'</td><td align="right">');
fputs($file, $time);
fputs($file, '</td></tr></table><table class="post" Name=$_POST["type"] border=0><tr><td>');
fputs($file, '<center>');
fputs($file, $_POST['edit-post']);
fputs($file, '<br>');
fputs($file, '<font color="CDCDCD" size=2>');
fputs($file, $IP);
fputs($file, '</font><hr>');
fputs($file, "<?php ");
fputs($file, '$IPP = getenv (REMOTE_ADDR);');
fputs($file, "\$admin = 'authorized.dat';\$IP = getenv (REMOTE_ADDR);if (!file_exists (\$admin)) { die('Error: \$admin does not exist.');}\$h = fopen (\$admin, 'r');while (!feof (\$h)) { \$line = fgets (\$h, 4096); \$line = trim (\$line); if (\$line == \$IP){ \$authorized=true;}else{ \$authroized=false; }}fclose(\$h);");
fputs($file, "if(\$authorized){ ");
fputs($file, "echo ('<center><form style=float:left action=gb.php method=post><input type=Submit Name=delete$lines Value=Delete></form></center>'); }");
fputs($file, "?>");
fputs($file, '<br><hr></td></tr></table>');
fputs($file, $newline);
/*
while($posts < $lines)
{
fputs($file, $array[$posts]);
}
*/
fclose($file);
}

except that only solves it if you want the edited post on the top, I am very confused :confused: :confused: So I need some help :p

sidney
05-09-2004, 03:29 PM
i have no idea also im guessing you want to just edit a section of guestbook.php

fopen(file,'w') will open the file for writing so will truncate file (deleete all that is ready there)

so may be you need to read the file first and then preg_replace new data over exsisting section


as to editing storage file your users would not need to use html if you loaded there post into text fileds they could then edit and save again

also maybe look at getting some database as this would make life alot simpler

stophon4
05-09-2004, 03:45 PM
My goal is to stay away from databases, and yes my guestbook does not just output text to the storage file, it is almost like this forum, so they would have to mess with html (beleive me, I tried) but I think I almost have it, I will edit my post (or if someone else has posted I will make a new post) whether my idea I just got worked or not :o

stophon4
05-09-2004, 09:01 PM
What the heck I'll post a new post :rolleyes:

I got it to work with the following code:


else if(isset($_POST['edit0']))
{
if($authorized)
{
$post = stripslashes($_POST['edit0-post']);
$Name = stripslashes($_POST['edit0-name']);
echo ("<form action='gb.php' method='post'><b>Name:</b><INPUT type='text' Name='edit-name' Value='$Name'><b>POST:</b><INPUT type='text' Name='edit-post' Value='$post'><INPUT type='Submit' Name='edit-posted0' Value='Edit Post'></form>");
}
else if(!$authorized)
{
echo ("<H1><b><font size='7' color'red'>You Do Not Have Permission To Access This Function</font></b></H1><br>");
}
}

and


else if(isset($_POST['edit-posted0']))
{
$_POST['comments'] = stripslashes($_POST['comments']);
$post0 = '1';
$lines = count(file("post-count.dat"));
$array = file("post-count.dat");
$file = fopen("guestbook.php", "w");
fputs($file, '<table class="post" Name=$_POST["type"] border=0><tr><td>');
fputs($file, $_POST['edit-name']);
fputs($file,'</td><td align="right">');
fputs($file, $time);
fputs($file, '</td></tr></table><table class="post" Name=$_POST["type"] border=0><tr><td>');
fputs($file, '<center>');
fputs($file, $_POST['edit-post']);
fputs($file, '<br>');
fputs($file, '<font color="CDCDCD" size=2>');
fputs($file, $IP);
fputs($file, '</font><hr>');
fputs($file, "<?php ");
fputs($file, '$IPP = getenv (REMOTE_ADDR);');
fputs($file, "\$admin = 'authorized.dat';\$IP = getenv (REMOTE_ADDR);if (!file_exists (\$admin)) { die('Error: \$admin does not exist.');}\$h = fopen (\$admin, 'r');while (!feof (\$h)) { \$line = fgets (\$h, 4096); \$line = trim (\$line); if (\$line == \$IP){ \$authorized=true;}else{ \$authroized=false; }}fclose(\$h);");
fputs($file, "if(\$authorized){ ");
fputs($file, "echo ('<center><form style=float:left action=gb.php method=post><input type=Submit Name=delete$lines Value=Delete></form></center>'); }");
fputs($file, "?>");
fputs($file, '<br><hr></td></tr></table>');
fputs($file, $newline);
if($lines > '0'){
fputs($file, $array[1]);
}if($lines > '1'){
fputs($file, $array[2]);
}if($lines > '2'){
fputs($file, $array[3]);
}if($lines > '3'){
fputs($file, $array[4]);
}if($lines > '4'){
fputs($file, $array[5]);
}if($lines > '5'){
fputs($file, $array[6]);
}if($lines > '6'){
fputs($file, $array[7]);
}if($lines > '7'){
fputs($file, $array[8]);
}if($lines > '8'){
fputs($file, $array[9]);
}if($lines > '9'){
fputs($file, $array[10]);
}if($lines > '10'){
fputs($file, $array[11]);
}if($lines > '11'){
fputs($file, $array[12]);
}if($lines > '12'){
fputs($file, $array[13]);
}if($lines > '13'){
fputs($file, $array[14]);
}if($lines > '14'){
fputs($file, $array[15]);
}if($lines > '15'){
fputs($file, $array[16]);
}if($lines > '16'){
fputs($file, $array[17]);
}if($lines > '17'){
fputs($file, $array[18]);
}if($lines > '18'){
fputs($file, $array[19]);
}if($lines > '19'){
fputs($file, $array[20]);
}if($lines > '20'){
fputs($file, $array[21]);
}if($lines > '21'){
fputs($file, $array[22]);
}if($lines > '22'){
fputs($file, $array[23]);
}if($lines > '23'){
fputs($file, $array[24]);
}if($lines > '24'){
fputs($file, $array[25]);
}if($lines > '25'){
fputs($file, $array[26]);
}
fclose($file);
}

anyway there are 25 of each of those because I dont know how to work it any other way (I repeated each of those codes, e.g. instead of edit0 -> edit1), I probably should have used a while loop but oh well :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum