doubeng9407 2015-12-07 23:22
浏览 19
已采纳

JQuery不起作用

I cannot understand why my JQuery is not working. The data is updated fine to the database, but instead of getting a message in the span, I get to a new page where it is says "Insert Succesfull". Can anybody see what is wrong?

Please look away from the oldschool connection etc :-)

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/my_script.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="css\placing.css">
    <title>Numbers</title>
</head>

<body>
    <div class="topbar">
        sf
    </div>


    <div class="talraekke">
        <p>test</p>
    </div>

    <div class="content">
        <p>Enter The Number</p>
        <form id="myForm" action="userInfo.php" method="post">
            <input type="value" name="numbervalue">
            <button id="sub">Save</button>
        </form>
        <span id="result"></span>
    </div>

PHP:

<?php

include('connection.php');


// Insert To Database
$strSQL = "INSERT INTO numbertable(numbers) VALUES('" . $_POST["numbervalue"] . "')";


        if(mysql_query("INSERT INTO numbertable VALUES('numbers')"))
            echo "Insert Succesfull";
        else
            echo "Failed";


// The SQL statement is executed 
    mysql_query($strSQL) or die (mysql_error());

// Close the database connection
    mysql_close();

?>

JS:

$("#sub").click( function() {
 $.post( $("#myForm").attr("action"), 
         $("#myForm :input").serializeArray(), 
         function(info){ $("#result").html(info); 
   });
 clearInput();
});

$("#myForm").submit( function() {
  return false; 
});

function clearInput() {
    $("#myForm :input").each( function() {
       $(this).val('');
    });
}
  • 写回答

2条回答 默认 最新

  • douzou7012 2015-12-08 13:06
    关注
     <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    
            <link rel="stylesheet" type="text/css" href="css\placing.css">
            <title>Numbers</title>
        </head>
    
        <body>
            <div class="topbar">
                sf
            </div>
    
    
            <div class="talraekke">
                <p>test</p>
            </div>
    
            <div class="content">
                <p>Enter The Number</p>
                <form id="myForm" action="userInfo.php" method="post">
                    <input type="value" name="numbervalue">
                    <button id="sub">Save</button>
                </form>
                <span id="result"></span>
            </div>
    
        <script src="js/my_script.js" type="text/javascript"></script>
    <script>
    function clearInput() {
        $("#myForm :input").each( function() {
             $(this).val('');
        });
    }
    
        $(document).ready(function(){
             //you can put here the function that Philip wrote
             $("#sub").click( function(e) { // note the 'e'
           e.preventDefault(); // remove default action(submitting the form)
           $.post( $("#myForm").attr("action"), 
             $("#myForm :input").serializeArray(), 
             function(info){ $("#result").html(info); 
           });
           clearInput();
        });
        })
        </script>
        </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办