Johnny Prescott
Johnny Prescott

Reputation: 271

PHP loading pre-saved form

I have a php page with a form in it.

The main idea here is to have a list of picture that you can select with a checkbox. Client will select the picture he want and submit the form.

I want the information of this form to be save in a file.(a .txt file would do it. I don't want a single table in mysql for each client if possible)

Then when the admin will log in the same page(or client let's say because he is not happy with his choice), I want the form to be filled exactly as it was saved previously.

Honestly, I don't know how to proceed to do that and the way it's gonna be loaded.

I could easily write in a .txt file with php variable but how to load these data and not getting any error if(only example) I remove a file in the folder?

Here's my form with the php code:

<form method="post" action="Exec.php">

<?php
$counter = 0;
foreach (glob("preview/*.jpg") as $file)
{
?>
<div class="photoscontainer">
<a class="big" href="<?php echo $file; ?>"><img src="<?php echo $file; ?>" width="100%" height="auto" alt="Photo <?php echo $file; ?>" /></a>
<br />
<input class="checkbox" type="checkbox" name="Photo <?php echo $counter; ?>" id="<?php echo pathinfo($file,PATHINFO_BASENAME) ; ?>" />
</div>
<?php
$counter++;
}
?>

</form>

Upvotes: 0

Views: 122

Answers (2)

Technobyte
Technobyte

Reputation: 32

It really doesn't change much either way using a file to store data or a database. A database is generally the preferred method, but you'd only need 1 table with 1 field for each client.

You will want to store the form data in an array/object that you hold in your database or file using serialize or json_encode.

For example:

<?php

if(!empty($_GET)){
$client_form_data = json_encode($_GET);
echo $client_form_data;
} 

?>

Then build your form using your stored values.

As far as errors go - you'll need to develop a method to catch potential errors using conditional statements.

Upvotes: 0

Shomz
Shomz

Reputation: 37711

You can save users answers as a custom field in the database (by JSONing the form inputs, for example), or even several new fields (one for each form field)... so definitely not using a separate table for each user. That's what databases are for.

If you still wish to use plain text, you can use a CSV (comma-separated values) file, .ini file, or whatever custom structure you want.

So, worthwhile Google searches:

And to avoid file missing errors - simply check if file_exists.

Upvotes: 1

Related Questions