Input from site to text file

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,366
I am learning the html/php basics and I want to record input to a txt file. It can be used for the MCU with NJ60 internal or external ethernet adapter, so we can record who was using the site and turning on, off the oven so he burned the house remotely or so on.

Here is the code as much as I have reached, but I dont know hot to connect it toghether. I can record with the php file, and the html file gives me what I need as a form, how to put the text in the form as what I need to record by the php file.

html file
Code:
<!DOCTYPE html>
<html>
<body>
<center>

<br><br><br>
<p>Please enter your somecontent!</p>

<form id="frm1" action="/action_test.php">
  somecontent: <input type="text" name="somecontent" id="somecontent"><br>
  <input type="button" onclick="myFunction()" value="Update">
</form>

<script>
function myFunction() {
    var input = document.getElementById('somecontent'),
        fileName = input.value;
    if (fileName) {
        Recorder('somecontent', fileName);    // This is uncertain
    } else {
        alert('Please enter a somecontent!');
        input.focus();
    }
}
</script>
php file
Code:
<?php 
$filename = 'test.txt'; 
$somecontent = "asdasd";
// Let's make sure the file exists and is writable first. if (is_writable($filename)) { 

    // In our example we're opening $filename in append mode. 
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it. 
    if (!$handle = fopen($filename, 'a')) { 
         echo "Cannot open file ($filename)"; 
         exit; 
    } 

    // Write $somecontent to our opened file. 
    if (fwrite($handle, $somecontent) === FALSE) { 
        echo "Cannot write to file ($filename)"; 
        exit; 
    } 

    echo "Success, wrote ($somecontent) to file ($filename)"; 

    fclose($handle);
?>



<center>
</body>
</html>
 

wayneh

Joined Sep 9, 2010
18,104
It's been a while since I dabbled in this but I think the piece your missing is that the server will run your php script and send out to the user the appropriate HTML. So I think you'll want to embed the HTML within the php script.

Here's an example of a sign-in page. Sorry, I don't have time to simplify and clean it up to be more understandable but hopefully you'll get some ideas.

PHP:
<?php // signup.php

include("common.php");
include("db.php");

if (!isset($_POST['submitok'])):
    // Display the user signup form
    ?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title> New User Registration </title>
  <meta http-equiv="Content-Type"
    content="text/html; charset=iso-8859-1
</head>
<body>

<h3>New User Registration Form</h3>
<p><font color="orangered" size="+1"><tt><b>*</b></tt></font>
   indicates a required field</p>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table border="0" cellpadding="0" cellspacing="5">
    <tr>
        <td align="right">
            <p>User ID</p>
        </td>
        <td>
            <input name="newid" type="text" maxlength="100" size="25" />
            <font color="orangered" size="+1"><tt><b>*</b></tt></font>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p>Full Name</p>
        </td>
        <td>
            <input name="newname" type="text" maxlength="100" size="25" />
            <font color="orangered" size="+1"><tt><b>*</b></tt></font>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p>E-Mail Address</p>
        </td>
        <td>
            <input name="newemail" type="text" maxlength="100" size="25" />
            <font color="orangered" size="+1"><tt><b>*</b></tt></font>
        </td>
    </tr>
    <tr valign="top">
        <td align="right">
            <p>Other Notes</p>
        </td>
        <td>
            <textarea wrap="soft" name="newnotes" rows="5" cols="30"></textarea>
        </td>
    </tr>
    <tr>
        <td align="right" colspan="2">
            <hr noshade="noshade" />
            <input type="reset" value="Reset Form" />
            <input type="submit" name="submitok" value="   OK   " />
        </td>
    </tr>
</table>
</form>

</body>
</html>

    <?php
else:
    // Process signup submission
    dbConnect('sessions');

    if ($_POST['newid']=='' or $_POST['newname']==''
      or $_POST['newemail']=='') {
        error('One or more required fields were left blank.\\n'.
              'Please fill them in and try again.');
    }
   
    // Check for existing user with the new id
    $sql = "SELECT COUNT(*) FROM user WHERE userid = '$_POST[newid]'";
    $result = mysql_query($sql);
    if (!$result) {   
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact you@example.com.');
    }
    if (mysql_result($result,0,0)>0) {
        error('A user already exists with your chosen userid.\\n'.
              'Please try another.');
    }
   
    $newpass = substr(md5(time()),0,6);
   
    $sql = "INSERT INTO user SET
              userid = '$_POST[newid]',
              password = PASSWORD('$newpass'),
              fullname = '$_POST[newname]',
              email = '$_POST[newemail]',
              notes = '$_POST[newnotes]'";
    if (!mysql_query($sql))
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact you@example.com.\\n' . mysql_error());
             
    // Email the new password to the person.
    $message = "G'Day!

