Jump to content

PHP/MySQL forms... PLEASE HELP


ansonb

Recommended Posts

I am having trouble with checkboxes and the information they post to my database.

 

I have a site registration form and one of the fields is a checkbox to accept or decline joining our mailing list.

 

"<input type="checkbox" id="mailinglist" name="mailinglist" value="yes" style="width: 10px;" checked />"

 

or

 

"<input type="checkbox" id="mailinglist" name="mailinglist" value="no" style="width: 10px;" />"

 

I want the box to be checked in the form to begin with. Then if they wish to decline joining the mailing list, they simply uncheck it.

 

My problem is:

 

If the box IS checked, I want to post a value of "yes" into the mailinglist field of the database. But, if it IS NOT checked, I want to post a value of "no" into this field.

 

I can not figure out the code to use to accomplish this. It keeps posting "yes" into the database regardless of if it is checked or not !

 

Please Help !! Thanks !!

Link to comment
Share on other sites

Try this for you html:

<form id="mail" method="post" action="action.php">
My Milaing list:
<br>
Yes : <input type="radio" id="mailinglist" name="mailinglist" value="I_Want" style="width: 10px;" checked="yes">
No : <input type="radio" id="mailinglist" name="mailinglist" value="No_Want" style="width: 10px;">
<br>
<input type="submit" value="click">
</form>

 

and this is the action.php to process the form:

<?php
$cks = $_POST["mailinglist"];
echo $cks;
?>

Link to comment
Share on other sites

The value of a checkbox is only sent in the $_POST array if the checkbox is checked, so if the value is present, insert the yes (1) in the DB, if it isn't, you can safely assume the user unchecked it and insert a no (0). Something following this general idea would be how I'd probably approach it.

 

if( isset($_POST['your_checkbox']) ) {
     checkbox = 1;
} else {
     $checkbox = 0;
}

Link to comment
Share on other sites

It is not working. I am frustrated and can not figure it out. Here is the entire PHP code. Please inspect and tell me what I am doing wrong. Thanks.

 

"<?php

require_once('../../includes/header.php');

 

if (file_exists(SITE_ROOT.'/themes/'.THEME.'/account/register.php')){//account register from the theme!

require_once(SITE_ROOT.'/themes/'.THEME.'/account/register.php');

}

