Genti Abazi
Genti Abazi

Reputation: 1

how to get data from javascript

hello i want to get text from textarea and register in database but not work where is problem thanks all. $get_text show empty value.

<script type="text/javascript">
    $(function() {
        $(".new_post").click(function(){
            var get_text = $("#post_text").val();
            $.ajax({
                type: "POST",
                url: "ajax/ajax_new_post.php",
                data: get_text,
                success: function() {
                    alert(get_text);
                    $('#post_text').val('');
                    //$(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
                    //.animate({ opacity: "hide" }, "slow");
                }
            });
            return false;
        });
    });
</script>
<body>
   <textarea id="post_text" placeholder="What's on your mind?"></textarea>
</body>

ajax_new_post.php

$get_text = $_GET['get_text'];
mysqli_query($Connection, "INSERT INTO posts VALUES('$ID', '$get_text')");

Upvotes: 0

Views: 49

Answers (4)

Thamilhan
Thamilhan

Reputation: 13303

You are missing key. Change your data to:

data: { get_text : $("#post_text").val() },

And your type is POST, so use $_POST['get_text'] in PHP file.

Upvotes: 1

Azhar Ansari
Azhar Ansari

Reputation: 104

Use jQuery to send a JavaScript variable to your PHP file:

'$url = 'path/to/phpFile.php';
 $.get($url, {name: get_name(), job: get_job()});'

In your PHP code, get your variables from $_GET['name'] and $_GET['job'] like this:

<?php
    $buffer_data['name'] = $_GET['name'];
    $buffer_data['job']  = $_GET['job'];
?>

Upvotes: 0

Jesus Segnini
Jesus Segnini

Reputation: 355

The problem is that you are making a POST and trying to get the value as GET.

Use :

$get_text = $_POST['get_text'];
mysqli_query($Connection, "INSERT INTO posts VALUES('$ID', '$get_text')");

Upvotes: 0

jophab
jophab

Reputation: 5509

You are using post method . So use $_POST['get_text'];

Upvotes: 0

Related Questions