javascript - jquery表单提交后如何刷新特定div

标签 javascript php jquery html ajax

//我的表单提交jquery

$(document).ready(function() {
var form = $('#form1'); // contact form
var submit = $('#submit1'); // submit button
var alert = $('.alert1'); // alert div for show alert message

// form submit event
form.on('submit', function(e) {
    e.preventDefault(); // prevent default form submit
    // sending ajax request through jQuery
    $.ajax({
        url: 'giftcard_check.php', // form action url
        type: 'POST', // form submit method get/post
        dataType: 'html', // request type html/json/xml
        data: form.serialize(), // serialize form data 
        beforeSend: function() {
            alert.fadeOut();
            submit.html('Checking....'); // change submit button text
        },
        success: function(data) {
            alert.html(data).fadeIn(); // fade in response data
            form.trigger('reset'); // reset form
            submit.html('Apply'); // reset submit button text
        },
        error: function(e) {
            console.log(e)
        }
    });
});
});

//我的表单

<p>
   <form action="" method="post" id="form1">
      <input type="text" name="valuebox" placeholder="type your code" required /> 
      <button name="submit" class="button1" type="submit" id="submit1">Apply</button>
   </form>
</p>
<div class="alert1">Hello</div>

//giftcard_check.php

include("include/dbconnection.php");

dbconnect();

session_start();

$_SESSION['fromttl']    =   0;

$valuebox   =   $_POST['valuebox'];

$query      =   "SELECT * FROM db_coupon WHERE code='$valuebox' AND publish='1'";
$result     =   mysql_query($query);
$length     =   mysql_num_rows($result);
$rows       =   mysql_fetch_array($result);
$discount   =   $rows['discount'];

if($length == 1)
{
    $_SESSION['fromttl']    =   $discount;
    echo $_SESSION['fromttl'];
}
else
{
    $_SESSION['fromttl']    =   0;
    echo "Invalid Gift Card!";
}

我的问题是如何刷新特定的div(即,

 <div id="show"><?php echo $_SESSION['fromttl']; ?></div>

)表单提交后立即。

我当前的结果在表单提交后没有刷新特定的 div。我不想刷新整个页面,只想刷新一个特定的 div。如果我刷新整个页面,div 将被刷新。

有什么解决办法吗?我被困在这里了。

最佳答案

您应该尝试一下,在 form.on('submit',... 的末尾添加 return false; 它不会提交您的表单请求您将停留在同一页面上:

//我的表单提交jquery

$(document).ready(function() {
var form = $('#form1'); // contact form
var submit = $('#submit1'); // submit button
var alert = $('.alert1'); // alert div for show alert message

// form submit event
form.on('submit', function(e) {
    e.preventDefault(); // prevent default form submit
    // sending ajax request through jQuery
    $.ajax({
        url: 'giftcard_check.php', // form action url
        type: 'POST', // form submit method get/post
        dataType: 'html', // request type html/json/xml
        data: form.serialize(), // serialize form data 
        beforeSend: function() {
            alert.fadeOut();
            submit.html('Checking....'); // change submit button text
        },
        success: function(data) {
            alert.html(data).fadeIn(); // fade in response data
            form.trigger('reset'); // reset form
            submit.html('Apply'); // reset submit button text
            if(data != 'Invalid Gift Card!') {
              $('div.show').html(data);
            }
        },
        error: function(e) {
            console.log(e)
        }
    });
   return false;  //Will not active the form submission
});

});

//我的表单

<p>
   <form action="" method="post" id="form1">
      <input type="text" name="valuebox" placeholder="type your code" required /> 
      <button name="submit" class="button1" type="submit" id="submit1">Apply</button>
   </form>
</p>
<div class="show"></div>
<div class="alert1">Hello</div>

关于javascript - jquery表单提交后如何刷新特定div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296998/

相关文章:

javascript - grunt 自动前缀配置

php - 单击 PHP 中的菜单选项时 POST 方法不起作用

javascript - 更改控制组事件按钮主题

jquery - 根据 href 在 jquery-ui 工具提示中显示图像

php - 从 laravel 中的数组中获取特定值

javascript - 未从服务器检索 jQuery 文件

javascript - 如何使用 jQuery 从下拉列表中获取所选项目的文本?

javascript - 使用 javascript 创建一个唯一的对象标识符

javascript - Canvas图像缩放、旋转、绘图

php - 在centos上运行yii应用程序