2016-04-14 81 views
0

我有一个团队电子邮件表单,它只使用名称匹配发件人和接收者的电子邮件。我想添加一个js提示,以便不在列表中的人可以使用他们的电子邮件发送消息。这里就是我有PHP触发器JS然后返回数据到PHP

else { 
    echo "<script type='text/javascript'>prompt('$otheremail');</script>"; 
    $otheremail = file_get_contents("otheremail.json"); 

    #$otheremail=$_POST['data']; 
    $findat2= '@'; 
    $pos2=strpos($otheremail, $findat2); 
    while ($pos2 == false) { 
     $pos2 = 'enter a valid email'; 
     echo "<script type='text/javascript'>prompt('$pos2' , '');</script>"; 
    } 
} 
mail($toaddress, $subject, $message, $headers); 
$success=htmlspecialchars($_GET['to']); 
$success=ucwords($success); 
$valid= 'sent to' . ' ' . $success . ' ' . 'from a php server. Hit the back arrow' ; 
echo "<script type='text/javascript'>alert('$valid');</script>"; 

继承人全身

<?php 
    session_start(); 
    $name=htmlspecialchars($_GET['name']); 
    $name=trim($name); 
    $name=ucwords($name); 
    #talkes in the parameter of 'name' from html form then trims it and changes the first letter to uppercase [tells the name of sender] 
    $to=htmlspecialchars($_GET['to']); 
    $to=trim($to); 
    $to=ucwords($to); 
    #takes in the parameter of 'to' from html form then trims it and changes the first letter to uppercase [tells the name of recipient] 
    $from=htmlspecialchars($_GET['from']); 
    $from=trim($from); 
    #takes in the parameter of 'from' from html and trims it [tells the email of the sender] 
    $message=htmlspecialchars($_GET['message']); 
    $message=trim($message); 
    #takes in the message content from the html form 
    $fromsub=htmlspecialchars($_GET['name']); 
    #takes in the parameter of 'name' from the html form and inserts it in the subject field of the email 

    #below is where the email is formatted given the values from the html form 

    $toaddress='merri[email protected]'; 
    $subject= $fromsub . ' ' . 'sent you a message'; 
    $headers = 'From: ' . $fromsub . "\r\n" . 
       'Reply-To: ' . $from . "\r\n"; 
    $mailcontent= 'Team Member: ' . $fromsub . "\r\n" 
        .'Email: ' . $from . "\r\n" 
        .'Message: '.$message. "\r\n"; 
    $fromaddress= 'From: ' . $from; 

    mail($toaddress, $subject, $message); 

    $nwc=str_word_count($to); 
    #checks that word count of name is at least 2 
    while ($nwc < 2) { 
     $nwc = "Please enter a first and last name"; 
     echo "<script type='text/javascript'>alert('$nwc');</script>"; 
     exit; 
    } 
    #counts words in from sub 
    $fswc=str_word_count($fromsub); 
    #checks that fromsub word count is less at least two 
    while ($fswc < 2) { 
     $fswc = "Please enter a first and last name"; 
     echo "<script type='text/javascript'>alert('$fswc');</script>"; 
     exit; 
    } 

$passemail = htmlspecialchars($_GET['from']); 
$findat = '@'; 
$pos = strpos($passemail, $findat); 
    while ($pos === false) { 
$pos = "Enter a valid email"; 
     echo "<script type='text/javascript'>alert('$pos');</script>"; 
     exit; 
    } 
          /*INDEX OF NAMES --- WILL EVENTUALLY BECOME AN SQL DATABASE --- INDEX OF NAMES*/ 

