这是我第一次尝试使用 php 类,以前从未这样做过,我很享受这个过程。
代码可能有点乱,如果您有任何建议,我将非常感激!
关于这个问题,我正在尝试,echo "$asd->getData()"
结果有点奇怪,我无法解释结果:
数组 ( [0] => 名称 [1] => 电子邮件 [2] => telemovel [3] => loja [4] => assunto [5] => mensagem [6] => localidade [7] => nserie [nome] => holder [email] => eeee [telemovel] => 913582837 [loja] => braga [assunto] => [mensagem] => [localidade] => [nserie] => )
我正在寻找以下结果:
Array([nome] => MyName [email] => emailHotmail [telemovel] => cellphone_here [loja] => store_name [assunto] => subject_here [mensagem] => Message_here [localidade] => Location [nserie] => Serial_number )
为什么会出现数组编号/键?
<?php
class Email{
private $data;
function Email(){
$this->data=array();
}
public function setData($d){
$this->data = $d;
}
public function valida(){
$verifica = true;
foreach ($this->data as $campo) {
$this->data[$campo] = $_GET[$campo];
if(!isset($_GET[$campo]) || empty($_GET[$campo])){
$verifica=true;
}
}
return $verifica;
}
public function getData(){
print_r($this->data);
}
public function sendEmail($data){
$to = ($this->data['loja']=="braga") ? "email1@email.com" : "email1@email.com";
$subject = $this->data['assunto'];//$campos[4];
$nserie = ($this->data['nserie']) ? '\r\n nº de série:'.$this->data['nserie'] : "";
$localidade = ($this->data['localidade']) ? '\r\n Localidade:'.$this->data['localidade'] : "";
$messages = $this->data['mensagem'];//$campos[5];
$message = $messages.$nserie.$localidade;
$message = htmlspecialchars_decode($message,ENT_QUOTES);//optional - I use encoding to POST data
$name = $this->data['nome'];
$from = $this->data['email'];//$campos[1];
$headers = "From:".$name." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit";
return mail($to, "=?utf-8?B?".base64_encode($subject)."?=", $message, $headers);
}
}
$asd = new Email();
$campos = array('nome','email','telemovel','loja','assunto','mensagem','localidade','nserie');
$asd->setData($campos);
$asd->sendEmail($data);
echo ($asd->valida($data)) ? "sent<br/>" : "not sent<br/>";
echo "<br/>".$asd->getData()."<br/>";
?>