Janaka Dombawela
Janaka Dombawela

Reputation: 1357

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 . "')");

?>

Upvotes: 0

Views: 78

Answers (2)

Aditya Jhunjhunwala
Aditya Jhunjhunwala

Reputation: 635

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

Upvotes: 4

Chetan Gawai
Chetan Gawai

Reputation: 2401

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>

Upvotes: 1

Related Questions