How to pass variable one file to another using f write function - php

Hi how to pass variables from one file to another file with f write
i have a file name a.php
code is
<?php
if(isset($_POST['psub'])){
$myfile = fopen("an.php", "w") or die("Unable to open file!");
$pst = $_POST['pst'];
fwrite($myfile,$pst);
fclose($myfile);
}
?>
<form action="" method="post">
<input type="text" name="pst">
<input type="submit" name="psub" value="submit">
</form>
i need in an.php
$abc = "GET VALUE FROM $pst"

It looks like you approach this the wrong way.
Add an.php as an action in your form in a.php and, if you need to, redirect back to it after processing needed data.
Your a.php:
<form action="an.php" method="post">
<input type="text" name="pst">
<input type="submit" name="psub" value="submit">
</form>
and your an.php:
if( isset($_POST['psub']) && isset($_POST['pst']) ){
// do what you need with it
$abc = "GET VALUE FROM $_POST['pst']";
}
// some other work that you need
header("Location : a.php");

Related

This is the code to replace .txt files with each other so i would like to do this but i dont know where i am going wrong any tips?

this is the new code I am trying to replace the .txt file with the new car makes and model right now I am getting errors on those lines also I am entering the model and make but only adds to the txt files not replaces.
Notice: Undefined index: car_make in C:\Users\Ali\Desktop\USBWebserver v8.5\8.5\root\txtFiles\carcode.php on line 10
the contents of the.txt file are this '5bmw i5audii8' in one txt file but in the other, no contents are there but I only want one single.txt file with two forms
<?PHP
session_start();
?>
<body>
Car Make & Model
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type="submit" name="submit" value="submit">
</form>
</body>
<?PHP
$car = $_POST['car_make'];
$car2 = $_POST['car_model'];
$Data = "Car make & Model.txt";
$File = fopen($Data, 'a+');
$name = $car;
$name2 = $car2;
fwrite($File, $name );
fwrite($File, $name2);
fclose($File);
?>
<html>
<body>
Car Replacements
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type="submit" name="submit" value="submit">
</form>
</body>
<?PHP
if (!empty($_POST['car_make'])&& !empty($_POST['car_model']))
{
$new_car_make=$_POST['car_make'];
$new_car_model=$_POST['car_model'];
$Data="New Car make & Model.txt";
$haystack=file($Data);
for($i=0; $i<count($haystack);$i++)
{
$haystack[$i]=str_ireplace($new_car_make,$new_car_model,$haystack[$i]);
}
file_put_contents($Data, $haystack);
}
?>
To add data, yes your codes fopen with 'a+' parameter is correct.
To replace existing data, we may use fopen with 'w+' (which clear the existing data and do the data insertion)
Since you are trying to work in one single form, you need to tell the system which action (add / replace) you want to execute. The simplest way is to use a hidden field (e.g. xaction) to let the php knows what part you want to do.
On the other hand, I also suspect that you want the "replacement" to work by replacing a particular make and model with a new one (instead of clearing the whole txt file, if that is the case I suggest you first read the contents of the txt file and then do a string replacement and after that save the data)
Try the codes below:
<?php
// session_start();
?>
<body>
Car Make & Model (Add more data)
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type=hidden name=xaction value="Add">
<input type="submit" name="submit" value="submit">
</form>
</body>
<?php
if ($_REQUEST["xaction"]=="Add") {
$car = $_POST['car_make'];
$car2 = $_POST['car_model'];
$Data = "Car make & Model.txt";
$File = fopen($Data, 'a+');
$name = $car;
$name2 = $car2;
fwrite($File, " " );
fwrite($File, $name );
fwrite($File, " " );
fwrite($File, $name2);
fclose($File);}
?>
<html>
<body>
Car Replacements (replace whole file data with the following new data)
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type=hidden name=xaction value="Replace">
<input type="submit" name="submit" value="submit">
</form>
</body>
<?php
if ($_REQUEST["xaction"]=="Replace") {
$car = $_POST['car_make'];
$car2 = $_POST['car_model'];
$Data = "Car make & Model.txt";
$File = fopen($Data, 'w+');
$name = $car;
$name2 = $car2;
fwrite($File, " " );
fwrite($File, $name );
fwrite($File, " " );
fwrite($File, $name2);
fclose($File);
}
?>
For your information, I have commented the line "session_start()" because in your case you do not need to work on any session variables, but if other part of your code require to work on session variables, please un-comment this line)
Actually the manipulation of insertion / update / deletion of data should be performed by using a database. If you have time , please try to use database instead of using a txt file to do the job.

