0

I am trying to set up a daily specials page for my website. I want the chefs to be able to fill out a form online and have the info they type into the form show up on the daily specials page on the website. I am very new to programming and I don't know PHP at all so I wonder if there is a simple way to do this that I just don't know about.

I can do this with a database easily enough however I can not set up a database for this site. Long story, don't ask.

I can use PHP to transfer data from the form online that is filled out by the chefs to the daily specials page - but the data does not stay there once the form is closed. I have tried using onSubmit return false on the form to keep it from being cleared but that did not work. Is there another way to do this or did I do it wrong?

I can use local storage to keep the info from the form and use jQuery to take the data from the form and put it on the daily specials page - but that only works for my machine. Is there anything like local storage for a server or for a domain name?

I tried embedding a google doc and a google sheet - Google's documentation reads that the sheets will automatically update on the website where they are embedded when changes are made to the sheet but that is not the case - or at least i have not managed to get it to work. Also the CSS is very limited so they do not look very good.

I can get info from an .html and .txt to my daily specials page using jQuery.load() - but how do i get the info onto the html or the txt file first? The text file needs to be on the server or on line so the chefs have access to it and the website has access to it. I can write to a .txt using PHP and an html form but I have not been able to put it all together.

Using these very simple pages:

form.html

<form action="action.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

action.php

   <?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
    $ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

load.html

<b>Projects:</b>
<ol id="cafe"></ol>

<script>
$( "#cafe" ).load( "mydata.txt" );

</script>

I have been able to fill out a form - write that data to a text file - grab that data off the text file and add it to an html page - but how do i get it to do all that and look good? I don't know how to set it up so that when it adds the data the text file it adds a specific piece of data into a specific div so then i can call that specific div onto my html page and add CSS to it? Do i have to make a new .txt file for each new piece of data?

I have 5 things that need to be updated per restaurant and i have 4 restaurants.

So I would have: restaurant A - I would need to update, 1. chef name. 2. special name, 3. special description, 4. kid's special name, 5. kid's special description. restaurant B - I would need to update, 1. chef name. 2. special name, 3. special description, 4. kid's special name, 5. kid's special description.
restaurant C - I would need to update, 1. chef name. 2. special name, 3. special description, 4. kid's special name, 5. kid's special description.
restaurant D - I would need to update, 1. chef name. 2. special name, 3. special description, 4. kid's special name, 5. kid's special description.

Nothing would need to be saved just replaced -

So right now i think i can do it if i write each new piece of data to a different txt file. so restaurant A would have chefname.txt, specialname.txt, specialdescription.txt, kidspecial.txt and kiddescription.tx.

Does anyone have ideas of a better way to do this? It seems like it would be a simple thing to do and I am just very new and don't know what I should be doing to accomplish this.

Any ideas or help would be greatly appreciated.

kat
  • 5
  • 3
  • Not sure what the issue is? If the HTML is stored at the server you can update the HTML directly. – guest271314 Feb 12 '19 at 16:55
  • If you don't need a .txt file you could store those form data as JSON and parse it on load.html. With JSON, you could then loop through and easily access each key and values. – Shawn Yap Feb 12 '19 at 16:59
  • While I agree that storing it as JSON is 'the' way to go (though many options exist...), my first concern is your SUBMIT page - if you have chefs, etc. entering data - what is stopping the general public (or a disgruntled, fired chef...) from also doing so??? You need to consider a lot more than 'how to store the data' here - some basic security issues need to be considered too! – Apps-n-Add-Ons Feb 12 '19 at 17:02
  • @CFPSupport Thank you so much for your help and I plan to make the form password protected. I just haven't gotten that far yet. But I do know I need to address that as well. – kat Feb 12 '19 at 18:59

2 Answers2

0

You can store your data at .txt files like you done, but you can use some format for it for example (first restaurant) chef name|special name|special description,|kid's special name| kid's special description. After that parse it by line by line and split it by special character (for example: | )

Bartek Smagacz
  • 140
  • 1
  • 5
  • You should give example code - not just a statement (I understand what you are getting at, but the OP likely won't) - it was stated "I don't know PHP at all", so if you are going to help, you should show the code....... – Apps-n-Add-Ons Feb 12 '19 at 17:05
  • @CFPSupport I don't know PHP either. I can write it at JS. But some how you are right. I should provide move informations. To split line after parse it: http://php.net/manual/en/function.explode.php Or JS if you want: https://www.w3schools.com/jsref/jsref_split.asp To parse a file: http://joombig.com/tutorials/php_tutorial/193-php-reading-file-line JS: https://stackoverflow.com/questions/23331546/how-to-use-javascript-to-read-local-text-file-and-read-line-by-line – Bartek Smagacz Feb 12 '19 at 17:15
0

The easiest way to go without using databases would be using JSON, stored in a simple .txt file. This however does need some more thinking into security. The most important issue being giving authorization to people to edit your form and hence modify your data.

But to answer your question, I would suggest something as follows:

form.html

<form action="action.php" method="POST">
    <select name="restaurant">
        <option value="1">Restaurant 1</option>
        <option value="2">Restaurant 2</option>
        <option value="3">Restaurant 3</option>
        <option value="4">Restaurant 4</option>
    </select>
    <input name="chef" type="text" />
    <input name="special_name" type="text" />
    <input name="special_descr" type="text" />
    <input name="child_name" type="text" />
    <input name="child_descr" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

action.php

<?php
if (isset($_POST['submit'])) {
    $data = json_decode(file_get_contents("data.txt"), true); // true for assoc
    $data[$_POST['restaurant']] = array(
        "chef" => $_POST['chef'],
        "special_name" => $_POST['special_name'],
        "special_descr" => $_POST['special_descr'],
        "child_name" => $_POST['child_name'],
        "child_descr" => $_POST['child_descr']
    );
    $newData = json_encode($data);
    file_put_contents("data.txt", $newData);
}
?>

load.php

<?php
$data = json_decode(file_get_contents("data.txt"), true); // true for assoc

foreach($data as $k => $v) {
    echo "<p>" . PHP_EOL;
    echo "Special Restaurant " . $k . ":<br />" . PHP_EOL;
    echo "Chef: " . $v['chef'] . "<br />" . PHP_EOL;
    echo "Name: " . $v['special_name'] . "<br />" . PHP_EOL;
    echo "Description: " . $v['special_descr'] . "<br />" . PHP_EOL;
    echo "Children: " . $v['child_name'] . "<br />" . PHP_EOL;
    echo "Description: " . $v['child_descr'] . "<br />" . PHP_EOL;
    echo "</p>" . PHP_EOL;
}
?>

Basically, in action.php you first decode the existing data in the data.txt file from JSON to a PHP readable multidimensional array, with keys 1 through 4 (for the restaurants), each containing an arry with the different values for chef, special name etc. You can then easily update this array, convert it to JSON again and store it.

In load.php (not load.html) you can again convert the JSON string to an array, loop through it and output the values per restaurant.

Now, this is just an example to illustrate how I would go about this, you should add error handling and authorization checks yourself.

nucleaR
  • 325
  • 2
  • 11