Reputation: 887
I'm trying to POST from my program to a separate PHP file, then grab that data and store it into my SQL database. I've checked my code over and over and can't' find what I'm doing wrong. Any help would be appreciated.
$(".btn btn-success last").click(function(){
$.post("go2.php",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
PHP file (go2.php) w/ SQL code
<?php
include 'connection.php';
$x = $_POST["name"];
$sql = "INSERT INTO users (Username) VALUES ('$x') ";
$query = mysql_query($sql);
?>
Upvotes: 0
Views: 73
Reputation: 24001
From your code ..
you need to learn a little bit more about selectors
$(".btn btn-success last") // what is btn-success ID or Class and
//What is last ID or Class for id use #btn-success for class use .btn-success
before you insert anything to php file check if this file already connected with js or not .. and with the next code you should get alert with 'file already connected'
$(".btn .btn-success .last").click(function(){
$.post("go2.php",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data){
alert(data);
});
});
and in php
<?php
echo('file already connected');
?>
Most important thing to know what is a (.last) is that a button or form or anchor
if .last is form
$(".btn .btn-success .last").on('submit',function(e){
e.preventDefault();
return false;
});
if .last is button or anchor
$(".btn .btn-success .last").on('click',function(e){
e.preventDefault();
return false;
});
hope it will help
Upvotes: 1
Reputation: 622
Try the long version of ajax request
$(document).ready( function () {
$(".btn btn-success last").click(function(){
$.ajax({
type: "POST",
url: "go2.php",
data: {name:"Donald Duck", city:"Duckburg"},
success: function(data){
document.getElementById('divId').html(data);
}
});
});
});
For the echo data, you have to echo a message in your go2.php like "Success"
Upvotes: 0