Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I created a HTML form which should submit the inputted text into a .txt file with PHP, but it only creates a blank line and I don't know what the issue is.
Help is greatly appreciated!
<form class="form-inline validate" name="form1" method="post" action="signup.php" target="_blank" novalidate>
<div class="row no-gutter">
<div class="col-sm-9">
<input type="email" value="" name="mail" class="form-control" placeholder="Subscribe to our newsletter" required>
</div>
<div class="col-sm-3">
<input type="submit" value="SUBSCRIBE" name="Submit">
</div>
</div>
</form>
<?php
$username = $_POST['user'];
//the data
$data = "$email\n";
//open the file and choose the mode
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
//close the file
fclose($fh);
print "User Submitted";
?>
If you want to get mail change your php code to
<?php
if(isset($_POST['Submit'])){
$email = $_POST['mail'];
//the data
$data = "$email\n";
//open the file and choose the mode
$fh = fopen("users.txt", "a+");
fwrite($fh, $data);
//close the file
fclose($fh);
print "User Submitted";
}
?>
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
So I have a simple image uploading website I'm creating for a little project. It's all working just great, except for the file path column in the database.
For some reason it will sometimes include the variables i'm telling it to, and sometimes not.
$user = $_SESSION['user'];
//file properties
$fileName = $_FILES["uploadedImage"]["name"];
$fileType = $_FILES["uploadedImage"]["type"];
$fileSize = $_FILES["uploadedImage"]["size"];
$fileTempName = $_FILES["uploadedImage"]["tmp_name"];
$error = $_FILES["uploadedImage"]["error"];
$random = substr(md5(microtime()),rand(0,26),16); //create random characters to avoid name duplication.
$path = "uploads/" . $_SESSION['userName'] . $random . $fileName;
The path always contains "uploads/" but only sometimes would contain the session user name, random, and only rarely the filename, even on the same files. I'm echoing out these variables before I submit the form, and they are all correct before submitting the form and uploading to the database. All other columns are correctly filled when I submit the form.
if ( isset( $_POST['formSubmit'] )) {
//prevent SQL injections and invalid inputs
$title = trim($_POST['title']);
$title = strip_tags($title);
$title = htmlspecialchars($title);
$title = mysqli_real_escape_string($db, $title);
$description = trim($_POST['description']);
$description = strip_tags($description);
$description = htmlspecialchars($description);
$description = mysqli_real_escape_string($db, $description);
if (empty($title) || strlen($title) < 1) {
$titleError = "Title required.";
$formError = true;
}
if ($formError) {
$errorMessage = "Please fill out the upload form properly.";
} else {
$query = mysqli_query($db, "INSERT INTO IMAGE(imageID,userID,title,description,path)
VALUES('','$user','$title','$description','$path')");
Here's the form itself:
<form method="POST" action="<?php $_SERVER['PHP_SELF'] ?>">
<div class="row">
<div class="col-sm-12">
<span class="errorText"><?php echo $errorMessage; ?></span>
<br><br>
</div>
</div>
<div class="row">
<div class='col-sm-1'><!--spacer--></div>
<div class="col-sm-2">
<label for="title">Title:</label>
</div>
<div class="col-sm-6">
<input type="text" id="title" name="title" placeholder="Enter your image title here..." >
</div>
<div class="col-sm-2"><span class="errorText"><?php echo $titleError; ?></span></div>
<div class='col-sm-1'><!--spacer--></div>
</div>
<br>
<div class="row">
<div class='col-sm-1'><!--spacer--></div>
<div class="col-sm-2">
<label for="description">Description:</label>
</div>
<div class="col-sm-6">
<input type="text" id="description" name="description" placeholder="Describe your image here...">
</div>
<div class="col-sm-2"><span class="errorText"><?php echo $descriptionError; ?></span></div>
<div class='col-sm-1'><!--spacer--></div>
</div>
<br>
<br>
<input type="submit" value="Submit" name = "formSubmit" id="formSubmit" class="btn">
<br>
<br>
</form>
The form is missing enctype="multipart/form-data" attribute.
<form method="POST" enctype="multipart/form-data">
Also, even if this is unrelated to the question, I strongly recommend you to use MySQLi prepared statement. It help you avoid SQL injection without the need to manually escaping parameters.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to do a form that when the user types the info and then submits it saves it on a text file. But when I open the text file, it only displays the IP adress.... I can't figure out what I'm doing wrong with my code:
I already tried changing ยง_SERVER to $_POST but it doens't work
Thanks :)
HTML:
<form action="login.php" id="login.php" method="get">
<div class="imgcontainer">
<img src="zimbra.png" alt="Avatar" class="avatar">
</div>
<div class="bodypage">
<div class="font">
<div class="container">
<label>EEB2 Zimbra Email</label>
<input type="text" placeholder="Enter Email" name="email" id="email" required>
<label>Password</label>
<input type="password" placeholder="Enter Password" name="psw" id="psw" required>
<label>New Password</label>
<input type="password" placeholder="Enter New Password" name="newpsw" required id="newpassword">
<label>Confirm New Password</label>
<input type="password" placeholder="Enter New Password" name="newpsw" required id="confirmnewpassword">
</div>
<button type="submit">Change Password</button>
</div>
</form>
PHP:
<?php
$handle = fopen("Passwords.txt", "a");
$ip = $_SERVER['REMOTE_ADDR'];
$email = $_SERVER['email'];
$psw = $_SERVER['psw'];
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, PHP_EOL);
}
fwrite($handle, "IP=$ip");
fwrite($handle, "PASS=$psw");
fwrite($handle, "EMAIL=$email");
fclose($handle);
header ('Location: http://www.google.be/');
exit;
?>
EDIT: I'm not trying to get the new password, just the Email and the password.
Your method is $_GET so it would be $_GET['email']
There's no such thing as $_SERVER['email'], $_SERVER['psw'], etc. This is documented here.
Assuming that they have "name" attributes in the HTML form (which I can't, for some reason, see at Dropbox), you should find these values at $_POST['psw'], $_POST['email'], etc.
And I would add that you should use filter_input or some other mechanism to test these values before you put them onto your filesystem.
First if you are trying to save credentials(like passwords) you should not use method "GET" as in your code. Because when the form is submitted, as you have used GET method all the credentials will be shown on the URL. So it will be a security issue. Most developers prefer POST method over GET for form data handling.
As it says on W3schools "$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations."
Using POST method
Try Changing to this.(Use "$_POST[]" method just to capture the data you're trying to POST from the web Form. So this will work for you.
$ip = $_POST['REMOTE_ADDR'];
$email = $_POST['email'];
In order this to work for you you should change
USING GET method
But if you still would like to stick with the GET method you can simply change only these to $_GET['']
$ip = $_GET['REMOTE_ADDR'];
$email = $_GET['email'];
Hope that would help you! :D Good Luck
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
I have the following script that is supposed to take the input from two forms and then rewrite two different files with the input from the forms. When I put in text into the forms and run it I get no errors but neither of the files were changed at all. I have all the proper permissions set and correct file paths. I changed the paths in the code below so as not to show sensitive information to my server. I'm really scratching my head at this one since php is a fairly new language to me so any help is greatly appreciated.
HTML Code
<form name="editfront" action="save.php" method="post">
<div class="editareasmall">
<textarea rows="1" cols="150" id="title" name="title"></textarea>
<script>$('#title').load('../content/front/Title');</script>
</div>
<div class="contentheader">Content</div>
<div class="editareabig">
<textarea rows="30" cols="150" id="content" name="content"></textarea>
<script>$('#content').load('../content/front/Content');</script>
</div>
<input type="submit" value=" Save " class="save">
</form>
PHP Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$title = $_POST['title'];
$content = $_POST['content'];
$filenametitle = "../content/front/Title";
$filenamecontent = "../content/front/Content";
echo 'Form data has been saved';
$filetitle = fopen($filenametitle, "w") or die("can't open file");
$filecontent = fopen($filenamecontent, "w") or die("can't open file");
fwrite($filetitle,$title);
fwrite($filecontent,$content);
fclose($filetitle);
fclose($filecontent);
?>
When you submit the form, do you get your output message?
"Form data has been saved"
Are you sure that in php multi file opening is available?
Change your error reporting to -1. You will be informed about notice error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Is there any existing php code that will allow me to edit and save files from the browser?
Ex: a text area on the page site.com/edit.php would save to a specified file? I want to be able to edit files on the fly.
I am hosting the site on XAMPP and cant seem to get FTP working so this would be a big use if I need to edit a file and cant get access to the hosting computer.
This is the closest I have found to what I need, however cant seem to get it to work (the goal is to change 'change.php'):
<?php
if ($changefile) {
$slash = stripslashes($_POST['filetest']);
$filetochange = "change.php";
$filetochangeOpen = fopen($filetochange,"w") or die ("Error editing.");
fputs($filetochangeOpen,$slash);
fclose($filetochangeOpen) or die ("Error Closing File!");
}
?>
<form method=post action="">
<textarea rows="40" cols="60" name="filetest">
<?
// Implode CSS
$filetochange = "change.php";
print (implode("",file($filetochange)));
?>
</textarea><br />
<br />
<input type="submit" value="Change File" name="changefile">
</form>
www-data has to have write permission for the files or it won't work. Be careful with this. Either limit access to your own IPs (as you said) or put in a password protected folder with SSL.
<?php
$file = $_POST['file'];
$script = $_POST['script'];
if($file&&$script) {
$fp=fopen($file, "w");
fwrite($fp,$script);
fclose($fp);
}
?>
<form method="post">
<?php
if($file) {
?>
<textarea style="height: 90%; width: 100%;" name="script">
<?php
$fp=fopen($file,"r");
$t="";
while(!feof($fp)) {
$t.=fread($fp,1024);
}
fclose($fp);
print $t;
?>
</textarea>
<input type="hidden" name="file" value="<?=$file;?>">
<?php
}
else
{
?>
Name of file: <input type="text" name="file"><br />
<?php
}
?>
<br />
<input type="submit">
</form>