HTML form field data in a text file - html

I want to retrieve the HTML form field data in a text file on my desktop.
I am having a HTML page containing the HTML form field box.
Further, I want to run a bash script taking the text file as a input.The bash script contains the command :
sed -f replacer input.txt > output.txt
I have to also include this output.txt file in another html form field.
Help me out.
Thank You.

Have html like this:
<form id="some" name="someName" method="post" action="/ur/url/to/post">
<input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
<iput type="submit" value="submit" class="submitClass"/>
</form>
Have a php script like this:
$myFile = "testFile.txt";
if(isset($_POST['fileWrite']) && !empty($_POST['fileWrite'])) {
$fileWrite = $_POST['fileWrite'];
}
if($fileWrite) {
$fh = fopen($myFile, 'a') or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
Thats it. Job is done.

Related

Path file issue when uploading csv file via laravel form to database

I want to upload a csv file to my database via a laravel form. I can only insert file if the file is stored into public folder. How can I correctly set path with fopen function?
When submitting form, my code uses only the csv that already exists in public folder and not the csv I try to upload. Except from the csv I also pass some other data to my controller. How can I alter code to receive form file and not public folder stored file?
1) Below is my form excerpt. At the bottom lines I try to upload the file.
<body>
<form method='post' enctype="multipart/form-data" action="/hard">
{{csrf_field()}}
<section>
<br>
<legend><i><b> Complete Data Below</b></i></legend>
<br>
</section>
<section>
Choose program:
<select name="sc" id="xaos">
<optgroup label="postgraduates">
#foreach($transport as $y)
<option value="{{$y->object_id}}">{{$y->object_name}}</option>
#endforeach
</optgroup>
</select>
</section>
<br>
<section>
ID number:
<input name='am' type='number' min="1000000" max="1999999" required="" oninvalid="this.setCustomValidity('1000000 < Value < 1999999')">
</section>
<br>
<section>
Select Language:
<select name="language" id="lang">
<option value="GR"> Greek</option>
<option value="EN"> English</option>
</select>
</section>
<br>
<section>
<label for="upload-file">select csv file</label>
<input type="file" name="upload-file">
</div>
<input type='submit' name='upload' value="Submit!">
</section>
</form>
<br>
<br>
</body>
2) And below is my controller code excerpt which handles file imported.
public function job(Request $p)
{
$a1 = $p -> get('sc');
$a2 = $p -> get('am');
$a3 = $p -> get('language');
$f_rownum = 0;
if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE)
{
while ( ($data = fgetcsv ( $handle, 1000, ';' )) !== FALSE )
{
$ac1=$data[0];
$ac2=iconv("Windows-1253", "UTF-8", $data[1]);
$ac3=iconv("Windows-1253", "UTF-8", $data[2]);
$ac4=iconv("Windows-1253", "UTF-8", $data[3]);
$ac5=$data[4];
...............
According to Laravel documentation you can store your csv file into the storage like this:
public function storeFile(Request $request)
{
$path = $request->file('upload-file')->store('{directoryName}');
return $path;
}
The above code store your file in storage and returns tha path which you can save it into your DB.
For retrieving the file, you can use this code if you want to force user to download the file:
return Storage::download('{pathWhichYouSavedInDB}');
or you can retrieve the content of the file by using this code:
$contents = Storage::get('{pathWhichYouSavedInDB}');
[I add the extra response which I wrote in the comments]
fopen requires a full-path url of the stored file to be able to open it. So you can get the file url and then pass it to the fopen function like this:
$url = Storage::url('file.jpg');
Or you can not to use fopen at all and just use the $contents which you already retrieved with
$contents = Storage::get('{pathWhichYouSavedInDB}');

How to use XQuery and HTML to upload files to MarkLogic?

As my question states I am trying to use XQuery and a simple HTML form to upload files to my MarkLogic local database. I already connected to a HTTP-server.
My code now looks like this:
Form:
<div id="content">
<form name="test" action="upload.xqy?uid={xdmp:random()}" method="post"
enctype="multipart/form-data">
<p><label>File to upload:
<input type="file" class="name" name="upload" size="50"/></label></p>
<p><input type="submit" value="Upload and Get Results"/></p>
</form>
</div>
upload.xqy:
let $filename := xdmp:get-request-field-filename("upload")
let $collection := "semansysdocs"
let $fileLocation := xdmp:get-request-path()
return
xdmp:document-load($fileLocation,
map:map() => map:with("uri", $filename)
=> map:with("permissions", xdmp:default-permissions())
=> map:with("collections", $collection)
)
The docs simply state to use xdmp:document-insert(), but I do not understand where.
Is there a way to specify where the file is coming from to get the $fileLocation, or do i need an other method to do this?
Thank you!
Your form is already sending both filename and file data. xdmp:get-request-field-filename('upload') returns the original file path as sent by the browser, and xdmp:get-request-field('upload') will get you the data. I think you are looking for something like:
let $filename := xdmp:get-request-field-filename("upload")
let $file := xdmp:get-request-field("upload")
let $collection := "semansysdocs"
return
xdmp:document-insert(
$filename,
$file,
xdmp:default-permissions(),
(xdmp:default-collections(), $collection)
)
HTH!

WWW::Mechanize::Firefox How do you extract the text within HTML element tags?

Good Day,
How do you print the text of an HTML tag with WWW::Mechanize::Firefox?
I have tried:
print $_->text, '/n' for $mech->selector('td.dataCell');
print $_->text(), '/n' for $mech->selector('td.dataCell');
print $_->{text}, '/n' for $mech->selector('td.dataCell');
print $_->content, '/n' for $mech->selector('td.dataCell');
Remember I do not want {innerhtml}, but that does work btw.
print $_->{text}, '/n' for $mech->selector('td.dataCell');
The above line does work, but output is just multiple /n
my $node = $mech->xpath('//td[#class="dataCell"]/text()');
print $node->{nodeValue};
Note that if you're retrieving text interspersed with other tags, like "Test_1" and "Test_3" in this example...
<html>
<body>
<form name="input" action="demo_form_action.asp" method="get">
<input name="testRadioButton" value="test 1" type="radio">Test_1<br>
<input name="testRadioButton" value="test 3" type="radio">Test_3<br>
<input value="Submit" type="submit">
</form>
</body>
</html>
You need to refer to them by their position within the tag (taking any newlines into account):
$node = $self->{mech}->xpath("//form/text()[2]", single=>1);
print $node->{nodeValue};
Which prints "Test_1".
I would do :
print $mech->xpath('//td[#class="dataCell"]/text()');
using a xpath expression
The only solution I have is to use:
my $element = $mech->selector('td.dataCell');
my $string = $element->{innerHTML};
And then formatting the html within each dataCell
Either:
$element->{textContent};
or
$element->{innerText};
will work.

HTML code to upload images

Can someone please provide me with some links (or source code) for me to research about how to upload images to a website (by the community) and for these images to be viewed online by the community.
I have searched via google, but have had no luck.
Thank you.
EDIT
Sorry, I have actually seen many examples to upload files. I am wanting (if possible) to be able to upload images, and then have them arranged in some kind of gallery so that the community can see/view them.
I am after a range of different technologies that are available on the WWW if possible.
Copy the below codes and save it as upload.php or anything.php (note it should be saved as php extension and should be run on apache server or any server supporting php).
<?php
if(isset($_REQUEST['submit']))
{
$filename= $_FILES["imgfile"]["name"];
if ((($_FILES["imgfile"]["type"] == "image/gif")|| ($_FILES["imgfile"]["type"] == "image/jpeg") || ($_FILES["imgfile"]["type"] == "image/png") || ($_FILES["imgfile"]["type"] == "image/pjpeg")) && ($_FILES["imgfile"]["size"] < 200000))
{
if(file_exists($_FILES["imgfile"]["name"]))
{
echo "File name exists.";
}
else
{
move_uploaded_file($_FILES["imgfile"]["tmp_name"],"uploads/$filename");
echo "Upload Successful . <a href='uploads/$filename'>Click here</a> to view the uploaded image";
}
}
else
{
echo "invalid file.";
}
}
else
{
?>
<form method="post" enctype="multipart/form-data">
File name:<input type="file" name="imgfile"><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
}
?>
And you should create two folders("uploads" and "tmp") in the parent directory (Where the script executes).
Now your upload script is ready. It's simply an upload script with which you can upload one image at a time.
Here's a good, basic start using PHP: http://www.w3schools.com/php/php_file_upload.asp
In order to get a list of files in a directory (assumes the file path is known ahead of time) you can do something like this:
$filePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'uploads';
$files = scandir($filePath);
If you wanted to filter this down to look for image files you could use something like this:
$filePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'uploads';
$files = scandir($filePath);
$imageFiles = array_values(preg_grep('/^.*?\.((gif)|(png)|(jpe?g))$/', $files));
And for a good guide on uploading the files you can check out http://www.exchangecore.com/blog/how-upload-files-html-php/

Submit form to calculate quadratic equation

Am just learning html. I need to write code that solves the quadratic equation formula. I tried php code embeding in html but am getting blank output. How do I get user values a, b, c and display conditional answers?
Here's a simple example of what you need to do. First make a HTML form:
<form method="post" action="index.php">
<input type="text" name="a" value="Enter 'a'" />
<input type="text" name="b" value="Enter 'b'" />
<input type="text" name="c" value="Enter 'c'" />
<input type="submit" name='calc' value="Calculate" />
</form>
There is your form. Now the calculations:
<?php
// Check if the form is submitted
if (isset($_POST['calc'])) {
//assign variables
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
//after assigning variables you can calculate your equation
$d = $b * $b - (4 * $a * $c);
$x1 = (-$b + sqrt($d)) / (2 * $a);
$x2 = (-$b - sqrt($d)) / (2 * $a);
echo "x<sub>1</sub> = {$x1} and x<sub>2</sub> = {$x2}";
} else {
// here you can put your HTML form
}
?>
You need to do more checks on it, but as I said before this is a simple example.
Edit: learn from the source , the official php site: http://php.net/manual/en/tutorial.forms.php
1.Create a form with the fields you want. <form method='post' ....>...</form>
2.The user submit the form and then write a PHP code which get the posted data ($_POST)
and manipulate it according to the quadratic equation formula.
3.Echo the result.
I have smaller example.
This file sends data from form to itself. When it sends something - result of condition
$_SERVER['REQUEST_METHOD']=='POST'
is true. If its true - server process code in "if" block. It assigns data sent from form to 2 variables, then adds them and store in "$sum" variable. Result is displayed.
<html>
<body>
<form method="POST">
<p>
A: <br />
<input name="number_a" type="text"></input>
</p>
<p>B: <br />
<input name="number_b" type="text"></input>
</p>
<p>
<input type="submit"/>
</p>
</form>
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') // process "if block", if form was sumbmitted
{
$a = $_POST['number_a'] ; // get first number form data sent by form to that file itself
$b = $_POST['number_b'] ; // get second number form data sent by form to that file itself
$sum = $a + $b; // calculate something
echo "A+B=" . $sum; // print this to html source, use "." (dot) for append text to another text/variable
}
?>
</body>
</html>
You need PHP server to test/use this! PHP file must be processed by web server, which creates page. Opening php file from disk will not work. If you need more explanations - ask for it in comments.