PHP GET and POST - not working - php

I am a beginner with php and have the following code for getting values from a form and inserting it into a csv file as an array
PHP:
if(isset($_GET['submitted'])){
$csvData = [$_GET["contract"],$_GET["article"],$_GET["specs"]];
$fp = fopen("order.csv","a");
if($fp) {
fputcsv($fp,$csvData); // Write information to the file
fclose($fp); // Close the file
}
}
HTML :
<form action="add.php" method="GET" >
<label class="wsite-form-label" >Contract No <span class="form-required">*</span></label>
<div class="wsite-form-input-container">
<input name="contract" id='contract'>
</div>
<label class="wsite-form-label" >Article <span class="form-required">*</span></label>
<div class="wsite-form-input-container">
<input name="article">
</div>
<label class="wsite-form-label"> SPECS<span class="form-required">*</span></label>
<input name="specs">
<button type='submit' name="submitted" value="true" >Submit </button>
</form>
Have tried both get and post but still not able to get inserted value to my php. please help.

Here is the code
<?php
if(isset($_GET['submitted']))
{
$csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
$fp = fopen("order.csv","a");
if($fp)
fputcsv($fp,$csvData);
file fclose($fp); // Close the file
}
?>
<html>
<head>
<title>dfsdfdsdfsdfs</title>
</head>
<body>
<form action="test.php" method="GET" >
/* Your Form */
</form>
</body>
</html>

You can check like this:
if(!empty($_GET["contract"]) && !empty($_GET["article"]) && !empty($_GET["specs"])){
$csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
$fp = fopen("order.csv","a");
if($fp) {
fputcsv($fp,$csvData); // Write information to the file
fclose($fp); // Close the file
}
}

It seems that there is no errors in your code. You can check this by using
var_dump($csvData);
if it works then check the file pointer $fp
You can find more details about fopen() here

Related

How do you make button save form data on a csv file and redirect you to a new page at the same time?

I am trying to make a button that saves form data on a file and redirects you afterwards, but although the save function works just fine, the redirection doesn't happen. I've tried only href and formaction so far. Any suggestions?
Paste bin : pastebin
Thank you for your time reading this !
the code:
<html>
<head></head>
<footer>
<form method="post">
<input type="radio" id="html" name="fav_language" value="private_number" checked="checked">
<label for="html">private_number</label><br>
<input type="radio" id="css" name="fav_language" value="clieant_number">
<label for="css">clieant_number</label><br>
<input type="text" id="fname" name="fname" maxlength="11" autocomplete="off" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" required >
<button type="submit" name="BUY"><i class="arrow right"></i></button>
</form>
</footer>
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
}
?>
</body>
</html>
You can use PHP's header() function for this. Change the PHP part to this
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
}
?>

How can I write to a json file on a nginx web server using PHP?

