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

1 Answers1

2

Replace below line from ajax script

from

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

to

  data: {name : $('#name').val()},
Jatin Kaklotar
  • 445
  • 5
  • 17