How can I get the output for form without the page changing

I am trying to get information from textarea to be converted to a .py file.
Here's my file https://jsfiddle.net/girlwhocancode/68mb49gq/
<form action=action.php method="post">
<center>
<textarea placeholder="Code you want to execute in python..."></textarea>
</center><br/>
<center><input type="submit" class="button_example"></center>
</form>
and this is my php file:
<?php
$path = #PATH;
if (isset($_POST['field1'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
For some reason my php isn't working and I have no idea how to keep the same webpage after submitting
Try this:
<form action="action.php" method="post">
<center>
<textarea name="script" placeholder="Code you want to execute in python..."></textarea> //added name
</center><br/>
<center><input name="submit" type="submit" class="button_example"></center>
</form> // added name submit
in php
<?php
$path = #PATH;
if (isset($_POST['submit'])) {
$fh = fopen($path,"a+");
$string = $_POST['script'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Remember that in php $_POST/$_GET keys are always your form field
names

Converting text to file and save it to in a folder

I have been trying to save input contact as a file in a folder but i don't know how to go about this using php.
<php
if(isset($_POST['s'])){
$neexe = $_POST['nameextention'];
$filecont = $_POST['fileBody'];
$folderpath = 'Files';
// I wan to save this as page.php with the body content inside so users can download it
}
?>
<form method="post">
<input type="text" name="nameextention" value="page.php"/>
<textarea name="fileBody"><?php if(isset($_HELP['please'])){echo 'i really need to get this done!!';}?></textarea>
<input type="submit" name="s" value="Save">
</form>
Easy. use fopen built-in function as below:
$myfile = fopen($neexe, "w") or die("Failed to open file!");
fwrite($myfile, $filecont);
fclose($myfile);

PHP write data from HTML to text file

I need to write some data from HTML form to data.txt file (the txt file have 777 permissions)
The HTML that I use is index.html:
<form action="index.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
And the PHP file is index.php:
<?php
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
But when I use the form to save the data, it show me a message box to download the index.php
what I am doing wrong?

PHP how to clear memory when refreshing

I am facing the following issue. I have a simple textarea where user will use to submit text which is subsequently written to a text file in the server. This is working.
But when I refresh the page it adds in the last added text into the text file again causing duplicate entries.
Any idea what I must do to prevent this? Below is the code I used for the textarea portion.
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
Pages that are loaded via POST will cause the browser to ask the user to resubmit the information to view the page resulting in the actions performed by that page happening again. If the pages is requested via GET and has variables in the querystring the same thing happens but silently (without the user being prompted to d it again).
The best to work around this is to use the POST/REDIRECT/GET pattern. I used it in an example about processing payments that I wrote for Authorize.Net. Hopefully that points you in the right direction.
A simpler so
You can just store a simple hash on session and regenerate it every time.
When the user reloads the page the php wont be executed.
<?php
if(isset($_POST['text_box']) && $_SESSION['formFix'] == $_POST['fix']) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<?php
$value = md5(rand(1,999999));
$_SESSION['formFix'] = $value;
?>
<input type="hidden" name="fix" value="<?= $value; ?>" />
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
ps: the order of the blocks will matter, so you need to invert em.
As John said, you need to redirect user after form submit.
fclose($fh);
// and
header("Location: success.php or where else");
exit;
Note: Your redirection won't work unless ob_start is not called before, cos your page contains html outputs.
// form.php
<?php ob_start(); ?>
<html>
<body>
<? if (isset($_GET['success'])): ?>
Submit OK! New submit
<? else: ?>
<form name="form" method="post" action="form.php">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
<? endif; ?>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
// send user
header("Location: form.php?success=1");
exit;
}
?>

Categories