save input data in php to notepad.txt - php

<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.

The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help

Related

Can I pass a variable into an if(isset($_FILES['whatever'])) logical process?

I am new to PHP and am trying to create a page which will change/update/refresh depending on which form buttons are selected. This part works. The purpose of the site is to allow multiple file uploads of two types of data: PHOTO and MUSIC, along with uploads of textarea text - with each type being stored in a different location. The problem I've run into is in dedicating paths for each data type (for use with the move_uploaded_file command). I can set the paths in variables, but it seems the if(isset($_FILES['whatever'])) block of code cannot see those variables. I receive no errors (as you can see, I'm testing via localhost). How should I redesign my code to accomplish the objective? I'm assuming that the problem is in the way I'm approaching the project in general.
ini_set('display_errors',1);
$MasterFolder= __DIR__;
$BodyText = "";
$BodyHeader='<form action="" method="post">
<div id="NavContainer" style="margin: 4% auto;">
<input type="submit" name="Button" value="Photos">
<input type="submit" name="Button" value="Writings">
<input type="submit" name="Button" value="Music">
</div>
</form>';
switch (htmlspecialchars($_POST['Button'])) {
case 'Photos':
$ParentFolder = $MasterFolder . '/Photo';
$DestinationFolder = $ParentFolder . '/PhotoStore';
$BodyText='<!-- Whatever -->';
break;
case 'Writings':
$ParentFolder = $MasterFolder . '/Write';
$DestinationFolder = $ParentFolder . '/WriteStore';
$BodyText='<!-- Whatever -->';
break;
case 'Music':
$ParentFolder = $MasterFolder . '/Music';
$DestinationFolder = $ParentFolder . '/musicStore';
$BodyText='<div class="EntryForm">
<div class="TextLabel">Music</div>
<form action="" style="margin: 0 auto 4% auto;" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" multiple="multiple" />
</div>
<div class="EntryForm">
<div class="TextLabel">Description:</div>
<textarea name="MusicText"></textarea>
</div>
<div class="EntryForm">
<input type="submit" value="Publish Entry" />
</p>
</form>
</div> <!-- End ENTRYFORM div -->';
#echo $ParentFolder; ################## These two lines print the expected ##############
#echo "\n" . $DestinationFolder; #################### values #############################
break;
}
if (isset($_FILES['myfile'])) {
#echo $ParentFolder; ################## These two lines are equal to "" ##############
#echo "\n" . $DestinationFolder; ###### and I expected them to contain the values assigned in the switch/case statement above ########
foreach($_FILES['myfile']['tmp_name'] as $key => $tmp_name) {
$DFile = $DestinationFolder . htmlspecialchars($_FILES['myfile']['name'][$key]);
chmod($ParentFolder, 0777);
chmod($DestinationFolder, 0777);
move_uploaded_file($tmp_name, $DFile);
chmod($DFile, 0644);
}
chmod($DestinationFolder, 0650);
chmod($ParentFolder, 0650);
}
?>
<!DOCTYPE html>
<html lang="en">
<head><title>Publish</title>
</head>
<body>
<form action="" method="post">
<input type="submit" name="Button" value="Photos">
<input type="submit" name="Button" value="Writings">
<input type="submit" name="Button" value="Music">
</form>
<div class="EntryForm">
<?php echo $BodyText; ?>
</body>
</html>```
When you submit the second form (the one with the file) you don't re-send the 'Button' value. To circumvent this you could add a hidden input to your file-form above the 'Publish Entry'-submit-input like this:
<input type="hidden" name="Button" value="Music" />

delete button in PHP website behaving like static when clicked

i have a simple delete button in PHP which uses simple SQL query to delete the data from database, I have used the following code:
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}
<div style="margin-left:38%" class="clearfix">
<a href="https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory">
<button type="button" class="cancelbtn bemine">Cancel</button></a>
<form action="" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
the problem here is if I click the delete button, its not responding, nothing is happening. the button is like static. can anyone please tell me what could be wrong here
If any Javasscript is not involved, than just put form action.
Also send myid in the request or in URI.
please see below:
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="abc.php" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
You should add an action url/path to where your PHP file is located and add a Method to your form. Also, I noticed you deleting an entry where "name" is equal to $myid please check if that is correct.
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="url-to-server.php" method="post" >
<button type="submit" name="submit" class="deletebtn bemine">Delete</button>
</form>
</div>
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}

How to keep values in re-opened form?

I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?
Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

How to change form from get to post?

the links of this form have question marks:
This is a file: sidebar.php
echo
<form method='get'>
<div class='left'>
<div class='btn-group-vertical'>
<!-- Vertically Stck button group -->
<button 'submit' name='Service)name' class='btn btn-default'>Service Name</button>
<button type='submit' name='YYY_Service' class='btn btn-default'>YYY Service</button>
</div>
</form>;
?>
This is a part of index.php:
ini_set('session.save_path','/path/to/session');
session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$current_menu='empty.php'; //If no menu/button is selected get right column empty(empty.php)
if(isset($_REQUEST['Service'])){$current_menu='serviceform.php';}
if(isset($_REQUEST['YYY_Service'])){$current_menu='yyyform.php';}
This is showing URLS such as:
www.foo.com/index.php?Service=
www.foo.com/index.php?YYY_Service=
How should this form changed in order to show instead urls this way?
www.foo.com/serviceform.php
www.foo.com/yyyform.php
And this code in another file:
function Button_set($name, $newline)
{
if($newline){
?>
<br><br><input type="button" name="<?=$name?>" value="<?=$name?>" /input>
<?php
}
else if (!$newline){
?>
<input type="button" name="<?=$name?>" value="<?=$name?>" /input>
<?php
}
}
If I only change the form method to POST it doesn't work...
Change:
<form method="get">
to:
<form method="post">
Also add attribute "action" to site you want:
<form method="post" action="serviceform.php">
<form method="post" action="yyyform.php">
W3C link on form

if isset submit failed

$jepse = $_POST['slj'];
if(isset($jepse)){
$sql43 = "UPDATE notifications SET seen='1' WHERE touser='$myid' ";
if(mysqli_query($con, $sql43)){
}}
?>
<center>
<form action="#" method="POST">
<input type="submit" name="slj" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</center>
I have very stupid problem here... My submit button wont set ..... Don't know what is problem... I have many same things on different pages and with different names... But for this it wont work .... Anyone help?
:::::::::::::::::::UPDATE:::::::::::::::::::::::
I did it with moving update code to another file and at action set that file...
A submit button alone isn't enough to post a value. You should use a form field like a hidden input to post your data. I made this stupid mistake also.
<input type="hidden" name="slj" value="some-value">
You doesn't set any values to submit to the server.
You have to change the form like this:
<center>
<form action="" method="POST">
<input type="hidden" name="slj" value="1">
<input type="submit" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</center>
A value in the submit button is just the text printed on the button. You have to set another hidden input value and that will be sent.
Also it could be heplful to change the action to "".
your codding is expected to work as is if there are no spelling mistakes somewhere: Although the
defining a POST element before checking if it is set, the program will see it as it is not defined ref: $jepse = $_POST['slj']; but that should not be the main problem why your database is not updating since once you clicked the button it was then defined.
Also in your form <form action="#"... remove the #
i.e. if you are on the same page <form action=""...
or
i.e. be direct <form action="the-php-page.php"...
<?php
if(isset($_POST['slj'])){
$sql43 = "UPDATE `notifications` SET `seen`='1' WHERE `touser`='$myid' ";
if(mysqli_query($con, $sql43)){
echo "updated";
}
else
{
echo "Error updating record: " . mysqli_error($con);
}
}
?>
<span>
<form action="" method="POST">
<input type="submit" name="slj" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</span><br>
REGARDS
<center>
<form method="POST">
<input type="submit" name="slj" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</center>
<?php
$jepse = $_POST['slj'];
if(isset($jepse)){
echo "Working fine";
}
?>
Let mw know whether this code return "Working fine" or not

Categories