save and retrive checkbox in various save files

I am developing a webtool with the purpose of doing a conversion of a XML(xmi) file to a SQL file, following specific rules (determined by the user) through checkboxes.

So basically the user should be able to save the checkbox used in a kind of savefile for the next time he visits the website/webtool he can load and thus get the selected checkboxes in the last previous use, understand?

Therefore, the user indicates a name for the save file to be able to load the save file in a posterior visit. I dont know how to code this :x NEDD HELP :

it seems odd to use a file to save “settings” why not write it to a database?

anyway - where is this file stored? on the server?
if you just need to load a file then look at fopen()

or load the whole file

[php]

<?php $homepage = file_get_contents('http://www.example.com/'); echo $homepage; ?>

[/php]
then use explode or something to split it up…
writing to a DB would be easier

Well, another way is to just load all of the checkboxes into an array and store the array.
Then, when you reload them, you would read the array and add the correct checkmarks to the fields.

It HTML FORM’s, a check box is something like:
If it is checked by the user this is read using $_POST and the value is checked or not.
To redisplay that, you do something like:
<input type=‘checkbox’ name=‘check1’<? if(checkboxarray[$thischeckboxnumber]==1{echo " checked";} ?>>
Note: I made up an array called checkboxarray, and a variable that would look thru the checkbox list called $thischeckboxnumber and also note the ?>> at the end was ?> for the PHP and > to end the checkbox field.

I am sure this looks crazy at first, but, you can placed the " checked" into the checkbox using PHP after retrieving the values from your storage area such as a database. Hope that made sense, if not show us your test code and we can help…

hi again :slight_smile: at first, sorry my english ;D
i undertand now a litle bit better what you say but i am stil, with problems. my problem is now, how to retrive from the database the values of the checkbox? :x

This is only a example that i found, and when this works i will implement in my situation.
Now with my codes, i can insert into database, a name (that will be the identifier), and the array of the checkboxes.
my codes are this:

[php]
Monday

Tuesday

Wednesday

[/php]

and

[php]include(‘config.inc’);

$name=$_POST[‘name’];
$days = implode(",",$_POST[‘days’]);
mysql_query(“INSERT INTO users (name,days) VALUES (’$name’,’$days’)”) or die("something went wrong during the registration. MySQL said: ".mysql_error());[/php]

So, that is what you wanted to do! Good!

Now to retrieve the checkboxes, you read the value into an array.
You just do a query against the database and get the value of the stored “days”.
Load it into a new array of $days , same as before.

This becomes a list of the previous checkbox values, in this case something like: (“Monday”, “Wednesday”)
In that example, I selected only two days… Now, to use this to put back into a list of checkboxes, you
will need to parse thru the array of days and mark each which exists as “checked”.
So, to display your checkbox using PHP, it would look like this:

And, if it is checked, it would be like this:

So, get the days from the database into your $days variable. Then, on each checkbox, use this for the displayed value of the checkbox:
<input type=“checkbox” name=“days[]” value=“Monday” <?if(in_array("Monday", $days)){echo " checked";}?>/>
This will check for “Monday” in the array “$days”. If found, it adds the " checked" option to the checkbox.
Please note that you will have to do this for each checkbox displayed. If Tuesday, if Wed… etc…

Hope that helps!

something like this or no?

[php]<input type=“checkbox” name=“days[]” value=“Monday” <?php if(in_array("Monday", $days)){echo "checked";}?>/>
Monday
<input type=“checkbox” name=“days[]” value=“Tuesday” <?php if(in_array("Tuesday", $days)){echo "checked";}?>/>
Tuesday
<input type=“checkbox” name=“days[]” value=“Wednesday” <?php if(in_array("Wednesday", $days)){echo "checked";}?>/>
Wednesday

[/php]

[php] include(‘config.inc’);
$name=$_POST[‘name1’];
$val2 = mysql_query( "SELECT days from users where name=’$name’ ") or die("something went wrong during the registration. MySQL said: ".mysql_error());
$days = array();
while($row = mysql_fetch_array($val2)) {
$days[] = $row[days];
}[/php]
i am lost in this part :X

Yes, except two things.
First, there MUST be a space before the “checked”. Should be " checked". This is inserted into your checkbox field and no space would run it together with the value…

Next, how you pull the $days out of the database should be changed. Something like this:
[php]
$name=$_POST[‘name1’];
$val2 = mysql_query( "SELECT days from users where name=’$name’ ") or die("something went wrong during the registration. MySQL said: ".mysql_error());
$days = mysql_fetch_array($val2));
[/php]
The $val2 is the the recordset of the query (one thing pulled, just “days”).
Then, you fetch all of the data in that recordset which is just one variable that you previously saved.
At that point $days should be the array that you saved. You will need to test this of course. I may have missed something, but, should do you well… Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service