Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax post method not working

I am currently creating a website and am having trouble using AJAX to post my data. I have a button and when clicked the following code is processed...

var name = document.getElementById('name').innerHTML;
var text = document.getElementById('text').innerHTML; 

$.ajax({  
    type: "POST",  
    url: "php/post.php",  
    data: { postName: name, postText: text},  
    success: function() {  
        $('#paragraph').html("worked");    
    }  
});  

This definitely opens the post.php page but it is not passing through the desired data. Am I doing something wrong?

Thanks in advance

like image 266
Phil Avatar asked Dec 20 '22 21:12

Phil


1 Answers

What do the variables name and text contain? Rather than doing

var name = document.getElementById('name').innerHTML;
var text = document.getElementById('text').innerHTML; 

You can do:

var name = $("#name").val(); 
var text = $("#text").val();

You may need to pass the datatype object too:

$.ajax({  
type: "POST",  
url: "php/post.php",  
data: { postName: name, postText: text}, 
dataType: "json",
success: function() {  
    $('#paragraph').html("worked");    
}  
});  
like image 141
Darren Avatar answered Dec 23 '22 09:12

Darren