else{//not found in theme

 

?>

<h2><?php _e("Register")?></h2>

<?php

$show_form = true;

 

if ($_POST && checkCSRF('register_user')){

    if(checkMathCaptcha('register')) {

        $name = cP('name');

        $email = cP('email');

        $password = cP('password');

        $password_confirmation = cP('password_confirmation');

$mailinglist = cP('mailinglist');

$agree_terms = cP('agree_terms');

if( isset($_POST['mailinglist']) ) {

          $mailinglist = 1;

}          else {

          $mailinglist = 0;

}

   

if ($agree_terms == "yes"){

if (isEmail($email)){       

                if ($password == $password_confirmation){

                    $account = new Account($email);

                    if ($account->exists){

                        echo "<div id='sysmessage'>".T_("Account already exists")."</div>";

                    }

                    else {

                        if ($account->Register($name,$email,$password)){

                            $token=$account->token();

                           

                            $url=accountRegisterURL();

                            if (strpos($url,"?")) $url.='&account='.$email.'&token='.$token.'&action=confirm';

                            else $url.='?account='.$email.'&token='.$token.'&action=confirm';

                           

                            $message='<p>'.T_("Click the following link or copy and paste it into your browser address field to activate your account").'</p>

                        <p><a href="'.$url.'">'.T_("Confirm account").'</a></p><p>'.$url.'</p>';

                           

                            $array_content[]=array("ACCOUNT", $name);

                            $array_content[]=array("MESSAGE", $message);

                           

                            $bodyHTML=buildEmailBodyHTML($array_content);

                           

                        sendEmail($email,T_("Confirm your account")." - ".SITE_NAME,$bodyHTML);//email registration confirm request

                           

                            $show_form = false;

                            echo "<div id='sysmessage'>".T_("Instructions to confirm your account has been sent").". ".T_("Please, check your email")."</div>";

                        } else _e("An unexpected error has occurred trying to register your account");

                    }

                } else echo "<div id='sysmessage'>".T_("Passwords do not match")."</div>";

            } else echo "<div id='sysmessage'>".T_("Wrong email")."</div>";

        } else echo "<div id='sysmessage'>".T_("Terms agreement is required")."</div>";

    } else echo "<div id='sysmessage'>".T_("Wrong captcha")."</div>";//wrong captcha

}

 

if (trim(cG('account'))!="" && trim(cG('token'))!="" && trim(cG('action'))=="confirm"){

    $show_form = false;

   

    $email = trim(cG('account'));

    $token = trim(cG('token'));

   

    $account = new Account($email);

    if ($account->exists){

        if ($account->Activate($token)){

            echo "<div id='sysmessage'>".T_("Your account has been succesfully confirmed")."</div>";

           

            $bodyHTML="<p>".T_("NEW account registered")."</p><br/>".T_("Email").": ".$account->email." - ".$account->signupTimeStamp();

        sendEmail(NOTIFY_EMAIL,T_("NEW account")." - ".SITE_NAME,$bodyHTML);//email to the NOTIFY_EMAIL

           

            $account->logOn($account->password());

           

            echo '<p><a href="'.accountURL().'">'.T_("Welcome").' '.$account->name.'</a></p><br/>';

        } else echo "<div id='sysmessage'>".T_("An unexpected error has occurred trying to confirm your account")."</div>";

    } else echo "<div id='sysmessage'>".T_("Account not found")."</div>";

}

 

if ($show_form){

?>

<div>

<form id="registerForm" action="" onsubmit="return checkForm(this);" method="post">

    <p><label for="name"><?php _e("Name")?>:<br />

    <input type="text" id="name" name="name" value="<?php echo $name;?>" maxlength="250" onblur="validateText(this);" lang="false" /></label></p>

    <p><label for="email"><?php _e("Email")?>:<br />

    <input type="text" id="email" name="email" value="<?php echo $email;?>" maxlength="145" onblur="validateEmail(this);" lang="false" /></label></p>

    <p><label for="password"><?php _e("Password")?>:<br />

    <input type="password" id="password" name="password" value="" onblur="validateText(this);" lang="false" /></label></p>

    <p><label for="password_confirmation"><?php _e("Confirm password")?>:<br />

    <input type="password" id="password_confirmation" value="" name="password_confirmation" onblur="validateText(this);" lang="false" /></label></p>

    <p><label for="mailinglist"><input type="checkbox" id="mailinglist" name="mailinglist" value="<?php echo $mailinglist;?>" style="width: 10px;" /></label>Join Mailing List</p>

    <p><label for="agree_terms"><input type="checkbox" id="agree_terms" name="agree_terms" value="yes" style="width: 10px;" /> <?php _e("Accept")?> <a href="<?php echo termsURL();?>"><?php _e("Terms")?></a> - <?php echo SITE_NAME?></label></p>

    <br />

<?php if (CAPTCHA){

mathCaptcha('register');?>

<p><input id="math" name="math" type="text" size="2" maxlength="2"  onblur="validateNumber(this);"  onkeypress="return isNumberKey(event);" lang="false" /></p>

    <br /><?php }?>

    <?php createCSRF('register_user');?>

    <p><input name="submit" id="submit" type="submit" value="<?php _e("Submit")?>" /></p>

</form>

</div>

<?php

}

 

}//if else

 

require_once('../../includes/footer.php');

?>"

Link to comment
Share on other sites

<?php
require_once('../../includes/header.php');

