drfals1307 2016-04-14 23:02
浏览 88
已采纳

PHP触发器JS然后将数据返回给PHP

I have a team email form which matches email of the sender and receiver using only names. I want to add a js prompt so that someone not on the list could use their email to send a message. here's what I have

 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>";

Heres the whole body

<?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='merrimack.scioly@gmail.com';
    $subject=  $fromsub . ' ' . 'sent you a message';
    $headers = 'From: ' . $fromsub . "
" .
               'Reply-To: ' . $from . "
";
    $mailcontent= 'Team Member: ' . $fromsub . "
"
                  .'Email: ' . $from . "
"
                  .'Message: '.$message. "
";
    $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='gamegalaxy64@gmail.com';
}
elseif (strstr($to, 'Anna Correia')) {
    $toaddress='anna.correia99@gmail.com';
}
elseif (strstr($to, 'Bryanna Bergevin')) {
    $toaddress='bubblebeauty25@gmail.com';
}
elseif (strstr($to, 'Brianna Bergevin')) {
    $toaddress='bubblebeauty25@gmail.com';
}
elseif (strstr($to, 'Caroline Pitten')) {
    $toaddress='crpitten@gmail.com';
}
elseif (strstr($to, 'Jason Provencher')) {
    $toaddress='jtproven@gmail.com';
}
elseif (strstr($to, 'Jordan Provencher')) {
    $toaddress='jrproven@gmail.com';
}
elseif (strstr($to, 'Ryan Bobbit')) {
    $toaddress='rbobbit7@gmail.com';
}
elseif (strstr($to, 'Sean Sylvester')) {
    $toaddress='sean.sylvester1@gmail.com';
}
elseif (strstr($to, 'Max Goddard')) {
    $toaddress='max.goddard6@gmail.com';
}
elseif (strstr($to, 'James Davis')) {
    $toaddress='James.Davis@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Mr Davis')) {
    $toaddress='James.Davis@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Mr. Davis')) {
    $toaddress='James.Davis@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Mrs Corliss')) {
    $toaddress='maryalice.corliss@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Mrs. Corliss')) {
    $toaddress='maryalice.corliss@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Sean Muller')) {
    $toaddress='seanp.muller@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Mr Muller')) {
    $toaddress='Sean.Muller@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Mr. Muller')) {
    $toaddress='Sean.Muller@merrimack.k12.nh.us';
}
elseif (strstr($to, 'Josh Waxman')) {
    $toaddress='jwembo21@gmail.com';
}
elseif (strstr($to, 'Carissa Yim')) {
    $toaddress='carrisayim@gmail.com';
}
elseif (strstr($to, 'John Pitten')) {
    $toaddress='John.pitten@tufts.edu';
}
elseif (strstr($to, 'Zev Sernik')) {
    $toaddress='zev.sernik@gmail.com';
}
elseif (strstr($to, 'YG')) {
    $toaddress='jrproven@gmail.com';
}
elseif (strstr($to, 'Tech Support')) {
    $toaddress='merrimack.scioly@gmail.com';
} 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
*/
    ?>
  • 写回答

1条回答 默认 最新

  • dongyue110702 2016-04-14 23:24
    关注

    This should be achieved through jquery's ajax, since php is executed on the server side it is always executed before the javascript and so you can't do it that easily. Here's my suggestion (without ajax):

    1. Create a new page(p1.php), send-mail.php, that simply receives the email parameters(to, from, message, ...) in either post or get and sends the email;
    2. Use your current page as a form only to get the values and set your form like so <form action = "p1.php" method = "post"> and then in that form display the inputs for the user to choose the email to send from.

    Here's how to use ajax, this is javascript:

    This could be a function triggered by a button click or other event...

    <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>
    

    And you could have something like this in your php page:

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

    After editing my answer:

    Once again, if you include JS in the php the JS will be printed to html but it will not be executed untill all the php code has

    Perhaps it would be best for you to evaluater whether there is a valid name or not and, if not, print a new form with an input asking for the other name, and when this form is submited it calls itself(the php page) again with the same GET parameters except for the new name the user inserted, this way you are continuously asking for a valid email/name until you have one, if the name is valid then simply send the email and don't print the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题