表单提交后,Ajax没有正确填充目标DIV

时间:2013-07-31 17:25:19

标签: php ajax

我正在尝试为我拥有的网站实现表单插件。它应该很简单,但它拒绝表现得像我想要的那样。

这是一个div中的简单php电子邮件表单,一旦正确提交,就会被同一div中的成功或失败消息所取代。现在我的JS只是在表单上添加了消息。

我很惊讶,因为我认为我为这样做遇到了一些严重的麻烦,但我无法克服这个简单的错误。我想要的只是包含表格的DIV内部的响应消息。

这是包含PHP:

if (isset($_REQUEST['sender']) && isset($_REQUEST['message'])) {//if "sender" and "message" is filled out, proceed

    //check if the email address is invalid
    $mailcheck = spamcheck($_REQUEST['sender']);
    if ($mailcheck==FALSE) {
        echo "An error has occurred" . $_REQUEST['sender'] . " " . $_REQUEST['message'];
    }
    else {//send email
        $email = $_REQUEST['sender'] ;
        $subject = "This is the subject" ;
        $message = $_REQUEST['message'] ;
        mail("myself@gmail.com", $subject, $message, "From: $email" );
        echo "Your message was sent";
    }

}

function createForm($formID, $formName) {
    echo "<div id=\"" . $formID . "\" class=\"ajaxForm\" >" . PHP_EOL;
    echo "<form name =\"" . $formName . "\" method=\"post\" onsubmit=\"ajaxUpdate(\"" . $formID . "\",\"sandbox.php\"," . sender . "," . message . ");>" . PHP_EOL;
    echo "<input name=\"sender\" type=\"text\" placeholder=\"email\" required><br>" . PHP_EOL;
    echo "<br>" . PHP_EOL;
    echo "<textarea rows=\"6\" cols=\"40\" name=\"message\" placeholder=\"message\" required></textarea><br>" . PHP_EOL;
    echo "<input type=\"submit\" value=\"Send\" >" . PHP_EOL;
    echo "</form>" . PHP_EOL;
    echo "</div>" . PHP_EOL;
}


function spamcheck($field) {
    //filter_var() sanitizes the e-mail
    //address using FILTER_SANITIZE_EMAIL
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);

    //filter_var() validates the e-mail
    //address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}

和ajaxUpdate JS:

function ajaxUpdate(targetDIV, responsePHP, param1, param2) {

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(targetDIV).innerHTML = xmlhttp.responseText;
        }
    }
    var strContent = responsePHP + "?sender=" + param1 + "&message=" + param2;
    xmlhttp.open("POST", strContent, true);
    xmlhttp.send();
}

我通过包含js和php使用它,并使用createForm()函数生成表单。

请帮忙..

1 个答案:

答案 0 :(得分:0)

此行不正确:

echo "<form name =\"" . $formName . "\" method=\"post\" onsubmit=\"ajaxUpdate(\"" . $formID . "\",\"sandbox.php\"," . sender . "," . message . ");>" . PHP_EOL;

你必须在函数调用中使用单引号:

echo "<form name =\"" . $formName . "\" method=\"post\" onsubmit=\"ajaxUpdate('" . $formID . "','sandbox.php','" . sender . "','" . message . "');\">" . PHP_EOL;

要阻止提交,您只需返回false

echo "<form name =\"" . $formName . "\" method=\"post\" onsubmit=\"ajaxUpdate('" . $formID . "','sandbox.php','" . sender . "','" . message . "');return false;\">" . PHP_EOL;