2

I'm having some troubles trying to send an id through ajax script, I'm trying to create an address book, when you select a contact it load all his information in another page and the ajax script load this page inside the main. Everything is working except to send the ID of the contact. There's a bit of the code :

 $.ajax({
         url: "select.php",
         dataType: "html",
         data: {id: id},
         success: function(Result) {
         $('#result').html(Result);
         }
        });

PHP

$id = $_POST['id'];
echo $id;
$sql = "SELECT * FROM ca11 WHERE contact_id= $id";

does anyone have an idea ?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Nalik
  • 21
  • 2
  • 3
    add `type: POST` – Rotimi May 31 '17 at 12:34
  • you haven't provided the type of request in the ajax if its post – Kevin May 31 '17 at 12:34
  • be careful with the query though, don't directly inject variables into query statements, prepare them instead – Kevin May 31 '17 at 12:35
  • learn about prepared Statements to prevent SQL injection – Jens May 31 '17 at 12:35
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 31 '17 at 12:35
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) and do what @Akintunde said to do. – Jay Blanchard May 31 '17 at 12:38
  • Oh it's working with the type: post thx ! – Nalik May 31 '17 at 12:41
  • you're not providing the type, you also have a space character between contact_id= $id; you should also think about using a prepared sql query to avoid some security issues. Finally, make sure your contact_id field is defined as an integer and not a varchar in your database – sic-sic May 31 '17 at 12:47

1 Answers1

1

you need to add which type of request you are making, as your question, you can call post request as below

$.ajax({
  type: "POST",
  url: "select.php",
  dataType: "html",
  data: {id: id},
  success: function(Result) {
         $('#result').html(Result);
     }
});

For more information you can refer this link http://api.jquery.com/jQuery.post/

Ashok
  • 184
  • 2
  • 14