Unable to write to txt file in php - php

I'm trying to simply overwrite a txt file with a number entered in a text box in PHP. Both the PHP and text file are in the html directory of my apache2 server. Every time it executes it just displays the die() string. I also tried using fwrite() with the same results. Any help would be much appreciated.
<HTML>
<TITLE>Home Automation Interface</TITLE>
<BODY>
<H1>Air Conditioning Power(Relay 1)</H1>
<H2>Turn AC On/Off</H2>
<H3>
<p>
<form action="relayToggle.php" method="get">
<input type="submit" value="AC Power">
</form>
</p>
<form method= "post" action = "homeInter.php">
<p>
<label for="acTemp">AC temperature (F):</label><br/>
<input type="text" id="temp" name="temp">
</p>
<button type="submit" name="submit" value="send">Change Temperature</button>
</form>
<p><strong>
<?php
$temp= $_POST['temp'];
echo $temp;
file_put_contents("/var/www/html/temp.txt", (string)$temp) or die("Unable to open file!");
?>
}
}
?>
</strong>
</p>
</H3>
</BODY>
</HTML>

Wrap your PHP code Ina valid checker to see if form was submitted
Like this:
if ($_POST):
//All functions go here
endif;
If the file is in the same directory as your PHP file just change the directory to the file name.file Extension in your file_put_content()

Related

Seperating html form from php file

