1

I have problem with ajax and php.I have next code

$("form").submit(function() {
 var poruka = $("#poruka").val();
  $.ajax({
        type: "GET",
        url: "/ajax/gather/send/"+ poruka + "/'.$id.'",
        success: function(){
         loaduj('.$id.');
         alert("Your message is "+poruka);
        },
        error: function(){
            alert("fail");
        }
    });
});

"poruka" is the message and it's taken from form

<form action='javascript:;'>
<input type='text' name='poruka' id='poruka' placeholder='Poruka..' style='width:100%;'>
<input type='button' name='button' hidden>
</form>

And when i try to send to php it won't insert that into database :

Php code :

$text = $_POST['text'];
$id = $_POST['id'];
if($text != ""){
  mysql_query("INSERT INTO messages (text,id2) VALUES ('$text','$id')") or     die(mysql_error());
}

And it can't work..How can i fix it ? Thanks

2 Answers 2

2

You use POST instead of GET. In your AJAX form you specify that you use GET, but in PHP you use $_POST superglobal.

Change $_POST into $_GET in your PHP code and it should work :)

Also if you do not specify method in your tag, the default is GET.

From experience, in case if the data from the form is not written/updated in/to the database use

echo mysql_error();

after the MySQL query and also open the developer tools on your browser and inspect in the network panel. Developer tools can be opened using F12 and with CTRL+SHIFT+I if your F12 is not working due to the coffee spilled on it. Network panel shows the data being sent, the method used and every other detail of a network call. Also there you can see the response of your server side script which in this case would return a mysql error message because you echo'ed it out.

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

1 Comment

No problem, hope that helped! :)
0

The usual deal is to first locate the problem, then post:

  • Form not calling JS
  • JS not making the request
  • PHP not getting the variable

SQL injection detected

5 Comments

SQL injection is not the case at this point. He just wants to have a simple code working, not to implement full functionality when even a simple thing does not work as expected
i can not be speaking on the part of the author - what he does or does not want, but usually you don't write code to keep it in a sealed box with you being the only person to ever open it. There can be trouble with the given SQL; it's a mere warning.
That's good, however I believe your answer does not suit the question here :)
SQL injection detected is not answer. The answer is debug first O-)
Yes, debugging is one of the methods every programmer uses when something does not work. I believe the question author is fully aware of it :)

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.