0

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.

AJAX

$(".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);



?>
4
  • You have to echo something back Commented Apr 25, 2015 at 23:54
  • Your code has an SQL injection vector and a deprecated API used . Commented Apr 25, 2015 at 23:55
  • How come I need to echo something back @adeneo ? I'm checking the SQL database after each execution for any update Commented Apr 25, 2015 at 23:56
  • Should provide more detail on what is or isn't working. You haven't specified if the request is being made or not , if any errors are thrown etc. Use browser dev tools/console to inspect request Commented Apr 26, 2015 at 0:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I think it would be more clear if you seen the actual demo I'm working on. It's a SPA, so I really can't echo anything back when making the POST. I understand selectors and what not, it's just not reaching my PHP file and I dont understand why. Since it's SPA, I've been testing a echo by having it write data to a file when it reaches the go2.php file
check out my demo at justinsamuels.info/structures/Steps/index.html and click all the way to STEP 4. The data being entered, I need it to be POSTED so I can retrieve it and store into a SQL database
is the go2.php directory is the same with your index.html?
0

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"

9 Comments

there is no echo in javascript
where does findElementById come from?
changed, going to sleep
no error being thrown, just my DB is not updating with the info being POSTED
still no luck with this long version of the AJAX request.
|

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.