Previously I wrote a PHP code that includes an HTML form. So basically, the user inputs info in the HTML form and if all necessary input is filled, then the PHP file directs information to another PHP file. This is the HTML form I made This is the PHP File code (I will only put one variable of the form so that it's easier to understand:
<?php
session_start();
include('config/db_connect.php');
$Telephone = '';
$errors = array('Telephone' => '');
if(isset($_POST['submit'])){
// check Telephone
if(empty($_POST['Telephone'])){
$errors['Telephone'] = 'A Telephone number is required';
} else{
$Telephone = $_POST['Telephone'];
$_SESSION['Telephone'] = $Telephone;
if(!filter_var($Telephone, FILTER_VALIDATE_INT)){
$errors['Telephone'] = 'Telephone must be a valid Telephone number';
}
if(array_filter($errors)){
//echo 'errors in form';
} else {
// escape sql chars
header('Location: OTP.php');
}
} // end POST check
?>
<!DOCTYPE html>
<html>
<?php include('templates/header.php'); ?>
<section class="container grey-text">
<h4 class="center">Add a form</h4>
<body>
<form class="white" action="add.php" method="POST">
<label style="font-size: 16px">Your Telephone</label>
<input type="text" name="Telephone" value="<?php echo htmlspecialchars($Telephone) ?>">
<div class="red-text"><?php echo $errors['Telephone']; ?></div><p></p>
<div class="center">
<input type="submit" name="submit" value="Submit" class="btn brand z-depth-0">
</div>
</form>
</body>
</section>
<?php include('templates/footer.php'); ?>
</html>
Anyway, due to some reason, now I need to make the HTML form in a separate HTML file. So when users inputs the data in that form, the data has to be transferred to the PHP file I mentioned above and the innit['submit'] has to take place to further process it from there. In other words, previously the HTML form within the PHP file was filled and when 'submit' was clicked, the PHP file redirected it to another PHP file, but now the HTML form is in a HTML file itself and I want that when the form in that file has a click on 'submit', the PHP file is forced to follow the same procedure as it did previously. However, I cannot make the HTML file transfer the info to PHP and the PHP to directly go into the innit['submit'] function.
Here's the HTML part of the code (in the new HTML file):
<form action="work/add.php" method="POST">
<div id="Group_206">
<div id="Text_ca">
<span>電話號碼</span>
</div>
<input class="TextBox03" type="text" id="Telephone" name="Telephone">
<div id="Phone_name_position">
</div>
<div id="Group_159_m">
<input type="submit" value="Submit" >
</div>
The answer is as given in the comment by brombeer:
if(isset($_POST['submit'])){ will never be triggered, there is no
HTML element with name="submit" – brombeer

How to pass a variable from one file to another in PHP through a button and without and input

I have a search functionality that it works as shown below, but I would like to be able to pass a variable from a php file to another file without having an input like I have in my search function.
This is search functionality that works:
I have the following index.html file
<html>
<body>
<p>Search</p>
<form name"form1" method="post" action="searchresults.php">
<input name="search" type="text" size="40" maxlength="50">
<input type="submit" name="submit" value="Search">
</form>
</body>
</html>
and I have my searchresults.php file
<?php
$nameofcity = $_POST['search'];
?>
Something like this is what I would like to have but not sure how to do it.
index.php file
<html>
<body>
<p>Search</p>
<?php
$variabletopass = "London";
echo '<form name"form1" method="post" action="searchresults.php">';
echo '<input type="submit" name="submit" value="Search">';
</form>
?>
</body>
</html>
my searchresults.php file where I want the $nameofcity to be equal to the value of $variabletopass, in the example = London.
<?php
$nameofcity = $_POST['search'];
?>
You could try session. For example,
// index.php
$_SESSION['variabletopass'] = "London";
//searchresults.php
echo $_SESSION['variabletopass']; // Prints London
Hope this helps.

edit text from/to an XML file : it works in localhost but not on internet? (php, xml)

i try to edit the text of an XML file from my page edit.php. It works fine if i test it in "localhost", the xml file is updated as expected.
But, when i put the files on internet, and i try to edit the text from there, it seems to be ok, but then i refresh the page, and the text in the edit.php is still the same as before, and nothing has been updated. Do you know where this comes from? is there a problem with my "form" ?
here is my code :
<body>
<?php
/* READ */
$dom = new DomDocument();
$dom->load('edition.xml');
$_haut = $dom->getElementsByTagName('haut')->item(0);
$haut = $_haut->firstChild->nodeValue;
/* WRITE */
if ( isset($_POST['cache']) ){
$haut = stripslashes($_POST['haut']);
$_haut->firstChild->nodeValue = $haut;
$dom->save('edition.xml');
}
?>
<div>
<h4 style="float:left;">Update the text</h4>
<div style="clear:both;"></div>
<form method="post" action="edition.php">
<p>
<label for="textarea1">the text : </label><br />
<textarea rows="14" cols="80" name="haut" id="textarea1"><?php echo $haut ?></textarea>
</p>
<input type="hidden" id="cache" name="cache"/>
<p><input type="submit" value="Envoyer" /></p>
</form>
</div>
</body>
Thanks for your help
My best guess is the user php is running as does not have the permissions to do so.

Transform HTML form in PHP include file to single line for feeding JQuery methods

I have a some.php with some php processing which also has:
<form action="" method="post">
<input type="text" name="name" value="bleh" />
<input type="submit" name="submit" value="submit" />
</form>
I also have other.php with some jquery script:
<h1>form goes here</h1>
<script>
$("h1").replaceWith(" <?php include ('some.php'); ?> ");
</script>
The thing is... the replaceWith function is not working because apparently the include is replaced with the form with its spaces and line breaks which has in the some.php file. I tried trim before the include but it did not work.
How can I solve it if I want to keep the form in some.php with that format and line breaks (as opposed to making it one-liner)?
You could write the include to a hidden textarea and read it from there:
<textarea id="formhtml" style="display:none;"><?php include ('some.php'); ?></textarea>
And then replace the h1:
$("h1").replaceWith($("#formhtml").html());

My HTML/php page is presented as clear text in browser

I have this problem that my .php file is presented as clear text in the browser. I.e., the page is not transcoded in any way.
This is my html-fil with a form
<html>
<head>
</head>
<body>
<form id="myForm" action="commentNoJQ.php" method="post">
Name: <input type="text" name="name" />
Comment: <textarea name="comment"></textarea>
<input type="submit" value="Submit Comment" />
</form>
<p>Click on the button to load result.html file:</p>
<div id="stage" style="background-color:blue;">
STAGE
</div>
<input type="button" id="driver" value="Load Data" />
</body>
</html>
This is my .php file that the form is posting to. This file is presented as it is in the browser:
<html>
<head>
</head>
<body>
<?php
$myFile = "/Library/WebServer/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $_POST["name"]);
fclose($fh);
?>
</body>
</html>
What is the problem?
It's php-related sine when I create a normal html-file to post to, the normal file is processed as it should be.
This is how it looks in the browser:
I checked the logs (access_log, error_log), no info at all. Are there any other logs I should check?
Using Apache 2.2 and Mac OSX 10.6.4
Thanks in advance!
/Niklas
PHP is not properly configured on Apache I think.
UPDATE: links http://php.net/manual/en/configuration.changes.php
http://www.php.net/manual/en/install.macosx.php

Categories