Jump to content

Recommended Posts

Ok, here's what I'm attempting to do.  Create a website where we can document daily/weekly tasks with a simple check off type list.  I've created a MySQL DB and I have a page where my users can setup their Login IDs and task list.  My problem is with the code below, I have it to where it brings up the user's task list and places a checkbox by it.  What I cannot get to work is checking to see if the checkbox has been checked, if it has then I want to update a table in the DB with the user's ID, the task by the checkbox and the current date.  I have probably gotten this all backwards and would greatly appreciate any help.  Thanks!

 

<?php

session_start();

 

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

submit();

}

 

//Function Update

function submit() {

 

//This is where I intend to place the code to update the MySQL table

 

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

print "Checked";

}

} //End Function

 

$con = mysql_connect("bmcsvrapache","root","brookwood"); //MySQL DB Username and Password

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

 

print "<h3>Tasks for $myusername</h3>";

 

mysql_select_db("tasks", $con); //MySQL DB Name

 

$result = mysql_query( "SELECT * FROM tasklist where username = '$myusername'" );

$num_rows = mysql_num_rows($result);

 

$i=1; //set first checkbox number

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

$chname="chbx$i";

print "<input type=checkbox name=$chname value=0>";

printf("  %s  Task: %s", $row["type"], $row["task"]);

print "<br\n>";

$i++;

}

?>

 

<html>

 

<form action="<?= $PHP_SELF ?>" method="POST">

<input type="submit" name="submit" value="Submit">

</form>

</html>

 

<?php

echo "<a href='login_success.php'>Return to Menu Page</a>";

 

mysql_close($con);

?>

Ok I tried that but for some reason it still not realizing that a checkbox is checked.  Will the way I have assigned a checkbox to each row brought back from the DB work?  LOL do you ever feel you just start over???  Thanks

I really owe big time, that worked!!!  I changed the code to this and it now sees if the checkbox is checked or not.  Thank you very much!  Now to see if I can get the code to update the table in...should be easy now.

 

<html>

 

<form action="<?= $PHP_SELF ?>" method="POST">

 

<?php

$i=1; //set first checkbox number

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

$chname="chbx$i";

print "<input type=checkbox name=$chname value=1>";

printf("  %s  Task: %s", $row["type"], $row["task"]);

print "<br\n>";

$i++;

}

?>

 

<input type="submit" name="submit" value="Submit">

</form>

 

</html>

 

One more quick question if I could.  I noticed that when I click the Submit button even though I'm posting back to the same page I loose the results that php returned from MySQL query when the page first loaded.  Do I need to use the post function on the data returned in order to access it in my php function and if so what would be the syntax?

 

Example of what I'm speaking of is the $num_rows, the print $num_rows right after the while statement returns the value but the print $num_rows in the function triggered by a checkbox being checked is empty.

 

<?php

session_start();

 

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

submit();

}

 

//Function Update

function submit() {

 

// Code to update MySQL table goes here for now the following is used for testing

 

if ($_POST['chbx1']==1){

print "Checked";

print "Number of Rows: $num_rows";

}

}

 

$con = mysql_connect("bmcsvrapache","root","brookwood"); //MySQL DB Username and Password

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

 

print "<h3>Tasks for $myusername</h3>";

 

mysql_select_db("tasks", $con); //MySQL DB Name

 

$result = mysql_query( "SELECT * FROM tasklist where username = '$myusername'" );

$num_rows = mysql_num_rows($result);

?>

 

 

 

<html>

 

<form action="<?= $PHP_SELF ?>" method="POST">

 

<?php

$i=1; //set first checkbox number

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

$chname="chbx$i";

print "<input type=checkbox name=$chname value=1>";

printf("  %s  Task: %s", $row["type"], $row["task"]);

print "<br\n>";

$i++;

}

// Used for testing

print "# of rows: $num_rows";

?>

 

<input type="submit" name="submit" value="Submit">

</form>

 

</html>

 

<?php

 

echo "<br /><br />";

echo "<a href='login_success.php'>Return to Menu Page</a>";

 

 

mysql_close($con);

?>

Im not entirely sure what you mean by 'lose' the previous MySQL data, the code looks ok..

 

As for the checkbox thing..

The way you had it originally was correct.

If a checkbox is NOT checked when the form is posted it will not be sent to PHP within the $_POST variables, therefor doing

if($_POST['chbx1']==1){

will throw an undefined index error..

Although setting the value to 1 is more appropriate, the code doesnt care what the value is, as long as its got one and the box is ticked.

I'm sorry, the way I explained maybe a bit confusing.  What I'm trying to do link the checkboxes with the data in the rows.  I was just going to use the variable $num_rows to setup a foreach loop to run through the checkboxes and if they were checked take the data on that row and send to a table called Completed.  What I was finding after clicking on the submit button to check the status of the checkboxes was that the value in $num_rows was no longer there.  Does that make more sense as to what I'm trying to do.  Thanks

Presumably you are talking about this line inside your submit function

print "Number of Rows: $num_rows";

yes?

 

If so.. that is because the variable isnt defined until AFTER submit() is called..

so what you need to do is change the location of WHERE you call it. ie move the function submit() to after your initial query and also add 'global $num_rows; to the top of the submit function. This enables the function to read that variable..

 

$result = mysql_query( "SELECT * FROM tasklist where username = '$myusername'" );
$num_rows = mysql_num_rows($result);

if (isset($_POST['submit'])) {
submit();
}

 

and

 

function submit() {
   global $num_rows;
   // Code to update MySQL table goes here for now the following is used for testing

   if ($_POST['chbx1']==1){
      print "Checked";
      print "Number of Rows: $num_rows";
   }
}

make following changes :

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    $chname="chbx$i";

  print "<input type=checkbox name=chk[] value=".$row['id'].">";// change no 1

  printf("  %s  Task: %s", $row["type"], $row["task"]);

  print "<br\n>";

  $i++;

}

 

----------------------

2. replace the submit function with this  & see if its working or not

 

function submit() {

  //This is where I intend to place the code to update the MySQL table

 

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

      print "Checked";

    echo count($_POST['chk']);

    echo "<br/>";

    $ids = implode(",",$_POST['chk'] )

    mysql_query("UPDATE table set field='value' where id IN ($ids)");

  }

} //End Function

 

 

You may get parse error with these modification ........please remove those and check the functionality

 

 

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.