Javascript 不断向动作脚本发送值。怎么做?
我想反复向Javascript 不断向动作脚本发送值。怎么做?,javascript,php,Javascript,Php,我想反复向php脚本发送用户名和密码值。我该怎么做?我们使用submit按钮将值发送到动作脚本,但是如何将值自动发送到脚本,并且连续发送 <form method="post" action="processor.php"> <input type="username" value="suhail" /> <input type="password" value="secret_code" /> <input type="submit" /&
php
脚本发送用户名和密码值。我该怎么做?我们使用submit
按钮将值发送到动作脚本,但是如何将值自动发送到脚本,并且连续发送
<form method="post" action="processor.php">
<input type="username" value="suhail" />
<input type="password" value="secret_code" />
<input type="submit" />
</form>
试试这样的方法 JAVASCRIPT
<script language=javascript>
var int=self.setInterval(function(){send_data()},1000);
function send_data()
{
document.getElementById('my_form').submit()
}
</script>
var int=self.setInterval(函数(){send_data()},1000);
函数send_data()
{
document.getElementById(“我的表单”).submit()
}
HTML
<form method="post" id="my_form" action="processor.php">
<input type="username" value="suhail" />
<input type="password" value="secret_code" />
</form>
使用,您可以执行以下操作:
setInterval(function() {
$('form').ajaxSubmit();
}, 1000);
另一种解决方案是将表单定向到iframe,因此如果提交表单,它不会重新加载页面:
HTML:
var计数=100,i=0;
对于(i=0;i使用Ajax,将表单数据发送到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
您可以重复使用$.post或$.get或$.ajax请求来发送连续请求
$(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>
这是一个完整的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>
函数发送请求(用户名、密码){
var dataString='username='+username+“&password=“+password;
$.post('login.php',数据字符串,函数(响应){
//您可以在此处检查登录是否成功/失败
控制台日志(响应);
//再次发送请求,这将创建一个无限循环
发送请求(用户名、密码);
});
}
//开始发送请求
发送请求($('username').val(),$('password').val());
试试这个
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);
}
});
}
});
<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>
HTML:
$(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);
}
});
}
});
<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>
@Florent你能告诉我怎么做吗?一个解决方案是使用setTimeout()
使用javascript进行无限循环。从哪里发送?从浏览器发送?从终端发送?你为什么要这样做?为什么没有人怀疑你试图连续刷新登录表单?@VuckosetTimeout()
不适合无限循环,您应该改用setInterval()
。@Vucko仅仅因为某些东西工作正常并不意味着它是正确的方法。对于这样无限重复的任务,他们添加了setInterval()
。它将触发提交100次,但很可能只有一个或两个请求,因为浏览器会将用户从页面移开并停止脚本。Karl-Johan,浏览器将阻止连续表单提交,您可以通过任何方式设置此提交的时间延迟,以便浏览器不会阻止提交。不使用是方法否。请看@siledh提供的答案,这是正确的方法。
<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>