I am trying to create a webpage where the user can input a username and ID and it updates a .json file with the provided input, but whenever I press the submit button, it redirects me to a blank page (i.e. example.com/index.php), and it doesn't update the JSON file.
I've tried:
Running the website locally on my PC
Updating example.com.conf file to run php through /var/run/php/php7.4-fpm.sock
I am using nginx version 1.18.0 on a Raspberry Pi 3B+
index.html, index.php and whitelist.json are all in the same directory.
HTML Code (index.html):
<form method="post" action="index.php">
<div class="group">
<input type="text" name="username" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Username</label>
</div>
<div class="group">
<input type="text" name="uuid" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>UUID</label>
</div>
<button id="btn"> <p id="btnText">Submit</p> </button>
</form>
PHP Code (index.php):
<?php
$data = file_get_contents('whitelist.json');
$json_arr = json_decode($data, true);
$json_arr[] = array('uuid'=>$_POST['uuid'], 'name'=>>$_POST['username']);
file_put_contents('whitelist.json', json_encode($json_arr));
?>
Firstly, this 'name'=>>$_POST['username'] should be 'name'=>$_POST['username'] (You have an extra > )
And secondly, I would rename the index.php file to something else like submit.php so it is not conflicting (don't forget to also change action="index.php" to the new file name)
If you do those two things the whitelist.json should update with the inputted values (but you will still have the blank page problem as described by me and #Barmar in the comments)
OR instead of having two separate index's for HTML & PHP, you could just have one file index.php, for both (what I recommend); and then you can create your whitelist.json in the same directory.
index.php Example:
<?php
# check if submit button was pressed and make sure both textboxes aren't empty
if(isset($_POST['submit']) && !empty($_POST['uuid']) && !empty($_POST['username'])){
# declare file path & name for .json file
$filepath = "whitelist.json";
# check if it exists
if(file_exists($filepath)){
# if it does, get contents and decode into array
$data = file_get_contents($filepath);
$json_arr = json_decode($data, true);
}
# add to existing array from above or create a new one with submitted data
$json_arr[] = array('uuid' => $_POST['uuid'], 'name' => $_POST['username']);
# open or create file & truncate it.
$fopen = fopen($filepath, "w");
# try to write data to said file; return $output regarding result
if($b = fwrite($fopen, json_encode($json_arr, JSON_PRETTY_PRINT))) $output = "Success! ${b} bytes written to: ${filepath}";
else $output = "Error!";
# close open file/handle
fclose($fopen);
}
?>
<html>
<head>
<title>Create ID</title>
</head>
<body>
<h2>Create ID</h2>
<form action="" method="POST" enctype="multipart/form-data">
<input required type="text" name="uuid" placeholder="UUID"><br>
<input required type="text" name="username" placeholder="Username">
<input type="submit" name="submit" value="Submit">
</form>
<!-- echo $output only if declared and different than NULL -->
<span><?php if(isset($output)) echo $output; ?></span>
</body>
</html>
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false; // serve the requested resource as-is.
} else {
echo "<p>Welcome to PHP</p>";
}
?>

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

Simple HTML/PHP Refuses to work

Here is poster dot php
<?php
$FILENAME = "poster.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
}
?>
Here is my HTML form
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I tried everything to make this work when i put it in a debugger it says the file should end on the first line. When I run the file all i get is a white screen. I'm in desperate need of help.
You are not writing anything after appending your form data in your poster.php file. You can get the file contents in the following way...
Html form:
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and in poster.php
<?php
$FILENAME = "poster.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'].PHP_EOL;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
echo file_get_contents("data.txt");
}
?>
As per requirement, write PHP code and HTML code inside a single file and remove the action attribute of the form in the following code ...
<?php
//$FILENAME = "poster.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'].PHP_EOL ;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
//echo file_get_contents("data.txt");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
action attribute of form should be file name where you submit your form.
<form action="action.php" method="post">
Here it is 'action.php' but you have to submit it on poster dot php.
please check it:
<form action="poster.php" method="post">
Form action will be your main file name(poster.php) and after form post, you can easily get all the form fields data on poster.php file.
you wotre this file name -> poster dot php means poster.php ?

How do you create a php file editor in php

I am trying to create a php file that can edit other php files on my website. I am able to do this except for when there is html in the php file that I want to edit. Since I am using a textarea to display/edit the php file contents, what I have built does not work when there is a textarea tag in the php file that I want to edit. What I have so far is below. The solution does not need to resemble this.
<?php
if ($_POST['file_text']){
file_put_contents($_POST['filename'], $_POST['file_text']);
$filename = $_POST['filename'];
echo "<script>
window.location = '_editor.php?filenm=$filename'
</script>";
}
else {
$myfilename = $_GET['filenm'];
if(file_exists($myfilename)){
$file_text= file_get_contents($myfilename);
}
echo "
<h3>$myfilename</h3>
<form name='input' action='_editor.php?filenm=$myfilename' method='post'>
<textarea name='contrib_entrybox' id='contrib_entrybox' rows='50' cols='180'>
$file_text
</textarea>";
?>
<?php
// configuration
$url = 'http://domain.com/backend/editor.php';
$yourfilePath = '/path/to/txt/file';
// check if form has been submitted
if (isset($_POST['text'])){
// save the text contents
file_put_contents($yourfilePath, $_POST['text']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($yourfilePath);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
<div id="sample">
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
</script>
<h4>
Second Textarea
</h4>
<textarea name="area2" style="width: 100%;">
Some Initial Content was in this textarea
</textarea><br />
</div>

Categories