简体   繁体   中英

jquery does not pass values to 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 . "')");

?>

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 );}

Id is accessed using # Change this :

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

    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM