0

Currently I have a form that makes use of fields and check boxes. The fields and checkboxes both update the database perfectly:

<?php
 function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
 {
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="stylized" class="myform">
      <form id="form" name="form" action="" method="post">

. . . . . .

 if(count($articletags) > 0)
{
 $articletags_string = implode(",", $articletags);
}

 if (isset($_POST['submit']))
 { 
 $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
 $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
 $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
 $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
 $articletags = implode(',', $_POST['articletags']);

. . .

mysql_query("INSERT articles SET articletitle='$articletitle',   articleorganization='$articleorganization', articledate='$articledate',   articleurl='$articleurl', articletags='$articletags' ")
 or die(mysql_error()); 

 // once saved, redirect to success page
 header("Location:addsuccess.html"); 
 }
 }
 else

{
 renderForm('','','','');
 }
?>

Now, though, I'm wondering if I should have gone with a boolean checkbox instead.

The reason is that I've built an edit form as well and it follows the new entry form exactly except that values are already filled in via the MySQL DB.

So, I'm assuming that it would be considerably easier to use boolean, right?

So, instead of using an array, I should give the checkboxes different names, and on the edit.php page I can use something like:

<?php
 function renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags)
 {
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="stylized" class="myform">
      <form id="form" name="form" action="" method="post">
      <input type="hidden" name="id" value="<?php echo $id; ?>"/>
        <h1>Edit Details for &nbsp; &nbsp;<?php echo $articletitle; ?></h1>
        <fieldset>
          <legend>Article details</legend>
          <div class="row">
            <div class="field"><label>Article Title</label><input type="text" name="articletitle" value="<?php echo $articletitle; ?>"/></div>
          </div>
          <div class="row">
            <div class="field"><label>Article Author </label><input type="text" name="articleorganization" value="<?php echo $articleorganization; ?>"/></div>
            <div class="field"><label>Article Date </label><input type="text" name="articledate" value="<?php echo $articledate; ?>"/></div>
          </div>
          <div class="row">
            <div class="field"><label>Article Url: </label><input type="text" name="articleurl" value="<?php echo $articleurl; ?>"/></div>
          <div class="row">

          <input type="checkbox" name="articletags1" value="checkbox" id="articletags_0" />
          <input type="checkbox" name="articletags2" value="checkbox 2" id="articletags_1" />

          </div>
        </fieldset>
        <footer><input type="submit" name="submit" value="Submit"></footer></form>
    </div>
  </body>
</html>
<?php
 }

 include('settings.php');

 if (isset($_POST['submit']))
 { 
 if (is_numeric($_POST['id']))
 {
 $id = $_POST['id'];
 $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
 $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
 $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
 $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
 $articletags = implode(',', $_POST['articletags']);


 if ($articletitle == '' || $articletags == '')
 {
 $error = 'ERROR: Please fill in all required fields!';
 renderForm($id, $articletitle, $articletags);
 }
 else
 {
 mysql_query("UPDATE articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' WHERE id=$id")
 or die(mysql_error()); 

 header("Location: editsuccess.html"); 
 }
 }
 else
 {
 echo 'Error!';
 }
 }
 else
 {
 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM articles WHERE id=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);
 if($row)
 {
 $articletitle = $row['articletitle'];
 $articleorganization = $row['articleorganization'];
 $articledate = $row['articledate'];
 $articleurl = $row['articleurl'];
 $articletags = $row['articletags'];

 renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags, '');
 }
   else
 {
   echo "No results!";
 }
 }
   else
 {
   echo 'Error!';
    }
     }
?>

The problem with that though is that my checkboxes on the edit.php page still aren't showing the checked state.

1 Answer 1

1

You need to use checked="checked" in the same way as you used for value. See the solution bottom:

<input type="checkbox" <?php if(isset($_POST[articletags1])) echo 'checked="checked" ' ?>name="articletags1" value="checkbox" id="articletags_0" />
<input type="checkbox" <?php if(isset($_POST[articletags2])) echo 'checked="checked" ' ?>name="articletags2" value="checkbox 2" id="articletags_1" />

Hope this works. Updated the right code. Use ini_set('display_errors', 1); to get the error generated.

Sign up to request clarification or add additional context in comments.

2 Comments

Praveen, I tried your suggestion and it resulted in a blank page...could you be a little more specific?
There is a problem with the isset(). It needs a $_POST[], otherwise the articletags1 is interpreted as a constant. isset($_POST['articletags1'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.