I have this Upload Form that when ‘Submit’ is selected the file uploads successfully.
Can some code be added so when “Submit” is selected the two fields I’ve added, (name & email address), get captured also?
I’ve added the two fields at line 92 & 97, but don’t know what else to add to make that work. Any guidance will be appreciated.
<?php
session_start();
require_once 'phps3integration_lib.php';
$message = "";
$message1= "";
if (@$_POST['submit'] != "") {
$allowed_ext = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docs", "zip", "mov", "MOV", "flv", "mp4", "3gp", "3GP");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < 104857600) && in_array($extension, $allowed_ext)) {
if ($_FILES["file"]["error"] > 0) {
//$message.="There is some error in upload, see: " . $_FILES["file"]["error"] . "<br>";//Enable this to see actual error
$message.="There is some error in upload. Please try after some time.";
} else {
$uploaddir = '../Upload/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
$uploaded_file = false;
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
$uploaded_file = $_FILES['file']['name'];
}
if ($uploaded_file != FALSE) {
$user_name = @$_POST['user_name'] != "" ? @$_POST['user_name'] : "Anonymous";
$form_data = array(
'file' => $uploaded_file,
'user_name' => $user_name,
'type' => 'file'
);
if(empty($_POST['agree']) || $_POST['agree'] != 'agree') {
$message1.= "Please read and agree to the Terms and Conditions To Proceed With Upload";
};
mysql_query("INSERT INTO `phps3files` (`id`, `file`, `user_name`, `type`) VALUES (NULL, '" . $uploaded_file . "', '" . $user_name . "', 'file')") or die(mysql_error());
$message.= "File Successfully Uploaded";
} else {
$message.="There is some error in upload. Please try after some time.";
}
}
} else {
$message.= "Invalid file, Please upload a gif/jpeg/jpg/png/pdf/doc/docs/zip/mov/flv/mp4/3gp file of maximum size 25 MB.";
}
}
?>
<?php
require_once 'header.php';
?>
<head>
<script>
var ids = ['input', 'message', 'button'];
var obj = {};
ids.forEach(function (v) {
obj[v] = document.getElementById(v);
});
obj.input.style.display = 'none';
obj.button.style.display = 'block';
obj.input.addEventListener('change', function () {
obj.message.innerText = this.value;
obj.message.style.display = 'block';
});
obj.button.addEventListener('click', function (e) {
e.preventDefault();
obj.input.click();
});
</script>
</head>
<html>
<div id="genericUp">
<br /><br /><br /><br />
<font size="6" color="#84786e"><b>Upload</b></font><br /><br />
<fieldset>
<form action="" method="post" enctype="multipart/form-data" onsubmit="if(document.getElementById('agree').checked) { return true; } else { alert('Please indicate that you have read and agree to the Terms by selecting the checkbox'); return false; }">
<div class="control-group">
<label for="file" class="control-label"><font size="6" color="#454545"><b>Choose a file to upload:</b></font></label><br /><br />
<!--<div class='controls'>-->
<input id="input" name="file" type="file" /></input>
<button id="button"><font size="3" color="#454545">Click To<br /> Select File</font></button>
<div id="message"><font size="3" color="#454545">No File Chosen</font></div>
</div>
<div>
<br />
<input class="form-control" type="text" name="name" maxlength="50" style="width:250px; height:20px; background-color:transparent; float:left; padding:0px 0px 0px 10px; margin:0px 0px 5px 0px; font-family: helvetica;
font-size: 12px; color:#cccccc; border:1px solid #cccccc"; placeholder="Name">
<br /><br />
</div>
<div>
<input class="form-control" type="text" name="email" maxlength="50" style="width:250px; height:20px; background-color:transparent; float:left; padding:0px 0px 0px 10px; margin:0px 0px 5px 0px; font-family: helvetica;
font-size: 12px; color:#cccccc; border:1px solid #cccccc"; placeholder="Email">
<br /><br /><br />
</div>
<div>
<input type="checkbox" name="checkbox" value="check" id="agree" vertical-align:top;/> By uploading a file you agree to these
<a href="../Terms.php" target="_blank"><font size="2" color="#000000" face="Arial"><u>Upload Terms/Agreement</u></a></font></label>
</div>
<br />
<div class="control-group">
<div class='controls'>
<label class="myLabel1">
<input type="submit" name="submit" value="Submit" class="btn" style="opacity: 0"><br /><br />
</label><br /><br />
</div>
</form>
</fieldset>
<script>
var ids = ['input', 'message', 'button'];
var obj = {};
ids.forEach(function (v) {
obj[v] = document.getElementById(v);
});
obj.input.style.display = 'none';
obj.button.style.display = 'inline-block';
obj.input.addEventListener('change', function () {
var filename = this.value.replace(/^.*[\\\/]/, '');
obj.message.innerHTML = filename;
obj.message.style.display = 'inline-block';
});
obj.button.addEventListener('click', function (e) {
e.preventDefault();
obj.input.click();
});
</script>
<?php
if ($message != "" || @$_SESSION['message'] != "") {
?>
<div class="alert alert-success">
<?php echo $message; ?>
<?php
echo @$_SESSION['message'];
@$_SESSION['message'] = '';
?>
</div>
<?php
}
?>
<div>
</div>
</div>
Yes, the question is what do you want to do with them once they have been captured?
If you want to store them in your database table, you need to add a bit of code to validate and sanitise them, then modify your SQL query to include the additional columns, or add another query if they’re to be stored in another table.
Thanks for your replies.
So, you say " the fields do get captured in the PHP’s $_POST array",
where does that information go?
How can I have it emailed?
I look forward to any additional guidance.
It doesn’t “go” anywhere - you have to change your code to do something with it, and if you don’t, it just disappears as soon as the PHP code ends, just the same as all the other form fields.
I haven’t done any email stuff so can’t help with code from scratch, but if you look back down the forum a bit there are plenty of threads where people are emailing form data around the place.
ETA - Here’s a thread with some PHP code that takes form fields and puts them in an email, have a look at whether that gives you enough pointers.
The order in which you define variables makes no difference. @Andres_Vaquero was referring to the PHP mail() function, as described in great detail in the link to the manual.
The function must have the right parameters, in the right order, for the function to work.
There are three mandatory parameters, in this order: to, subject and message.
Then there is an optional parameter for headers, which may have further optional, additional parameters.
You can’t put parameters into a function in any order, nor can you add parameters that are not valid parameters.
Read the information in the link to the manual for more detail on this.
Thanks for all the replies.
I have looked thru the link and don’t know how to change my code according to that.
The name & email fields are sent successfully, but still not the message text.
Although the headers is said to be optional, it does say this:-
Note:
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): “sendmail_from” not set in php.ini or custom “From:” header missing. The From header sets also Return-Path under Windows.
So maybe it also needs the “from” in the 4th header parameter, something like:-
I think the problem now might be in that the mail you’re sending is being filtered as spam by the email client. This can happen usually when your from address doesn’t match the domain name from where is being sent. I suggest looking into SMTP, there are free php libraries like phpMailer for sending emais with SMTP, which help to actually get your emails through
That could be it, some hosts don’t allow that as it could be used for mail spoofing. You could try changing the “from” address to the domain’s own address.
But it’s true that the mail() function can be finicky and unreliable in unpredictable ways. The mail libraries are considered a better option.
There’s a note in the PHP doc that says “Lines should not be larger than 70 characters” but doesn’t say what will happen if they are. Does the problem persist if you only have a short line of text in the message box?
I note the comments about using a valid “from” address and have said similar things in the past, however isn’t the problem that the email does arrive, but doesn’t have the message text inside it?
If you var_dump($_POST) at some convenient point, is the message text actually coming through from the form posting? For example, is the form being submitted by some JavaScript code that also needs to be modified in order to include the message field in the data being submitted? I have a vague memory that a poster in the past had that issue.
ETA - Now I’ve read back up the thread to your source code:
You’ve already got a div with the id="message" where you show form debug messages, in your first code. You can’t have two elements with the same id, but I’m not sure if it would cause a problem like this.