gpt4 book ai didi

javascript - 表单提交时未调用 Ajax 函数

转载 作者:行者123 更新时间:2023-11-29 21:05:16 25 4
gpt4 key购买 nike

我是这方面的初学者,所以请原谅我是否愚蠢。

所以基本上我试图让一个 div 在使用 ajax 提交表单后重新加载来自 php 文件的信息,这就是我所拥有的。

索引.php

<script type="text/javascript">
$('#searchForm').submit(function(event){
$.ajax({
url: 'result.php',
type: 'post',
dataType:'html', //expect return data as html from server
data: $('#searchForm').serialize(),
success: function(response, textStatus, jqXHR){
$('#underInput').html(response);
},
error: function(jqXHR, textStatus, errorThrown){
console.log('error(s):'+textStatus, errorThrown);
}
});
});
</script>

<form class="form-group" id="searchForm" onsubmit="return false">
<div class="input-group">
<input id="search" class="form-control" name="user" type="text" placeholder="Enter instagram username">
<span class="input-group-btn">
<button class="btn btn-primary" id="submit" type="submit">Sök</button>
</span>
</div>
</form>
<div id="underInput"/>

结果.php

//dont want to show calculation for therate here
echo '<h2><?php echo $_POST["user"]?> can charge <?php echo $therate?>€ per
post</h2>';

点击提交后没有任何反应,为什么?

最佳答案

使用点击而不是提交

示例-

 $(document).ready(function(){
$('#submit').on("click", function(event){
$.ajax({
url: 'result.php',
type: 'post',
dataType:'html', //expect return data as html from server
data: $('#searchForm').serialize(),
success: function(response, textStatus, jqXHR){
$('#underInput').html(response);
},
error: function(jqXHR, textStatus, errorThrown){
console.log('error(s):'+textStatus, errorThrown);
}
});
});
});

然后用这段代码替换你的php代码

<?php
echo '<h2>'.$_POST["user"].' can charge '.$therate.' € per
post</h2>';
?>

关于javascript - 表单提交时未调用 Ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44408525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com