php
  • javascript
  • post
  • 2011-11-17 49 views 1 likes 
    1

    我无法通过邮寄从以下功能提交一些隐藏的表单数据:的Javascript:POST到新窗口

    function displayCertificate() 
    { 
        window.open("../mailer.php","certificate","width=500,height=400,status=1") 
    } 
    var sHTML = ""; 
    sHTML += '<FORM id="quizResults" method="POST" action="/articulate/mailer.php" enctype="text/plain" target="certificate" onSubmit="displayCertificate()">'; 
    sHTML += '<INPUT TYPE="hidden" NAME="name" VALUE=\'' + g_arrResults[0].strStudentResponse + '\'>'; 
    sHTML += '<INPUT TYPE="hidden" NAME="email" VALUE=\'' + g_arrResults[1].strStudentResponse + '\'>'; 
    sHTML += '<br><input type="submit"><br>'; 
    sHTML += '</FORM>'; 
    alert(g_arrResults[0].strStudentResponse); 
    document.getElementById("divEmail").innerHTML = sHTML; 
    document.getElementById("quizResults").submit(); 
    

    我需要的是一个新的窗口,与我发送到信息打通它通过POST。窗口弹出正常,萤火虫显示POST成功,但数据似乎没有发送到我的新窗口。

    这里是我的mailer.php:

    <?php 
    print("name: ".htmlspecialchars($_POST['name'])); 
    print("email: ".htmlspecialchars($_POST['email'])); 
    ?> 
    

    很简单但它是输出:name: email:

    有人能看到为什么这会发生?我的解决方案必须是纯JavaScript(所以没有图书馆请)。

    谢谢。响应

    编辑到alessioalex:

    console.log(g_arrResults)输出是这样一个数组:

    [QuestionResult { nQuestionNum=1, strQuestion=" ", more...}, QuestionResult { nQuestionNum=2, strQuestion=" ", more...}, QuestionResult { nQuestionNum=3, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=4, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=5, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=6, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=7, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=8, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=9, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=10, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=11, strQuestion="A, B or C?", more...}, QuestionResult { nQuestionNum=12, strQuestion="A, B or C?", more...}] 
    

    这里是阵列中的第一对象的示例:

    bFound 
        false 
    
    dtmFinished 
        Date {Thu Nov 17 2011 12:54:29 GMT+0000 (GMT Standard Time)} 
    
    nPoints 
        "0" 
    
    nQuestionNum 
        1 
    
    strCorrectResponse 
        "&nbsp;" 
    
    strInteractionId 
        "I4e28818f-4014-418c-a6e4-863679014098" 
    
    strLatency 
        "1862" 
    
    strObjectiveId 
        "I4e28818f-4014-418c-a6e4-863679014098" 
    
    strQuestion 
        " " 
    
    strResult 
        "neutral" 
    
    strStudentResponse 
        "Peter Rabbit" 
    
    strType 
        "fillin" 
    
    +0

    什么是 var_dump($ _ POST); 给你吗?似乎它是服务器端的东西。 –

    +0

    @WillemMulder这是告诉我没有POST数据:'array(0){}' –

    +0

    你可以做一个console.log(g_arrResults);在JavaScript中?我想看看你发送了什么(如果有实际的数据)。 – alessioalex

    回答

    2

    删除window.open代码,并尝试将target =“_ blank”添加到您的表单中:

    <FORM target="_blank" id="quizResults" method="POST" ...> 
    
    +0

    在'window.open'之前已经尝试过了,相同的结果不起作用。 –

    +0

    你是否也从表单中删除了另一个目标attr(target =“certificate”)? – matthiasmullie

    +0

    也从您的窗体中删除enctype属性 - 测试和工作 – matthiasmullie

    0

    因为你试图从表单标签

    删除的onsubmit()方法

    然后尝试提交页面之前访问页面mailer.php它会工作

    +0

    不工作。 –

    1

    你需要注入形式弹出窗口中的文件,然后自动提交:

    function displayCertificate() 
    { 
        var win = window.open("","certificate","width=500,height=400,status=1"); 
        win.document.open(); 
        win.document.write('<FORM id="quizResults" method="POST" action="/articulate/mailer.php" enctype="text/plain" target="certificate" onSubmit="displayCertificate()">'); 
        win.document.write('<INPUT TYPE="hidden" NAME="name" VALUE=\'' + g_arrResults[0].strStudentResponse + '\'>'); 
        win.document.write('<INPUT TYPE="hidden" NAME="email" VALUE=\'' + g_arrResults[1].strStudentResponse + '\'>'); 
        win.document.write('<br><input type="submit"><br>'); 
        win.document.write('</FORM>'); 
        win.document.close(); 
        win.document.getElementById('quizResults').submit(); 
    } 
    

    Live test case(使用谷歌了一个例子)。

    相关问题