[英]not able to submit form without page reloading
I have the following code that i wish to use to submit form without reloading the page, but it reloads the entire page and when checked in console the script.php page is also not getting executed. 我有以下代码,希望在不重新加载页面的情况下提交表单,但是它将重新加载整个页面,并且在控制台中检查script.php页面时也未执行。
Code on index.php page index.php页面上的代码
<script>
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var dataString = 'name=' + name;
console.log(dataString);
$.ajax({
type: "POST",
url: "script.php",
data: dataString,
success: function() {
$('#result').html(response);
}
});
return false;
});
});
</script>
<form method="post" name="form">
<input id="name" name="name" type="text" />
<input type="submit" value="Submit" class="submit" />
</form>
<div id="result"></div>
code on script.php page script.php页面上的代码
<?php
$name=$_POST['name'];
echo $name;
?>
Can anyone please tell how i can submit the form without reloading the page and also display the result from script.php page on index.php page 任何人都可以告诉我如何在不重新加载页面的情况下提交表单,并在index.php页面上显示script.php页面的结果
update your function. 更新您的功能。 pass event object in function.
在函数中传递事件对象。 and use event.preventDefault() to prevent default submit action.
并使用event.preventDefault()阻止默认的提交操作。
$(".submit").click(function(e) {
e.preventDefault();
try this 尝试这个
<script>
$(function() {
$(".submit").click(function(event) {
event.preventDefault();
var name = $("#name").val();
var dataString = 'name=' + name;
console.log(dataString);
$.ajax({
type: "POST",
url: "script.php",
data: dataString,
success: function() {
$('#result').html(response);
}
});
return false;
});
});
OR 要么
<input type="button" class="submit" />Submit
Do not mix form submit
and button click
. 不要混用表单
submit
和按钮click
。
If you are a click
event of button
then use type="button"
and if you are doing $("form").submit()
then use type="submit"
. 如果您是
button
的click
事件,则使用type="button"
;如果您正在执行$("form").submit()
则使用type="submit"
。
Also check for 同时检查
$(".submit").click(function(e) {
e.preventDefault();
and check your ajax
post data
, do 并检查您的
ajax
发布data
,
$.ajax({
type: "POST",
url: "script.php",
//changes made here
data: { name: $("#name").val()},
//changes made here. you have not written response
success: function(response) {
$('#result').html(response);
}
});
You need to send the data from the script.php page back to your index.php page. 您需要将数据从script.php页面发送回index.php页面。 Swich out the little bit of ajax for this:
为此,将一点ajax切掉:
$.ajax({
type: "POST",
url: "script.php",
data: "datastring",
dataType: "json",
success: function(dataType){
$("#result").html(dataType);
}
});
In your script.php: 在您的script.php中:
<?php
$name=$_POST['name'];
echo json_encode($name);
?>
Instead of listening to the input's click
event, you should listen to the form's submit
. 与其监听输入的
click
事件,不如监听表单的submit
。
<script>
$(function() {
$("#name-form").on('submit', function(e) {
e.preventDefault(); // prevent form submission
var name = $("#name").val();
var dataString = 'name=' + name;
console.log(dataString);
$.ajax({
type: "POST",
url: "script.php",
data: dataString,
success: function(response) {
$('#result').html(response);
}
});
});
});
</script>
<form id="name-form" method="post" name="form">
<input id="name" name="name" type="text" />
<input type="submit" value="Submit" class="submit" />
</form>
<div id="result"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.