2013-10-21 16 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