PHP / Javascript无法在文件夹中上下移动

时间:2015-04-29 08:26:33

标签: javascript php jquery html css

所以我有一个简单的联系表格。在目录中有main.html和2个文件夹。 folder1包含js代码,folder2包含php文件。 js文件调用php文件来分析一些表单数据,php文件将它返回给js文件,最终在main.html上输出。

当从main.html调用JS文件时,没有数据从php文件发回。这是我在JS文件中使用的

 $.post("../folder2/contact_form.php", 

但是,如果我将main.html放入文件夹3并使用此行调用JS文件就可以了。

 <script src="../test/contact_form.js"></script>

基本上,当main.html在文件夹中时,代码可以正常工作。如果main.html位于根文件夹中,则在访问JS文件时不会调用php文件。同样,此代码可以工作,但仅当main.html位于文件夹中时。

main.html中

<html>
<head>
    <title>Simple jQuery Contact Form With Validation</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <link rel="stylesheet" href="../css/contact.css" />
    <script src="../test/contact_form.js"></script>

</head>
    <body> 
        <div id="mainform">         
            <form id="form">

                <input type="text" id="name" placeholder="Name"/><br/>

                <input type="text" id="email" placeholder="Email"/><br/>

                <input type="text" id="subject" placeholder="Subject"/><br/>

                <textarea id="message" placeholder="Message"></textarea><br/>

                <input type="button" id="submit" value="Send"/></td>

                <p id="returnmessage"></p><br/></td>

            </form>
        </div>
    </body>

contact_form.js

$(document).ready(function(){  
$("#submit").click(function(){
    var name = $("#name").val();
    var email = $("#email").val();
    var message = $("#message").val();
    var subject = $("#subject").val();

    $("#returnmessage").empty(); //To empty previous error/success message.
//checking for blank fields 
if(name==''||email==''||subject=='')
{
   alert("Please Fill Required Fields"); 
}
else{
// Returns successful data submission message when the entered information is stored in database.
$.post("../test2/contact_form.php",{ name1: name, email1: email, message1:message, subject1: subject},
   function(data) {
                $("#returnmessage").append(data);//Append returned message to message paragraph
                    if(data=="Your message was sent successfully! Thank-You for contacting us!"){
                        $("#form")[0].reset();//To reset form fields on success
                    }
            });
         }

});
});

contact_form.php

<?php
//Fetching Values from URL
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$subject = $_POST['subject1'];
//sanitizing email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
//After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you...! For Contacting Us.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Subject:' . $subject . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We will contact you as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// message lines should not exceed 70 characters (PHP rule), so wrap it
$sendmessage = wordwrap($sendmessage, 70);

// Send mail by PHP Mail Function
mail("asd@gmail.com", $subject, $sendmessage, $headers);
echo "Your message was sent successfully! Thank-You for contacting us!";

} else {
echo "<span>Please enter a valid email.</span>";
}

?>

1 个答案:

答案 0 :(得分:0)

如果你的文件夹结构是:

根/ main.html中

根/ CSS / contact.css

根/测试/ contact_form.js

根/ TEST2 / contact_form.php

然后你的网址不需要../ 例如:$.post("test2/contact_form.php",{},function(){})