【问题标题】:Javascript: POST to new windowJavascript:发布到新窗口
【发布时间】:2011-11-17 12:35:19
【问题描述】:

我无法通过以下函数通过 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 发送给它的信息。窗口弹出很好,firebug显示POST成功但数据似乎没有发送到我的新窗口。

这是我的 ma​​iler.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"

【问题讨论】:

  • 什么是 var_dump($_POST);给你?好像是服务器端的东西。
  • @WillemMulder 这告诉我没有 POST 数据:array(0) { }
  • 你能做一个console.log(g_arrResults);在 JavaScript 中?我想看看你在发送什么(如果有实际数据)。
  • @alessioalex 添加到问题
  • 表单可以提交到“_blank”的目标。

标签: php javascript post


【解决方案1】:

删除 window.open 代码并尝试将 target="_blank" 添加到您的表单中:

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

【讨论】:

  • window.open之前已经试过了,同样的结果没有用。
  • 您是否还从表单中删除了另一个目标 attr (target="certificate")?
  • 并且还从您的表单中删除 enctype 属性 - 经过测试并且有效
  • @George 那么你已经做了一些事情来阻止它,mlitn 发布的内容是 100% 正确的。
【解决方案2】:

您需要将表单注入弹出窗口文档,然后自动提交:

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(以 Google 为例)。

【讨论】:

    【解决方案3】:

    因为您在提交页面之前尝试访问页面 mailer.php

    从表单标签中移除 onsubmit() 方法

    那就试试吧

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      相关资源
      最近更新 更多