2013-10-21 21 views
4

我想重複發送用戶名密碼的值到php腳本。我該怎麼做呢 ?就像將這些值發送到動作腳本一樣,我們使用submit按鈕,但是如何將這些值自動發送到腳本並且這些值會持續太久?將值連續發送到動作腳本。怎麼做?

<form method="post" action="processor.php"> 
    <input type="username" value="suhail" /> 
    <input type="password" value="secret_code" /> 
    <input type="submit" /> 
</form> 
+4

使用JavaScript。 – Florent

+0

@Florent你可以請告訴如何? –

+0

一個解決方案是使用'setTimeout()'使JavaScript無限循環。 – Vucko

回答

0

嘗試這樣的事情

JAVASCRIPT

<script language=javascript> 
     var int=self.setInterval(function(){send_data()},1000); 
     function send_data() 
     { 
      document.getElementById('my_form').submit() 
     } 
    </script> 

HTML

<form method="post" id="my_form" action="processor.php"> 
     <input type="username" value="suhail" /> 
     <input type="password" value="secret_code" /> 
    </form> 
1

使用jQuery form plugin,你可以做到以下幾點:

setInterval(function() { 
    $('form').ajaxSubmit(); 
}, 1000); 

另一種解決方案是到窗體目標的iframe,所以如果你提交表單,它不會重新加載頁面:

HTML:

<form id="myform" method="post" action="processor.php" target="frm"> 
    <input type="username" value="suhail" /> 
    <input type="password" value="secret_code" /> 
    <input type="submit" /> 
</form> 
<iframe name="frm" id="frm"></iframe> 

JS:

var form = document.getElementById('myform'); 
setInterval(function() { 
    form.submit(); 
}, 1000); 
0
<form id="myform" method="post" action="processor.php"> 
    <input type="username" value="suhail" /> 
    <input type="password" value="secret_code" /> 
    <input type="submit" /> 
</form> 

<script type="text/javascript"> 
var count=100,i=0; 
for(i=0;i<count;i++) { 
    document.getElementById('myform').submit(); 
} 
</script> 

這將提交表單100次

+0

它會觸發提交100次,但很可能只有一個或兩個請求,因爲瀏覽器會將用戶從頁面移開並停止腳本。 –

+0

Karl-Johan,瀏覽器將阻止連續的表單提交,您可以通過任何方式爲此提交設置時間延遲,以便瀏覽器不會阻止提交。 – srutheesh

+0

不使用此方法no。請看看@siledh提供的答案,而不是正確的做法。 –

0

使用Ajax,使用jQuery很容易。將表單數據發送到processor.php腳本:

var sendForm = function() { 
    $.ajax({ 
     type: 'post', 
     url: 'processor.php', 
     dataType: 'JSON', 
     data: { 
      username: $('#username').val(), 
      password: $('#password').val() 
     }, 
     success: function (data) { 
      // do something with the answer from server? 
     }, 
     error: function (data) { 
      // handle error 
     } 
    }); 
} 

所以,sendForm是表單數據發送到服務器的功能。現在,凌晨需要設置一個計時器,將反覆調用它:

window.setInterval(sendForm, 1000); // sends form data every 1000 ms 
0

您可能會.post的$或者$不用彷徨或$就反覆請求發送連續請求。

$(document).ready(function(){ 
    setInterval(function() { 
    var username = $("#username").val(); 
    var password = $("#password").val(); 
    var dataString = 'username='+username+"&password="+password; 
    $.post('login.php',dataString,function(response){ 
     //your code what you want to do of response 
     alert(response); 
    }); 
    }, 1000); 
}); 

和HTML代碼就像下面

<form method="post" action="processor.php"> 
    <input type="username" value="suhail" id="username"/> 
    <input type="password" value="secret_code" id="password"/> 
    <input type="submit" /> 
</form> 
0

這是一個完整的HTML文件做你想要什麼,閱讀評論。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 

<form method="post" action="processor.php"> 
    <input type="username" id="username" value="suhail" /> 
    <input type="password" id="password" value="secret_code" /> 
    <input type="submit" /> 
</form> 
<script> 
function send_request(username, password) { 
    var dataString = 'username='+username+"&password="+password; 
    $.post('login.php',dataString,function(response){ 
    // You can check if the login is success/fail here 
    console.log(response); 

    // Send the request again, this will create an infinity loop 
    send_request(username, password); 
    }); 
} 

// Start sending request 
send_request($('#username').val(), $('#password').val()); 
</script> 
0

試試這個,

JS:

$(document).ready(function(){ 
var int=self.setInterval(function(){statuscheck()},1000); 
function statuscheck() 
{ 
var username = $("#username").val(); 
    var password = $("#password").val(); 

    $.ajax({          
     type:"post", 
     url:"processor.php", 
     dataType: "html", 
     cache:false, 
     data:"&username="+username+"&password="+password, 
     success:function(response){ 
     alert(response); 
    } 
    }); 
} 
}); 

HTML:

<form method="post" action="processor.php"> 
    <input type="username" value="suhail" id="username"/> 
    <input type="password" value="secret_code" id="password"/> 
    <input type="submit" /> 
</form> 
+0

-1爲框架解決方案。 – Sebas