Jump to content

$_POST checkbox status


roeyhaim

Recommended Posts

but i need also the value of the checkboxes which not checked.

any idea?

They don't have a value because they are not checked.  If all checkbox values were sent, how would you know which ones were checked and which were not?  There are different ways to handle this, but what it comes down to is that you need to know what checkboxes are on the form and then check to see if they are submitted.  Example using an array:

 

FORM

<input type="checkbox" name="color[red]">
<input type="checkbox" name="color[green]">
<input type="checkbox" name="color[blue]">

 

PHP

if(isset($_POST['color']['red'])) echo 'RED';
if(isset($_POST['color']['green'])) echo 'GREEN';
if(isset($_POST['color']['blue'])) echo 'BLUE';

 

 

 

 

Link to comment
Share on other sites

i have a form with some checkboxes.

when the user want to edit his profile i need to take the data from the db an put it in the form.

and when he updated his profile i need to put in the db the value of each choice.

the problem is that i cannot do this on the same page with "if" statement.

i need to send the whole data to a class and from there i need to insert it into the db.

 

Link to comment
Share on other sites

<?php if(!isset($_POST['Submit'])) { ?>
<form method=post action=addOmna.php?action=add name=add>
<center>
<table align="center" border="5" dir="rtl" style="border-style: double;">
<tr><td align="center" nowrap="nowrap">
fullname: <input type="text" name="fullname" value="" /></td></tr> 
<tr><td align="center" nowrap="nowrap">
phone: <input type="text" name="phone" value="" /></td></tr>
<tr><td align="center" nowrap="nowrap">
Address: <input type="text" name="address" value="" /></td></tr>
<tr><td align="center" nowrap="nowrap">
E-Mail: <input type="text" name="email" value="" /></td></tr>
<tr><td align="center" nowrap="nowrap">
agree to get mails: <input type="checkbox" name="mails" value="1" /></td></tr>
<tr><td align="center" nowrap="nowrap">
show image profile: <input type="checkbox" name="status" value="1" checked /></td></tr>
<tr><td align="center" nowrap="nowrap">
comments: <br><textarea rows="5" name="comments"></textarea></td></tr>
<tr><td align="center" nowrap="nowrap">
<BR>
<input type="submit" value="Submit" name="Submit" />
</form></td></tr>    
</table>

</html>

<?php } else {
                   $db->insert("users", $_POST);
}

 

And this is the insert() function from the $db class:

 

function insert($table, $vars){
            foreach($vars as $key => $var){
                $values .= "'$var', ";
                $cols .= "$key, ";
            }
            $values = substr_replace($values, "", -12); 
            $cols = substr_replace($cols, "", -10);
            $sql = "INSERT INTO $table ($cols) VALUES ($values)";
            mysql_query($SQL) or die('Error, insert query failed: ' . mysql_error());
            echo "<center><font size=5 color=red>";
            echo "Process Complete .";
            echo "<BR>";
            echo "<a href=index.php>Home Page</a>";
            echo "</font></center>";
        }

 

as you can see i need all the values else i got error message:

Error, insert query failed: Query was empty

 

unless you have another idea to this class...

Link to comment
Share on other sites

[EDIT]^^^ I was beaten to that one ;-p

 

$sql = "INSERT INTO $table ($cols) VALUES ($values)";
mysql_query($SQL) or die('Error, insert query failed: ' . mysql_error());

 

Your error is obvious (hint: variables are CaSeSeNsItIvE) And hopefully your connection handle is correct too.

 

A bit of error handling wouldn't be amis here either:-

 

            $success = mysql_query($sql) or die('Error, insert query failed: ' . mysql_error());
            
            if ($success){
            echo "<center><font size=5 color=red>";
            echo "Process Complete .";
            echo "<BR>";
            echo "<a href=index.php>Home Page</a>";
            echo "</font></center>";
            }
            else{
            echo "something went wrong";
            exit;
            }

 

See what I mean - mysql_query() returns a bool, you may as well use it..

 

Rw

Link to comment
Share on other sites

You should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. You will save a ton of time. The nonexistent $SQL variable would have produced an undefined error message that would have alerted you to a problem with it.

Link to comment
Share on other sites

On a development system, you would typically set those in the master php.ini so that parse errors will be reported and displayed as well (setting those in your script will only show fatal runtime, warning, and notice errors.) Stop and start your web server to get any change made to the master php.ini to take effect and use a phpinfo(); statement to confirm that the settings actually changed in case the php.ini that you changed is not the one that php is using.

Link to comment
Share on other sites

<tangent>

I'm developing in NuSphere PhpED.

you know how i can config the debugger to that?

or its in the php code?

 

I have been looking for a new text editor that supports code folding, and from what the blurb tells me this looks pretty good.

 

I have used text pad for 8+ years (notepad before that for a few years too!), and am quite happy with it, but having code folding would make it THE perfect tool

 

Thanks for sharing your chosen IDE with us, I shall have to try it, it seems like the only downside is that it is a complete IDE similar to zend IDE & Eclipse IDE (which I use for NIOS) which is a pig to set up.

</tangent>

 

Yes, developing with error reporting/display errors is an absolute must, though I do set it to this:

 

error_reporting(E_ALL|E_STRICT|E_DEPRECATE);

 

I find as this satisfies all compatibility issues, and makes you structure things a little better when designing the flow of your work; especially with in class context.

 

Rw

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.