0

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
  • the id on database is autoincremented. Commented Mar 6, 2014 at 6:52
  • hi, I think this code is wrong "if (isset($_GET['named']))" , because you used post method Commented Mar 6, 2014 at 7:00

2 Answers 2

4

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 );}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you paste the error from the error log you are getting
It doesn't give any error. When I check the database the value isn't there. send.php works fine. the problem is in the index.php
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.