sending information into an IP through Html - javascript

I'm using making a web based microcontroller and now I want to send some sort of data to my microcontroller through a Html page .
can I do that in a raw Html file or I should use other programs like javascript or perhaps php
can every one give me some simple source code

Easiest way i could imagine is making a button, with a PHP script attached, and that PHP Execute a compiled binary that does something in your microcontroller.
Example:
index.html
<html>
<body>
<Form Name ="form1" Method ="POST" ACTION = "send.php">
First name: <input type="text" name="fname"><br>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">
</body>
</html>
send.php:
<?php
echo "<html><body>";
echo "The microcontroller say:"+exec('microcontrollerbinary $_POST['fname']');
echo "</body></html>";
?>
Your microcontroller should have a binary who read first argument from command line 'fname' and printout something

Related

grab values from input box with php

I was wondering if it would be possible to have a php file grab data from a html page with an input box, so that the user enters a word, and the php runs using that word in the script once the user hits enter. Any help would be appreciated.
Use a form with the method attribute set to ‘get’ or ‘post’ like this:
<form method=“post”>
<input type=“text” name=“test” value=“test” />
<button type=“submit”>submit</button>
</form>
Then on the same php page, use this to get the result, store it in a variable and output it:
<?php
$result = $_POST[‘test’];
echo $result;
?>
More information:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Your_first_HTML_form
Hope the formatting in this answer is ok, typed it out on my mobile phone.

Send a session variable from php to client side(HTML)

I am completely new to PHP and HTML. I want to send a session variable from PHP to client-side i,e HTML so that I can show which user is currently present.
If I just pass the variable to another php file present at the same location it does happen but I want to exchange the variable from server residing at local host to client at some other location Can any one help me in this
Thanks
let suppose you have a form in html
<form method="post" action="any.php">
<input type="text" name="fname" id="fname">
<input type="submit" value="Submit">
</form>
now in php you are getting input value
<?php
if isset($_POST["name"]){
$_SESSION["sessionname"] = $_POST["name"];
}
?>
so far you have taken input value from html and have stored in session with the name sessionname now you are going to show it in html again
<h1>welcome user : <?php echo $_SESSION["sessionname"]; ?> </h1>
you will need session_start(); on the very beginning of all php pages otherwise you will lose the scope of session variable.
for more information here is link wschools session

How to Link PHP Script with JavaScript and Bootstrap

I am trying to take a CSV file from the client side and read it in order to create arrays that can then be used in my original html file, that contains javascript, to create a dynamic table. My PHP code for parsing into arrays is:
<php?
$csvData = file_get_contents($fileName);
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
print_r($array);
?>
And my HTML (a simple file upload) is:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload CSV" name="submit">
but I am unsure as to how I should connect these pieces? Can the PHP script be in the same document as the HTML(which contains the JS to construct the table), or should it be called through a form?
Save your php script in a file eg. upload.php.
Then use the following code in your html:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload">
<input type="submit" value="Upload CSV" name="submit">
</form>
The easiest way to output the csv based on Reagan Gallant's suggestion above, is to print the data directly from upload.php. You can also add a header saying that you're serving csv, and let the browser decide what to do (save as file, display in browser):
header('Content-Type: text/csv');
print $csv;
Or you can use a javascript library such as JQuery to upload the file and return the result using ajax. This requires a little bit extra coding, but depending on your goal it might be worthwhile.

Ckeditor content retrieval using PHP

I've been trying to integrate ckeditor in my php website, and I've encountered the following issue.
Essentially, the content in ckeditor wouldn't appear in the $_POST variable after submitting the form.
I looked the issue up and apparently one has to update the form field with a small piece of code.
So I wrote the corresponding script and linked it to the submit button in order to get the result I want, but $_POST still shows up as empty.
I'm inexperienced with Javascript so the error probably lies there. Any ideas?
cktest.php:
<!DOCTYPE html>
<html>
<head>
<title>A Simple Page with CKEditor</title>
<!-- Make sure the path to CKEditor is correct. -->
<script src="http://localhost/ECLIPSEPHP/ckeditor/ckeditor.js"></script>
</head>
<body>
<form action = <?php echo $_SERVER['PHP_SELF']
?>>
<textarea name="test" id="test" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<input type = "submit" name = 'submitButton' id = 'submitButton' value = 'Submit'>
<script>
// Replace the <textarea id="test"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'test' );
</script>
<script type = "text/javascript" src = "http://localhost/ECLIPSEPHP/js/update.js"></script>
</form>
</body>
</html>
<?php
var_dump($_POST);
//echo $_POST['test'];
?>
The javascript supposed to handle the onclick event :
function updateAllMessageForms()
{
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].updateElement();
}
}
var submitButton = document.getElementById('submitButton');
submitButton.onclick = updateAllMessageForms;
There are quite a lot of problems with that code. The first thing to check is to add a method to that form tag: method="post".
See what <form action = <?php echo $_SERVER['PHP_SELF'] ?>> renders. It looks like it could be a wrong. I'm guessing it should be more like <form action="<?php echo $_SERVER['PHP_SELF'] ?>">.
Don't use ' for HTML attribute delimiters, use " instead: 'submitButton' --> "submitButton".
If you edit the updateElement a little: CKEDITOR.instances[instance].updateElement(); alert(1); - do you see the alert? If not, that code is not being called and you need to edit it so that it is.
Don't add spaces between your attribute name, the equals symbol and the value. That looks very strange and could be interpreted wrong or it could send Internet Explorer into quirks mode. Try to change this style: type = "submit" to type="submit" and keep up with that style.
Remember that it's often a good idea to look at the Rendered source in the browser to see what the browser actually gets. Happy coding!

Create a local file from user input and list existing files

I need to use the input from a user to create a filename.html on the server they are attached to. The program normally takes the input and writes it TO an existing html file. In this one case, I need to add the ability to create the file they are writing to before they begin writing to it.
While I am wishing :) I would like to start out by showing the user a directory of the html files are already there in case someone else has already created it.
The logic of the input would be to use the data input to create the file IF the file does not already exist. If it does exist, then they would open to the pre-existing file by that name.
This is an adaptation of "Microchat" php script which normally writes to msg.html.
It works fine as-is but this one project needs the added ability of creating multiple Name.html files and then letting the user continue as normal except they will be writing to the filename they chose or created rather than the using the generic msg.html. I have been working on the assumption that I will have to create a variable of their input to use for creating the file.
The entire script for Microchat is only 144 lines. But too long to post in its entirety
It can be viewed at www.phptoys.com. The area I need help with is this:
function createForm () {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center">
<tr><td colspan="2">Please eneter a nickname to login!</td></tr>
<tr><td>Your name: </td>
<td><input class="text" type="text" name="name" /></td></tr>
<tr><td colspan="2" align="center">
<input class="text" type="submit" name="submitBtn" value="Login" />
</td></tr>
</table>
</form>
<?php
This is normally used to create the users login. I have tried a few variations on the input to achieve a variable for creating a file but sadly, I know my limits. I doubt I will find a way without help.
I am not sure what you are trying to make. Possibly you can use database for the purpose but purpose is not clear to me so I don't know. By the way you can use following approach. Then you can do customization as per your requirements.
<?php
$fileName = "newHTMLFileName";
$path= "../yourFolder/".$fileName.".html";
if (!file_exists ($path))
{
$code = "<!DOCTYPE html><html><body><h1>Information</h1></body></html>";
$handle = fopen ($path, "w");
$write = fwrite ($handle,$code);
fclose($handle);
echo "created";
}
else
{
echo "already created";
}
?>

Categories

Resources