if (file_exists(SITE_ROOT.'/themes/'.THEME.'/account/register.php')){//account register from the theme!
   require_once(SITE_ROOT.'/themes/'.THEME.'/account/register.php'); 
}
else{//not found in theme

?>
<h2><?php _e("Register")?></h2>
<?php
$show_form = true;

if ($_POST && checkCSRF('register_user')){
    if(checkMathCaptcha('register'))   {
        $name = cP('name');
        $email = cP('email');
        $password = cP('password');
        $password_confirmation = cP('password_confirmation');
      $mailinglist = cP('mailinglist');
      $agree_terms = cP('agree_terms');
      if( isset($_POST['mailinglist']) ) {
          $mailinglist = 1;
}           else {
          $mailinglist = 0;
}
    
      if ($agree_terms == "yes"){
         if (isEmail($email)){        
                if ($password == $password_confirmation){
                    $account = new Account($email);
                    if ($account->exists){
                        echo "<div id='sysmessage'>".T_("Account already exists")."</div>";
                    }
                    else {
                        if ($account->Register($name,$email,$password)){
                            $token=$account->token();
                            
                            $url=accountRegisterURL();
                            if (strpos($url,"?")) $url.='&account='.$email.'&token='.$token.'&action=confirm';
                            else $url.='?account='.$email.'&token='.$token.'&action=confirm';
                            
                            $message='<p>'.T_("Click the following link or copy and paste it into your browser address field to activate your account").'</p>
                           <p><a href="'.$url.'">'.T_("Confirm account").'</a></p><p>'.$url.'</p>';
                            
                            $array_content[]=array("ACCOUNT", $name);
                            $array_content[]=array("MESSAGE", $message);
                            
                            $bodyHTML=buildEmailBodyHTML($array_content);
                            
                           sendEmail($email,T_("Confirm your account")." - ".SITE_NAME,$bodyHTML);//email registration confirm request
                            
                            $show_form = false;
                            echo "<div id='sysmessage'>".T_("Instructions to confirm your account has been sent").". ".T_("Please, check your email")."</div>";
                        } else _e("An unexpected error has occurred trying to register your account");
                    }
                } else echo "<div id='sysmessage'>".T_("Passwords do not match")."</div>";
            } else echo "<div id='sysmessage'>".T_("Wrong email")."</div>";
        } else echo "<div id='sysmessage'>".T_("Terms agreement is required")."</div>";
    } else echo "<div id='sysmessage'>".T_("Wrong captcha")."</div>";//wrong captcha
}

if (trim(cG('account'))!="" && trim(cG('token'))!="" && trim(cG('action'))=="confirm"){
    $show_form = false;
    
    $email = trim(cG('account'));
    $token = trim(cG('token'));
    
    $account = new Account($email);
    if ($account->exists){
        if ($account->Activate($token)){
            echo "<div id='sysmessage'>".T_("Your account has been succesfully confirmed")."</div>";
            
            $bodyHTML="<p>".T_("NEW account registered")."</p><br/>".T_("Email").": ".$account->email." - ".$account->signupTimeStamp();
           sendEmail(NOTIFY_EMAIL,T_("NEW account")." - ".SITE_NAME,$bodyHTML);//email to the NOTIFY_EMAIL
            
            $account->logOn($account->password());
            
            echo '<p><a href="'.accountURL().'">'.T_("Welcome").' '.$account->name.'</a></p><br/>';
        } else echo "<div id='sysmessage'>".T_("An unexpected error has occurred trying to confirm your account")."</div>";
    } else echo "<div id='sysmessage'>".T_("Account not found")."</div>";
}

if ($show_form){
?>
<div>
<form id="registerForm" action="" onsubmit="return checkForm(this);" method="post">
    <p><label for="name"><?php _e("Name")?>:<br />
    <input type="text" id="name" name="name" value="<?php echo $name;?>" maxlength="250" onblur="validateText(this);" lang="false" /></label></p>
    <p><label for="email"><?php _e("Email")?>:<br />
    <input type="text" id="email" name="email" value="<?php echo $email;?>" maxlength="145" onblur="validateEmail(this);" lang="false" /></label></p>
    <p><label for="password"><?php _e("Password")?>:<br />
    <input type="password" id="password" name="password" value="" onblur="validateText(this);" lang="false" /></label></p>
    <p><label for="password_confirmation"><?php _e("Confirm password")?>:<br />
    <input type="password" id="password_confirmation" value="" name="password_confirmation" onblur="validateText(this);" lang="false" /></label></p>
    <p><label for="mailinglist"><input type="checkbox" id="mailinglist" name="mailinglist" value="<?php echo $mailinglist;?>" style="width: 10px;" /></label>Join Mailing List</p>
    <p><label for="agree_terms"><input type="checkbox" id="agree_terms" name="agree_terms" value="yes" style="width: 10px;" /> <?php _e("Accept")?> <a href="<?php echo termsURL();?>"><?php _e("Terms")?></a> - <?php echo SITE_NAME?></label></p>
    <br />
   <?php if (CAPTCHA){
      mathCaptcha('register');?>
   <p><input id="math" name="math" type="text" size="2" maxlength="2"  onblur="validateNumber(this);"  onkeypress="return isNumberKey(event);" lang="false" /></p>
    <br /><?php }?>
    <?php createCSRF('register_user');?>
    <p><input name="submit" id="submit" type="submit" value="<?php _e("Submit")?>" /></p>
</form>
</div>
<?php
}

}//if else

require_once('../../includes/footer.php');
?>

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.