doushan6161 2014-11-24 17:25
浏览 35
已采纳

如果放置了preventdefault,Jquery将不会POST

I want to make a form where you write a number send it with Jquery and is shown by PHP without refreshing the page ,by far i have this code but if preventDefault() is placed it will post but the content is not shown,also if i remove it, the content is shown but the page will pe refreshed.

Javascript:

$('#testut').unbind('submit').bind('submit',function(ev) {
    ev.preventDefault();
    $.ajax({
        type: 'post',
        url: 'incerc.php',
        data: $('form').serialize(),
        success: function () {
            $('.success').fadeIn(500);
        }
    });
});
}

HTML:

Test
<br />
<?php
if(isset($_POST['test'])) {
    $test = $_POST['test'];
    $numar = $_POST['numar'];
    echo 'Order name:'.$test.'<br />Number:'.$numar.'<br />';
} else {
    echo 'No <br />';
}
?>
<div id='testut'>
    <form id='test' method='post'>
        <input type='text' name='numar'><br />
        <input type='submit' name='test' onclick ='check()' value='Place'><br />
        <span class='success' style='display:none'>Order was successfully placed.</span>
    </form>
</div>
  • 写回答

3条回答 默认 最新

  • douhua1890 2014-11-24 20:04
    关注

    I wanted to execute a query after the POST in php,the example was just for test, so i managed to do this(on example).

    Javascript:

    function check(){
    $("#test").submit(function(e)
    {   var numar = $('#numar').val();
        var test = $('#testim').val();
        var dataString = 'numar='+ numar + '&test=' + test;
    
        $.ajax(
        {
            url : 'incerc.php',
            type: "POST",
            data : dataString,
            success:function(data, textStatus, jqXHR) 
            {
                $('.success').fadeIn(500);
                $('body').load('incerc.php?'+dataString);
            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                $('.success').html("Nu a mers");     
            }
        });
        e.preventDefault(); 
        e.unbind(); 
    });
    
    }
    

    HTML:

    <?php if(isset($_GET['test'])){
    $test = $_GET['numar'];
    $numar = $_GET['numar'];
    echo 'Order name:'.$test.'<br />Number:'.$numar.'<br />';}
    else{echo 'No <br />';} ?>
    <div id='testut'>
    <form name='test' id='test' method='POST'>
    <input type='text' id='numar' name='numar'><br />
    <input type='submit' id='testim' name='test' onclick ='check()' value='Place'><br />
    <span class='success' style='display:none'>Order was successfully placed.</span>
    </form>
    

    So now the body will load when the data is sent sucessfully , and will get the result correctly ,also in the adress bar it will still show 'incerc.php' not 'incerc.php?'+dataString.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程