Your personal account for the Project Web Site
has been created! To log in, proceed to the
following address:

    http://www.example.com/

Your personal login ID and password are as
follows:

    userid: $_POST[newid]
    password: $newpass

You aren't stuck with this password! Your can
change it at any time after you have logged in.

If you have any problems, feel free to contact me at
<you@example.com>.

-Your Name
Your Site Webmaster
";

    mail($_POST['newemail'],"Your Password for the Project Website",
         $message, "From:Your Name <you@example.com>");
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title> Registration Complete </title>
      <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <p><strong>User registration successful!</strong></p>
    <p>Your userid and password have been emailed to
       <strong><?=$_POST['newemail']?></strong>, the email address
       you just provided in your registration form. To log in,
       click <a href="index.php">here</a> to return to the login
       page, and enter your new personal userid and password.</p>
    </body>
    </html>
    <?php
endif;
?>
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,366
At at a first glance these are the changes.

Code:
<?php

if (!isset($_POST['submitok'])):
    // Display the user signup form
    ?>
   
<!DOCTYPE html>
<html>
<body>
<center>

<br><br><br>
<p>Please enter your somecontent!</p>

<form id="frm1" action="/action_test.php">
  somecontent: <input type="text" name="somecontent" id="somecontent"><br>
  <input type="button" onclick="myFunction()" value="Update">
</form>


<script>
// JAVA SCRIPT CODE
function myFunction() {
    var input = document.getElementById('somecontent'),
        fileName = input.value;
    if (fileName) {
       
    } else {
        alert('Please enter a somecontent!');
        input.focus();
    }
}//JAVA SCRIPT CODE CLOSE
</script>

<center>
</body>
</html>


    <?php
else:
    // Process signup submission
    dbConnect('sessions');

$filename = 'test.txt'; 
$somecontent;
// Let's make sure the file exists and is writable first. if (is_writable($filename)) { 

    // In our example we're opening $filename in append mode. 
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it.    

    if (!$handle = fopen($filename, 'a')) { 
         echo "Cannot open file ($filename)"; 
         exit; 
    } 

    // Write $somecontent to our opened file. 
    if (fwrite($handle, $somecontent) === FALSE) { 
        echo "Cannot write to file ($filename)"; 
        exit; 
    } 

    echo "Success, wrote ($somecontent) to file ($filename)"; 

    fclose($handle);
   
    mail($_POST['newemail'],"Your Password for the Project Website",
    $message, "From:Your Name <you@example.com>");
?>

    <?php
endif;
?>
Maybe I will think of something more.
I also dont know how to activate the script on button press of the html/java script form.
Plus redirect to a new page after the text has been written to a file.
 

wayneh

Joined Sep 9, 2010
18,104
This is all pretty basic stuff. Unfortunately I haven't touched it for several years so I don't recall a lot. All I did back then was search for tutorials until I found several that I could understand and borrow ideas from. The things you are asking for should be covered in beginner tutorials.

I was specifically interested in using php for access control. So my setup would look at the username and password and if they were in an SQL database, that user was assigned an access control level that the site management deemed appropriate for that user. If not, they were granted public access until a higher level could be assigned.
 

xox

Joined Sep 8, 2017
936
Yep, you really would be best off diving into some tutorials. The finer points of PHP programming are tricky and it just doesn't suffice trying to explain it all here. That said:

  • Activating a script on submit is simply a matter of setting the form "action" to the filename of the PHP handler (as you seem to already have in place).
  • Redirecting to another page is done by setting the "Location" header to the page handler and then exiting the script. Only problem is, it doesn't work if you've already sent data to the browser. So...you either need to put all of the logic to detect the need for page redirects at the top of the file or else take another approach, such as buffering all output to the browser until the entire script completes its processing of everything. I personally prefer the second method; it's less efficient but allows quite a bit more freedom in the overall design of your program.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,366
Ok , everything is ready, now the only question left is how to redirect all traffic (like when someone types "google.com") tthe login page. It will be ran at my server, so when people connect to the house WiFi, first they will need to register or login, than they can use the WiFi and control everything in the house.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,366
Not 1? The idea is to run the PIC with an internal or external J60 adapter and use a local network similar to those for printing (HP laser jet WLAN). Whenever someone connects to the network, they will be asked for registration or log in, no matter what they type?
 

wayneh

Joined Sep 9, 2010
18,104
Not 1? The idea is to run the PIC with an internal or external J60 adapter and use a local network similar to those for printing (HP laser jet WLAN). Whenever someone connects to the network, they will be asked for registration or log in, no matter what they type?
Every request will include information about the user. If the information is lacking, or the user is not recognized in the database, then they get the login challenge screen. This is session control.

I don't understand your system. Usually the wifi router handles the chore of making sure only recognized users may access the network.
 
Top