[英]Submit form input value on page load with ajax
I have form and a input in it and I want to send its value to my controller.I wanna send its value when a page load or refresh current page using ajax and jquery because my page reload when I write submit function in body tags onload event. 我有表单和输入,我想将其值发送到控制器。我想在页面加载或使用ajax和jquery刷新当前页面时发送其值,因为当我在body标签onload事件中编写Submit函数时页面会重新加载。 Here is my code: 这是我的代码:
<form method="post" id="counterForm" name="counterForm">
@csrf
<input type="number" name="count" id="count" value="{{ $visit->counts }}">
</form>
Script code: 脚本代码:
$(window).load(function(){
var n=document.getElementById("count").value;
$.ajax({
type:'POST',
url:'/count',
data:n,
});
});
My controller file : 我的控制器文件:
public function count(Request $request)
{
$value=($request->count)+1;
DB::table('visitorcounter')->update(['counts'=>$value]);
}
That was my code but its not working...Thanks. 那是我的代码,但是没有用……谢谢。
you can use .submit() : 您可以使用.submit():
Bind an event handler to the "submit" JavaScript event, or trigger that event on an element. 将事件处理程序绑定到“提交” JavaScript事件,或在元素上触发该事件。
you code would be like : 您的代码将像:
$(window).load(function() {
$('#counterForm').submit(function(){return true;});
});
Check out this example: : 看看这个例子 :
$(window).load(function(){
// this is the id of the form
var url = "results.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data:$( "#myformR" ).serialize(),
dataType: "html", //expect html to be returned
success: function (response) {
$("#prores").html(response);
}
});
});
I found it ... We can create a visitor counter very very simply by using a table with one integer column and a DB::table('test')->increment('count'); 我发现了它……我们可以非常简单地通过使用一个带有一个整数列的表和一个DB :: table('test')-> increment('count');创建访问者计数器。 in AppServiceProvider Boot function. 在AppServiceProvider启动功能中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.