if (strstr($to, 'Andrew Pitten')) { 
$toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Anna Correia')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Bryanna Bergevin')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Brianna Bergevin')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Caroline Pitten')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Jason Provencher')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Jordan Provencher')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Ryan Bobbit')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Sean Sylvester')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Max Goddard')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'James Davis')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Mr Davis')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Mr. Davis')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Mrs Corliss')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Mrs. Corliss')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Sean Muller')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Mr Muller')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Mr. Muller')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Josh Waxman')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Carissa Yim')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'John Pitten')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Zev Sernik')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'YG')) { 
    $toaddress='[email protected]'; 
} 
elseif (strstr($to, 'Tech Support')) { 
    $toaddress='[email protected]'; 
} else { 
    echo "<script type='text/javascript'>alert('$otheremail');</script>"; 
    $otheremail=$_POST['data']; 
    $findat2= '@'; 
    $pos2=strpos($otheremail, $findat2); 
    while ($pos2 == false) { 
     $pos2 = 'enter a valid email'; 
     echo "<script type='text/javascript'>prompt('$pos2' , '');</script>"; 
    } 
} 

mail($toaddress, $subject, $message, $headers); 
$success=htmlspecialchars($_GET['to']); 
$success=ucwords($success); 
$valid= 'sent to' . ' ' . $success . ' ' . 'from a php server. Hit the back arrow' ; 
echo "<script type='text/javascript'>alert('$valid');</script>"; 


/* 
#=================#===============================================================#============== 
|VARIABLE GLOSSARY|      DEFINITON         | 
#=================#===============================================================#============== 
|$fromsub   |takes in from and puts it into email subject     | 
|$fswc   |counts the words in fromsub and makes sure its two words  | 
|$name   |takes in the name of SENDER         | 
|$nwc    |checks that the RECIPIENT is first and last name    | 
|$toaddress  |email address of RECIPIENT from the variable $to    | 
|$to    |takes in 'to'             | 
|$subject   |email subject             | 
|$message   |takes in content from the field 'message'      | 
|$passeamail  |checks that email includes @         | 
|$headers   |email header             | 
|$success   |lets the user know that they have succesfully sent the email | 
|$findat   |set the $passemail value to pos        | 
|$pos    |set the $passemail value to check for @      | 
|$valid   |pushes a javascript alert using $success as a field variable | 
#=================#===============================================================#=============== 
OTHER NOTES 

else echo "I do not recognize this person"; 
nl2br somehwere 
*/ 
    ?> 

回答

0

应通过jquery's ajax来实现,因为PHP是在服务器端它始终是之前的JavaScript执行的执行,所以你不能这样做很容易。 这里是我的建议(无ajax):

  1. 创建一个新的页面(p1.php),发送给mail.php,简单地接收电子邮件 参数(以从,消息,...)中邮寄或发送 电子邮件;
  2. 仅将当前页面用作表单来获取值并设置您的表单,然后以该形式显示用户选择要发送的电子邮件的输入。

下面是如何使用ajax,这是JavaScript的:

这可以通过一个按钮单击或其他事件触发的功能...

<script> 
    function send_mail(to_email, message, headers){ 
     var from_email = prompt("Where from?"); 

     var form_data = {from:from_email ,to:to_email, message, headers};//add your own data 

     $.ajax({ 
      url : "send-email-page.php", 
      type: "POST", 
      data : form_data , 
      success: function(data) 
      { 
       //data is what the send-email-page will echo 
       //it could be a message for the user saying 'email sent' 
      } 
     }); 
    } 
</script> 

而且你可以有一些像这样在你的php页面:

<a href = "javascript: send_mail(<?php echo "$to,$message,$headers";?>)">Send from another address</a> 

再次

,如果包括JS在PHP中的JS将被打印到HTML,但它不会被所有的PHP代码有

也许会执行,直到:

编辑我的回答后最好为你评估一下是否有一个有效的名字,如果没有,打印一个带有输入的新表单,询问另一个名字,当这个表单被引用时,它再次调用它自己(php页面) GET参数除用户插入的新名称外,这种方式一直持续询问有效的电子邮件/姓名,直到您拥有一个有效的电子邮件/姓名,如果姓名有效,则只需发送电子邮件并不打印表格。

+0

那么现在我需要使用js电子邮件表单吗? –

+0

因为喜欢它通过不同的名称和,然后应用一个电子邮件地址,它 –

+0

你如何通过这些名字来生成的电子邮件地址(GET,POST,...)的页面有更多的代码呢? –