Jump to content

checkboxes


GSP

Recommended Posts

I have this form:

<form name="survey" action="submit.php" method="post">
Name: <input type="text" name="name" /> <br />
Option1: <input type="checkbox" name="option1" value="yes" /> <br />
Option2: <input type="checkbox" name="option2" value="yes" /> <br />
Option3: <input type="checkbox" name="option3" value="yes" /> <br />
<input type="submit" value="Submit"/>

 

Which submits to submit.php:

// database connection here
$name = $_POST['name'];
$op1 = $_POST['option1'];
$op2 = $_POST['option2'];
$op3 = $_POST['option3'];

if (is_null($op1)) {
$op1 = no;
}
if (is_null($op2)) {
$op2 = no;
}
if (is_null($op3)) {
$op3 = no;
}

$q = "INSERT INTO options (name, option1, option2, option3) VALUES ('$name','$op1','$op2','$op3')";

if (mysql_query($q)) 
{
echo 'thanks!';
}
else (mysql_error());

 

So people just visit the form, fill out their name and check some options. They are unchecked by default, and if unchecked, it will send to the database as "no" as opposed to null. I then have this administration panel-type thing where I can see what's in the database:

$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo '<td>Update</td>';
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell) {
	if ($cell == yes){
	echo '<td><input type="checkbox" checked /> </td>';
	}
	else if ($cell == no) {
	echo '<td><input type="checkbox" /></td>';
	}
        else {
	echo "<td>$cell</td>";
	}
}

echo '<td><input type="submit" value="submit" /></td>';
echo "</tr>\n";
}
mysql_free_result($result);

 

I'm trying to have each name and the yes/no options displayed per row. I want to be able to toggle the yes/no options, hit a submit button, and then have it update the database with the updated values. If the update is successful, I guess have a popup window and say "values updated" or "error" or something like that.

 

But I do not know where to start. I'm guessing I need each checkbox to have a unique name? (I guess that when the table loops per row, the entire columns are [name], [option1], [option2], [option3])

 

I googled, but didn't find any answers I could understand, about the problems of having multiple submit buttons. How does the submit button know which values to update? Will it update all values that gets spit out from the database?

 

Any help would be very much appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/
Share on other sites

I can't really see your problem else than you need the finish tag for the <form> in the html code.

Actually your code looks pretty good, and simple?

 

And about the submit button:

When you press a button with the type submit, it will just execute the form.

It really doesn't matter how many submit buttons you have, they'll all have to same function ^^

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003135
Share on other sites

I can't really see your problem else than you need the finish tag for the <form> in the html code.

Actually your code looks pretty good, and simple?

 

And about the submit button:

When you press a button with the type submit, it will just execute the form.

It really doesn't matter how many submit buttons you have, they'll all have to same function ^^

 

I fail in copy and paste. In my code, the form tag is closed.  :-[

 

My problem right now is that I don't know how I should do this. I was thinking that I could create a function, execute it when the submit button is clicked, and then the function would compare the values from the form and then to the database, and if the values don't match, update with the new value. Then a popup window appears.

 

In the creation of the form, I'd just put the action to a new page and that new page inserts the value. But in that, I just made a variable equal to the $_POST["fieldname"], and the field names are unique for that single row. If I display the entire table, would it look like

[field1] | [field2] | [field3] | [field4]

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

name | option1 | option 2 | option3

name | option1 | option 2 | option3

name | option1 | option 2 | option3

name | option1 | option 2 | option3

name | option1 | option 2 | option3

?

If so, how do I create a function so that it would compare the current table to the table in the database and update if necessary? Do I load each row as an array? and then move on to the next row?

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003152
Share on other sites

To solve the problem with the unique forms you can start out by First make an "id" column to the table, and set it to auto increase ^^.

Then make sure, you create a new form for each set of boxes.

And then create a hidden input with a name similar to all the other, but with the value of the id of the row.

 

example upcoming

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003155
Share on other sites

To solve the problem with the unique forms you can start out by First make an "id" column to the table, and set it to auto increase ^^.

Then make sure, you create a new form for each set of boxes.

And then create a hidden input with a name similar to all the other, but with the value of the id of the row.

 

example upcoming

 

Adding an auto-increment, primary key "id" column was the first thing I did after I replied! :D

 

But I need an example. For some reason, the "thing I think I should do" is not translating to code right now. And that's where I'm stuck.

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003158
Share on other sites

Display data file

 


<?php

$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo '<td>Update</td>';
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    $nRow = 0;
    foreach($row as $cell) {
      echo "<form action='' method='post'>";
      if ($cell == yes){
      echo '<td><input type="checkbox" name="option' . $nRow . '" checked="checked" /> </td>';
      $nRow++;
      }
      else if ($cell == no) {
      echo '<td><input type="checkbox" name="option' . $nRow . '" /></td>';
      $nRow++;
      }
        else if(is_string($row)) {
      echo "<td>$cell</td>";
      }
      else {
      echo '<td><input type="hidden" name="row" value="" /></td>';
          }
      echo '<td><input type="submit" value="submit" /></td>';
      echo "</form>";
   }
   echo "</tr>\n";
}
mysql_free_result($result);



?>

 

and the submit.php

 

<?php

