【问题标题】:form submit without refreshing page using ajax使用ajax提交表单而不刷新页面
【发布时间】:2018-12-23 07:07:15
【问题描述】:

我有旧代码,它有 jquery v1.9。 现在进行一些新的编辑,我需要提交表单而不使用 ajax 刷新页面。但它不起作用。 当我更改 jquery 的版本时,旧脚本不起作用。 我的代码在这里: 天.php:

<script>
$(document).ready(function(){
$('#formSubmit').click(function(){
$.post("day2.php",
{name: $('#name').val()},
    function(data){
    $('#response').html(data);}         
);});});
</script>   
<div>
<input type="text" id="name">
<button id="formSubmit">send</button>
<textarea id="response"></textarea>
</div>

和day2.php:

$name = $_POST['name'];
echo "response: " +$name;  

我不知道问题出在哪里!

【问题讨论】:

  • PHP 中的字符串连接是 . 而不是 +

标签: php jquery ajax forms form-submit


【解决方案1】:

尝试:

echo "response: " . $name;

代替:

echo "response: " +$name;  

【讨论】:

  • 另外回答错字问题也不会帮助未来的访客。当您有更多代表时,最好标记问题
  • @Nikoo 请右击页面 > Console 选项卡并将您遇到的错误发送到这里。
【解决方案2】:

实际问题在第二个文件中

echo "response: " +$name;

您使用 + 作为字符串连接运算符而不是 '.'

所以实际的代码是这样的

echo "response: " .$name;

【讨论】:

    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 2020-10-13
    • 2017-04-09
    • 1970-01-01
    • 2013-07-10
    • 2014-03-03
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多