3

I'm trying post data to PHP file but i can't receive any data from PHP file. Let me add codes. This is my jQuery function:

$(document).ready(function () {

$(function () {
$('a[class="some-class"]').click(function(){

   var somedata = $(this).attr("id");

   $.ajax({
      url: "foo.php", 
      type: "POST",
      data: "id=" + somedata,

      success: function(){
          $("#someid").html();
      },
      error:function(){
          alert("AJAX request was a failure");
      }
    });
    });
    });
});

This is my PHP file:

<?php
$data = $_POST['id'];

$con = mysqli_connect('localhost','root','','somedatabase');
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"database");
$sql="SELECT * FROM sometable WHERE id = '".$data."'";
$result = mysqli_query($con,$sql);


while($row = mysqli_fetch_array($result)) {

 echo $row['info'];

 }

mysqli_close($con);

?>

This what i have in HTML file:

<p id="someid"></p>

<a href="#page2" class="some-class" id="1">Data1</a>
<a href="#page2" class="some-class" id="2">Data2</a>

Note: This website is horizontal scrolling and shouldn't be refreshed. When i'm clicking links (like Data1) it's going to another page without getting data from PHP file

3
  • This is a POST request -.- do not use data: "id=" + somedata, but something like id:somedata Commented Dec 16, 2014 at 14:02
  • you need to add ? at ur url after .php url: "foo.php?", Commented Dec 16, 2014 at 14:03
  • any solutions presented help you? Commented Dec 16, 2014 at 14:55

5 Answers 5

2

You have a few problems:

  1. You are not using the data as mentioned in the other answers:
    success: function(data){ $("#someid").html(data); },
  2. You are not cancelling the default click action so your link will be followed:
    $('a[class="some-class"]').click(function(e){ e.preventDefault(); ...;
  3. As the id's are integers, you can use data: "id=" + somedata, although sending an object is safer in case somedata contains characters that need to be escaped:
    data: {"id": somedata},;
  4. You have an sql injection problem. You should cast the variable to an integer or use a prepared statement:
    $data = (int) $_POST['id'];;
  5. As also mentioned in another answer, you have two $(document).ready() functions, one wrapping the other. You only need one.
Sign up to request clarification or add additional context in comments.

2 Comments

thank you! The only problem was forget to add 'data' to function and html. I want to ask another question for this, This function gets every data from PHP file but i want to take about 5 variable for each <p> How can i do that?
@KorhanYüzbaş That's really another question, but in short, you could return json containing whatever you need and parse / use that in the success() function.
1
success: function(){
          $("#someid").html();
      },

should be:

success: function(data){
          $("#someid").html(data);
      },

1 Comment

thank you so much it's working now! I want to ask another question for this, This function gets every data from PHP file but i want to take about 5 variable for each <p> How can i do that?
1

You should add parameter in success

success: function(data){ //Added data parameter
      console.log(data);
      $("#someid").html(data);
  },

The data get the values what you echo in PHP end.

Comments

1

This:

  success: function(data){
      $("#someid").html(data);
  },

and you have two document ready, so get rid of:

$(document).ready(function () { ...

});

Comments

0
data: "id=" + somedata,

Change it to:

data: { id : somedata }

2 Comments

A querystring is perfectly valid for data
somedata could be a not valid part of a querystring, so this solution would fix this problem.

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.