在JavaScript中的PHP无法正常工作

时间:2014-01-03 11:39:58

标签: javascript php

我想发送电子邮件并将数据保存到数据库中。但保存的数据为name = <script>document.write(name)</script> 等等。请帮我解决这个问题。感谢

我尝试了很多方法来解决这个问题。事实上,我试图首先回显所有js变量,而不是将它们保存在php变量中,但未能这样做。

function submit_form(){
    name = document.getElementsByName("name")[0].value;
    email = document.getElementsByName("email")[0].value;
    f_email = document.getElementsByName("f_email")[0].value;
    filter = document.getElementsByName("filter")[0].value;
    if(name == ''){
        alert('Pleas Enter Your Name');
    }else if(email == ''){
        alert('Pleas Enter Your Email');
    }else if(!(validateEmail(email))){
        alert('You Must Enter a Valid Email Address');
    }else if(f_email == ''){
        alert("Pleas Enter the Reciever's Email");
    }else if(!(validateEmail(f_email))){
        alert('You Must Enter a Valid Email Address');
    }else if((filter != "islamabad") && (filter != "Islamabad")){
        alert("The Answer of the Question is Wrong.");
    }else{
        //query_submit();
        <?
            if(($e_id > 0)){//ef_id
             $name = "<script>document.write(name)</script>";

             $email = "<script>document.write(email)</script>";

             $f_email = "<script>document.write(f_email)</script>";

            $query ="INSERT INTO email_sent (name, email, f_email, unsubscribed,datetime)
                        VALUES 
                        (\"".$name."\", \"".$email."\", \"".$f_email."\", 0,NOW()) "; ?>
                <? if($result = $DB->DB_Query($query)){
                    //echo $name." ==ppp";
                    $query_select_m ="SELECT * FROM post_m where pj_id =".$pj_id; ?>
                    <? $result_select_m = $DB->DB_Query($query_select_m); 
                    if(($result_select_m)){
                        $m_title = $result_select_m[0]['m_title'];
                        $m_desc = $result_select_m[0]['m_description'];
                        $m_skills = $result_select_m[0]['m_skills'];
                        sending_email($fromname, $fromaddress, $toname, $toaddress, $subject, $message); 
                        //want to send email
                        ?>
                            alert("selection Query");
                        <?
                    }
                ?>
                         alert("Query Executed Properly!eeee");
                         //alert("<?echo json_encode($name); ?>");

                    <? }else{ ?>
                         alert("Unable to execute");
                    <? }
            }else{ ?>
                alert("Here we are");
            <? }
        ?>


        alert("Done");

        document.getElementsByName("name")[0].value = '';
        document.getElementsByName("email")[0].value = '';
        document.getElementsByName("f_email")[0].value = '';
        document.getElementsByName("filter")[0].value = '';
        //alert(name_for_e);
        //set_values(name_for_e, email_for_e, f_email_for_e);
    }
}

2 个答案:

答案 0 :(得分:4)

你不能这样做,php是一种服务器语言,Javascript是一种Cient语言。因此PHP将在发送到客户端之前执行,javascript将由客户端执行。

您必须通过发送或获取HTTP命令发送数据,验证数据,然后将其放入de数据库。

以下是更多信息: http://www.php.net/manual/en/tutorial.forms.php

答案 1 :(得分:0)

你不能用这种方式混合JavaScript和PHP。

在您的HTML表单上放置一个“onclick =”javascript:submit_form()“;在您的提交按钮上,当所有检查都通过”return true;“(而不是// query_submit();其余的)

同样在您的HTML表单中设置一个表单目标,您可以在其中捕获所有表单数据以使用PHP处理它们。以下是一个完整的示例:http://www.php.net/manual/de/tutorial.forms.php

这里有一个额外的验证:http://hibbard.eu/simple-form-validation-in-php/