Jump to content

How to save changes in form checkboxes.


Archimedees

Recommended Posts

 

 

In first.php I have a table inside a form with 2 inputs, names of server and a checkbox next to each server name. Clicked checkboxes reflect that the server is active and unclicked reflect that the server is inactive.

Now, when I manually click the checkboxes and press submit button the server names and active status are passes as variables to second.php. Here I just extract the variables from request array and search for them in a function that creates server list. When Server name is found the active status is set to true. I later write new config file and account for these changes. My problem is that when I click the checkboxes which are empty(setting them to active or vice versa) everything works just fine and new configuration file gets written. However when I refresh first.php the newly clicked checkboxes are unclicked. It is like the new variables are passed and used in second.php but first.php does not store these changes. What to do??? Please help.

 

Here's section of form showing inputs and variables that are passed in first.php

 

   

echo "<tr>\n";

    echo "  <td></td>\n";

    echo "  <td><input type=\"checkbox\" name=\"active_".$key."\" value=\"active\"";

    if ($value["active"]==true)

    {

      echo "checked";

    }

    echo "></td>\n";

          $strServer = $value["name"];

    echo "<input type=\"hidden\" name=\"MyServer[]\" value=\"$strServer\" />";

    echo "</tr>\n";

 

 

Link to comment
Share on other sites

basicly what you tranna do is if checkbox is already checked (and the data is in db) when you view the page it should be checked (as it is in db) and not just be blank, so your problem is how to get the value of the checkbox from the db am i right?

Link to comment
Share on other sites

Unless your saving the values of your vars to a database like gtal3x mentioned, or using session vars once php is done processing the file and has sent it to the webserver to be sent to the requesting browser, it scrubs all and starts afresh each time.

 

I recommend the session route.

 

at the top of each php file place: session_start();

 

file1.php

<?php
session_start()

if (isset($_SESSION['active_key'])
{
$value["active"] = $_SESSION['active_key'];
}

<form method=post action=file2.php>

echo "<form method=post action=file2.php><table>";
echo "<tr>\n";
    echo "  <td></td>\n";
    echo "  <td><input type=\"checkbox\" name=\"active_".$key."\" value=\"active\"";
    if ($value["active"]==true)
    {
      echo "checked";
    }
    echo "></td>\n";
          $strServer = $value["name"];
    echo "<input type=\"hidden\" name=\"MyServer[]\" value=\"$strServer\" />";
   echo "<input type=\"submit\" value="\submit to see session work"\>";
    echo "</tr>\n"
echo "</table></form>";

unset($_SESSION['active_key']);
?>

 

file2.php

<?php
session_start()

if (isset($_REQUEST['active_key']) {
   $_SESSION['active_key'] = $_REQUEST['active_key'];
}

echo "the active_key = ".$_SESSION['active_key'];

echo "<br>Now press the back button, to see that the value for $value is restored via sessions"

?>

 

note: this is not fully tested code, just threw together for example.

 

Q: why do i unset the session value at the bottom of file1.php?

Answer: well the reason is I have already extracted the value and used it to effect the checkbox, I need to remove it so if the submit to the file2.php and in that hit the back button again, I want the value in session var to be the latest one.

 

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.