我声明了这个var a = "p", b = "q", c = "r";
和var obj = {a : "x", b: "y", c:"z"};
我希望var obj = { "p": "x" , "q": "y", "r": "z"}
但我没有得到同样的结果。
答案 0 :(得分:2)
<form enctype="multipart/form-data" method="POST" action="">
<label>Your Profile Pic<input type="file" name="my_file" /></label>
<label><input type="submit" name="button" value="Submit" /></label>
</form>
<?php
if($_POST && isset($_FILES['my_file'])) {
$from_email = 'sanjay@.com'; //from mail, it is mandatory with some hosts
$recipient_email = 'sanjay@.com'; //recipient email (most cases it is your personal email)
//Capture POST data from HTML form and Sanitize them,
//$sender_name = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name
// $reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
// $subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); //get subject from HTML form
//$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message
/* //don't forget to validate empty fields
if(strlen($sender_name)<1) {
die('Name is too short or empty!');
}
*/
//Get uploaded file data
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error > 0) {
die('Upload error or No files uploaded');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
// $headers .= "From:".$from_email."\r\n";
//$headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
// $body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) {
//output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
}
?>
除非你像上面那样直接指向它,否则变量无法评估。
var obj = {};
obj[a] = "x"; // is what you want.
//Or
obj = {[a]: "x"}
答案 1 :(得分:2)
你可以像这样使用computed property:
var obj = {[a]: "x", [b]: "y", [c]: "z"}
或者您可以使用obj[a]
,因为其他人已建议使用a
中的值作为密钥的名称。
注意:计算出的属性在每个环境中都不会得到支持。请参阅&#34;浏览器兼容性&#34;我在上面发布的链接部分。
答案 2 :(得分:0)
查找
var a = "p", b = "q", c = "r";
var obj = {[a] : "x", [b]: "y", [c]:"z"};
console.log(obj);
&#13;
答案 3 :(得分:0)
var a = "p", b = "q", c = "r";
var obj = {a : "x", b: "y", c:"z"};
function getObj(obj){
var scope = this;
var result = {};
for(var key in obj){
result[eval(key)] = obj[key];
}
return result;
};
var requiredObj = getObj(obj);
//result - Object {p: "x", q: "y", r: "z"}
console.log(requiredObj);