0

Have been trying to make a guestbook with jquery, ajax and php, I have been able to reed and print out everything inside the database but for some reason I cant save what I write in the database and then print it out as a post in the guestbook, if someone could see what I am doing wrong I would appreciate it! (for now I only try to get the username inside the database)

this is the jquery:

$("#newPost").bind('click', function(){
    var userName = $('#userName').val();
    var message = $('#message').val();
    $.ajax({
        url: "server.php?action=newPost",
        type: "POST",
        data: {userName: userName},
        success: function(data){
            if(data == "true"){
                alert(data);
                $('#posts').prepend('<td>'+userName+'</td>');
                $('#userName').val('');
            }
            else{
                alert('Something went wrong while trying to save!');
            }
        },
        error: function(xhr, error){
            alert('Could not connect to server!');          
        }

        });
    }); 

this is the server.php file:

$db = mysqli_connect('localhost', 'username', 'password', 'my_database');

if(isset($GET_['action']) && $GET_['action'] == 'newPost'){
    $userName = mysqli_real_escape_string($db, POST_['userName']);
    if(mysqli_query($db, "INSERT INTO message (name) VALUES ('$userName')")){
        echo "true";
    }
    else{
        echo "false";
    }

}

and tis is the html form:

<form action="#"> 
    <p>Name:</p>
    <textarea type="text" class="field" id="userName" rows="1" cols="20"></textarea><br/><br/>
    <p>Meddelande:</p>
    <textarea type="text" class="field" id="message" rows="3" cols="20"></textarea><br/><br/>
    <input value="Send" class="button" type="button" id="newPost"></input><br/>
</form>
spovell
  • 133
  • 2
  • 13
  • Echo the userName, there's nothing in it. It should also have a 'name' – Matheno May 17 '13 at 13:22
  • `url: "server.php?action=newPost",` are you sure this is the right path to the file you're trying to access? `server.php` may be in a different location. –  May 17 '13 at 13:43
  • it is in the same folder as all the other files... – spovell May 17 '13 at 14:03

2 Answers2

1

Try replacing $GET_ and POST_ with $_GET and $_POST

Also your field names are empty, add name="userName" and name="message" to your textarea HTML tags

vonUbisch
  • 1,226
  • 16
  • 27
  • thanks for that but I still get the message Could not connect to server each time??? – spovell May 17 '13 at 13:32
  • if i only fill out the name field I get the Something went wrong while trying to save! pop up instead? – spovell May 17 '13 at 13:43
  • Try referencing to this answer [link](http://stackoverflow.com/a/6960586/488074) or any other AJAX request for constructing your request. I don't like this piece "data: {userName: userName}", otherwise print out the messages coming from (xhr, error) – vonUbisch May 17 '13 at 13:43
  • For debugging Javascipt I would recommend use the command "console.log(xhr);", then open a console with Firebug or something. Then you can debug that object very easily. – vonUbisch May 17 '13 at 14:18
  • now I can see that the result of data returns false? know why? – spovell May 17 '13 at 15:56
  • Try debugging all these objects; `(xhr, error, msg){` also I would consider rewriting your AJAX call, look at this [example](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example) – vonUbisch May 17 '13 at 19:21
0

You forgot the name in your textarea. So $userName is empty.

Matheno
  • 3,867
  • 5
  • 30
  • 48