0

hi I was trying to insert input tag value into MySQL using Ajax since most of the operation is going in js I tried with reference to this. now don't know what is going on, it's not doing anything I checked console its shows nothing here...I tried this code in multiple projects and there it was working. any help or alternative way would be appreciated. Thanks here are my codes files.
test.php

<!DOCTYPE html>
<html>
<head>
<script src="./jquery-3.5.0.min.js"></script>
</head>
<body>
<input type="text" name="name" id="name" placeholder="enter name">
<script type="text/javascript">
    $(document).ready(function(){
    $("#name").on('keypress',function(e){
        if(e.which == 13){
            var strr = $("#name").val();
            console.log(strr);
            $.ajax({
                type: 'POST',
                url: 'testinsert.php',
                data: $('#name').val(),
                dataType: 'json',
                success: function( response){
                console.log( 'the feedback from your result.php: ' + response);
                }
            });
        }
    });
 });
</script>
</body>
</html>

testinsert.php

<?php 
if(isset($_REQUEST))
{
mysql_connect("localhost","root","");
mysql_select_db("usr");
error_reporting(E_ALL && ~E_NOTICE);

$email=$_POST['name'];
$sql="INSERT INTO at_user(user) VALUES ('$email')";
$result=mysql_query($sql);
if($result){
echo "You have been successfully subscribed.";
}
}
?>

here is output of console enter image description here

8
  • Please use entire form tag instead of just only take input , after taking form provide id attribute to form after edit your question Commented Apr 21, 2020 at 5:23
  • Take a look at your browser's DevTools Network tab to see what data gets sent to your script. Does $_POST['name'] exist? Where do you set name? Commented Apr 21, 2020 at 5:30
  • Consider rewording the question line as a question. Commented Apr 21, 2020 at 5:59
  • @kerbholz I check error 500 to my script but i got param $_POST['name'] Commented Apr 21, 2020 at 8:48
  • "but i got param $_POST['name']" I bet you don't: data: $('#name').val() says you're sending a string with the value of the field #name, no 'name' key here. Take a look at @JatinKaklotar s answer Commented Apr 21, 2020 at 8:56

1 Answer 1

2

Replace below line from ajax script

from

  data: $('#name').val(),

to

  data: {name : $('#name').val()},
Sign up to request clarification or add additional context in comments.

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.