dimanche 30 août 2015

I can't get the checkbox to transmit properly When I hit submit always marks it as unchecked in database

The goal of the code is to transmit the checkbox information and update the database. It does update the database, if I uncheck the box and hit submit, or if i check the box and hit submit the value gets moved to zero.

The database table users is set up so allow_email is (int) with a default of 1 with 1 space allowed only one or zero.

The goal is to allow email to be checked for people who want to be a part of my mass emailing. if it is unchecked they will not get emailed for updates with new articles. By default i have the database set to automatically make all new users a 1 for allowing emails.

When they go to the settings page i and they can update their user information ie first last or email and can uncheck or check a box to allow them to receive emails.

I have the checkbox html code correct I believe because the checkbox will represent properly based on what the database displays so if it's a 1 when the user goes to the page the checkbox will still be checked grabbing that from the database properly. If i leave it checked and hit submit to change data, the code updates the database and makes it a zero, when you refresh the users settings.php page the box is then unchecked now reading that the database has been set to 0. Only if you check the box and hit submit it stays zero. (I am lost here). here is my code. Please assist me if you can.

settings.php

  <?php
  if (isset($_GET['success']) === true && empty($_GET['success']) ===true) {
   echo 'Your details have been updated!';
  } else {
   if (empty($_POST) === false && empty($errors) === true) {

    $update_data = array(
      'first_name'  => $_POST['first_name'],
      'last_name'  => $_POST['last_name'],
      'email_address' => $_POST['email_address'],
      'allow_email' => ($_POST['allow_email'] == 'on') ? 1 : 0
    );

   update_user($session_user_id, $update_data);
   header('Location: settings.php?success');
   exit();

  } else if (empty($errors) === false) {
   echo output_errors($errors);
  }
  ?>

  <form action="" method="post">
   <ul>
    <li>
     First Name:*<br>
     <input type="text" name="first_name" value="<?php echo          $user_data['first_name']; ?>">
</li>
<li>
 Last Name:<br>
 <input type="text" name="last_name" value="<?php echo   $user_data['last_name']; ?>">
</li>
<li>
 Email Address:*<br>
 <input type="text" name="email_address" value="<?php echo  $user_data['email_address']; ?>">
</li>
<li>
 <input type="checkbox" name"allow_email" <?php if ($user_data['allow_email'] == 1) { echo 'checked="checked"'; } ?>> Like the idea of getting updates from us about new articles?
</li>
<li>
 <input type="submit" value="update">
</li>

You can see that i am passing 'allow email' into $user_data variable here. init.php

if (logged_in() === true) {
 $session_user_id = $_SESSION['user_id'];
 $user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email_address', 'password_recover', 'type', 'allow_email');
 if (user_active($user_data['username']) === false) {
  session_destroy();
  header('Location: index.php');
  exit();

 }
 if ($current_file !== 'changepassword.php' && $current_file !== 'logout.php' && $user_data['password_recover'] == 1) {
  header('Location: changepassword.php?force');
  exit();
 }
}




Aucun commentaire:

Enregistrer un commentaire