// database connection here
$name = $_POST['name'];
$op1 = $_POST['option1'];
$op2 = $_POST['option2'];
$op3 = $_POST['option3'];

if (is_null($op1)) {
$op1 = no;
}
if (is_null($op2)) {
$op2 = no;
}
if (is_null($op3)) {
$op3 = no;
}

$q = "INSERT INTO options (id, name, option1, option2, option3) VALUES ('', '$name','$op1','$op2','$op3')";

if (mysql_query($q))
{
echo 'thanks!';
}
else (mysql_error());

?>

 

 

I haven't tested it, but it should work ^^

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003166
Share on other sites

Hmm... I updated the submit file, and that runs fine. But the display file is giving me five submit buttons per row.

 

I sort of see what you did there, and I'll try to take a stab at it.

 

While there's an active row from the query result, the $nRow is set to zero. The form begins; the nRow counter is increased if there's an input that is a yes or no. So the options will be numbered option0, option1, and option2? I'm not quite understanding the purpose of the hidden input though?

 

So each row is a form. The counter resets and goes to the next row.

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003183
Share on other sites

Yea, sorry about that I had to leave for acting class :P

 

Anyway, its good that you see my point ^^. And of course the $nRow should have been set to 1 instead of 0 ^^

So it says option1, 2 and 3 :)

 

And i forgot to add a pretty important thing in the script (as I said I was in a hurry :P). In the value of the hidden, the Id should be written.

 

So now would the purpose of the hidden be to make it possible for the script to locate what to edit.

Locating it by its Id ^^

 

An example would be:

$id = $_REQUEST['row'];
$query = "UPDATE options SET 'value' = 'another value' WHERE ('id' = '{$id}')";

 

You see my idea? :P

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003270
Share on other sites

Thank you so much!!  :D :D :D

 

I eventually figured out what the hidden value was doing, so I took apart my foreach loop in the table display, made it echo each cell, and pass the id and name in the post variable. Not the most efficient, but I'm still learning!

 

(I spent three hours trying to figure out why I was only getting a blank page, and it turned out that I had <?pph instead of <?php  :'( )

 

The resultant code, for all of posterity to see:

<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'panel';
$table = 'options';

$con = mysql_connect($db_host, $db_user, $db_pwd);

if (!con)
    die("Can't connect to database" . mysql_error());

if (!mysql_select_db($database))
    die("Can't select database");


$result = mysql_query("SELECT * FROM {$table}");
if (!$result)
    die("Query to show fields from table failed");


echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Option1</th>
<th>Option2</th>
<th>Option3</th>
<th>Update</th>
</tr>";

while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$name = $row['name'];
$op1 = $row['option1'];
$op2 = $row['option2'];
$op3 = $row['option3'];


if ($op1 == yes){
$op1 = '<input type="checkbox" name="option1" checked="checked" />';
}
else {
$op1 = '<input type="checkbox" name="option1" />';
}

if ($op2 == yes){
$op2 = '<input type="checkbox" name="option2" checked="checked" />';
}
else {
$op2 = '<input type="checkbox" name="option2" />';
}

if ($op3 == yes){
$op3 = '<input type="checkbox" name="option3" checked="checked" />';
}
else {
$op3 = '<input type="checkbox" name="option3" />';
}

echo '<tr><form action="updatevalues.php" method="post">';
echo '<td>' . $id . '<input type="hidden" name="id" value="' . $id . '" /></td>';
echo '<td>' . $name . ' <input type="hidden" name="name" value="' . $name . '" /></td>';
echo '<td>' . $op1 . '</td>';
echo '<td>' . $op2 . '</td>';
echo '<td>' . $op3 . '</td>';
echo '<td><input type="submit" value="submit" /></td>';
echo "</form></tr>";
}
echo "</table>";

mysql_close();
?>

 

updatevalues.php

<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'panel';
$table = 'options';

$con = mysql_connect($db_host, $db_user, $db_pwd);

if (!con)
    die("Can't connect to database" . mysql_error());

if (!mysql_select_db($database))
    die("Can't select database");

$postarray = $_POST;
$id = $_POST['id'];
$name = $_POST['name'];
$op1 = $_POST['option1'];
$op2 = $_POST['option2'];
$op3 = $_POST['option3'];

// To make sure every value is passing through
if (is_null($op1)){
$op1 = no;
}
else {
$op1 = yes;
}
if (is_null($op2)) {
$op2 = no;
}
else {
$op2 = yes;
}
if (is_null($op3)) {
$op3 = no;
}
else {
$op3 = yes;
}

// To see if POST values are being reteived
echo "$id <br />";
echo "$name <br />";
echo "$op1 <br />";
echo "$op2 <br />";
echo "$op3 <br />";

// Query time!
$query = mysql_query("SELECT * FROM options WHERE id='$id'");
$query_data = mysql_fetch_array($query);
if (!$query_data) {
    mysql_error();
}
// To see the resultant arrays
print_r($query_data);
echo '<br />';
print_r($postarray);

// Update!
if ($_POST != $query_data)
{
$q = "UPDATE options SET option1='$op1', option2='$op2', option3='$op3' WHERE id='$id'";
if (mysql_query($q)) {
	echo 'thanks!';
	}
else (mysql_error());
}
?>

Link to comment
https://forums.phpfreaks.com/topic/190122-checkboxes/#findComment-1003806
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.