Jump to content

Inserting checkbox values into my DB


georgehowell

Recommended Posts

hi again, sorry that my first ever forum entry was so vaguely composed.

 

my question is, if the target column in the MySQL DB is a "tiny init", will the following html checkbox give me a "1" if checked, or "0" unchecked?

<input name="subscribe" type="checkbox" id="subscribe" value="true" property="boolean" >

 

 

Here's my PHP:

<?php

include("lib/ConnectToDb.php");

include("lib/User.php");

 

print_r($_POST);

 

if(isset($_POST['username']) && isset($_POST['password']))

    {

    $user = new User();

    if($user->register(

$_POST['subscribe']))

        {

        include("Welcome.php");

        exit();

        }

    }

 

?>

 

 

Link to comment
Share on other sites

I'm not sure where you got the property attribute from for that input tag, I've never heard of it.

 

That will give the value of 'true' as a string if you use $_POST['subscribe']. Since it is a checkbox though the value will not be submitted if it is not checked. As such you can use a simple check to get a boolean representation...

 

$subscribe = isset($_POST['subscribe']) ? 1 : 0;

Link to comment
Share on other sites

hi CodeCanyon

I'm giving it ago in the following fashion:

 

"User.php"

<?php  class User extends ConnectToDB 

{

  public function newsletter($subscribe = isset($_POST['subscribe']) ? 1 : 0; 

    {

      $ps = $this->db->prepare("Insert into users (subscribe) values (?)");

 

      $ps->execute(array($subscribe));

 

      return ($ps->rowCount() == 1);

  }

}

?>

 

Anybody, please let me know if this looks totally wrong.

Not sure what values to set the column within "users" table however.

 

Thanx a mil CodeCanyon

 

Link to comment
Share on other sites

a.) Who in the hell is CodeCanyon?  :shrug:

b.) The isset code wouldn't normally go in the function declaration, you would normally place it wherever you are calling the function newsletter. Having said that you have a syntax error since you do not close the brackets for newsletters parameters (after 1 : 0).

Link to comment
Share on other sites

sorry Cags, got you mixed up with some advert at the bottom of the page.

I kept getting errors after placing the code you gave me into "register.php" - something i'm doing wrong.

The attached files give me a result at least, but there's no differentiation between whether the checkbox is checked or not... 

 

Here's the html

<form name="join" action="register.php" onsubmit="checkEmptyFields()" method="post">

<input name="subscribe" type="checkbox" id="subscribe">

<input name="submit" type="submit" id="submit"  >

</form>

 

p.s.  What do i owe you for helping me on this one?

thanx, George

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

I realise what you did with the name, it just amused me :).

 

In register.php, line 25 contains...

 

$_POST['subscribe'],))

 

...replace that with...

 

isset($_POST['subscribe']) ? 1 : 0))

 

Technically you don't owe me anything, but my beer fund is always appreciative of donations ;)

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.