duanmangxie7131 2014-03-06 06:52
浏览 34

jquery不会将值传递给mysql db

I,m trying to pass text box value into mysql database using jquery. but nothing seems to work and I cannot figure out what the error. Here's my code.

index.php

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>

</head>
<body>
    <script>
                $(document).ready()
                $("btn").click(function() {
        $.post("send.php", {"named": $("named").val()}, function(data){
        alert("Data: " + data + ");}
        })
        });

    </script>
    <div>
        <form id="form" method="POST" action="">
            <input type="text" id="gname"/></br>
            <button id="btn">Set</button>
        </form>
    </div>
</body>

and send.php

<?php

mysql_connect("localhost", "root", "") or die("failed to connect");

mysql_select_db("ajax01") or die("failed to select");

if (isset($_GET['named'])) {

$named = mysql_real_escape_string($_POST['named']);

}

//$named = "phptest";

mysql_query("INSERT INTO `variables` (`id` , `name`) VALUES ('' ,  '" . $named . "')");

?>
  • 写回答

2条回答 默认 最新

  • dongwen4487 2014-03-06 06:55
    关注

    You are sending data from POST

     $.post("send.php", {"named": $("named").val()}
    

    and checking if GET is set:

    if (isset($_GET['named'])) {
    

    And retreiving the param from $_POST:

    $named = mysql_real_escape_string($_POST['named']);
    

    Hope you got the error now...

    Try:

    if (isset($_POST['named'])) {
    

    This should work

    Try this in index.php

     $.post("send.php", {"named": $("#gname").val()}
    

    and

    alert("Data: " + data );}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染