0

I would like to get data from a mysql database without page refresh. I use jquery ajax in client side and php in server side. The request went but the response is empty(if i go to xhr in Firefox than the request is good,no problem, but when i click answer/response it is empty and the size of the request also 0).There aren't any errors, but nothing happened. Here is my script.js javascript file:

 $(document).ready(function(){
    $.post("ajax.php",
    {
        task: "get_iron"
    },
    function(data)
    {
        $("#iron").html(data);
    }
    );
    console.log("js doesn't have any problem");//it works fine
});

And this is my ajax.php file:

<?php
$method = $_SERVER['REQUEST_METHOD'];
if(strtolower($method)=='post')
{
    if(isset($_POST['task']) && $_POST['task'] == "get_iron")
    {
        $con = mysql_connect('myhost','myusername','mypassword');
        if (!$con) {
          die('Could not connect: ' . mysql_error($con));
        }
        $sql="SELECT iron FROM minerals WHERE username = 'martintamiya'";
        $result = mysql_query($sql,$con);
        $row = mysql_fetch_object($result);
        echo json_encode($row);
    }
}

?>

Should i use $.ajax instead of $.post? Please help me and note that i'm a beginner. Thank you for the responses.

  • 1
    `$_POST['task' == "get_iron"]` should be `$_POST['task'] == "get_iron"`. – gen_Eric Oct 31 '14 at 18:18
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 31 '14 at 18:18
  • P.S. Your `console.log` will run *before* the AJAX call is finished. – gen_Eric Oct 31 '14 at 18:19
  • Just a note, you can improve your troubleshooting process by temporatily outputting something before making conditional statements on the server side. – MSTannu Oct 31 '14 at 18:20
  • Oh thank you, you are right i fixed it but i have the same problem right now – Martin Putz Oct 31 '14 at 18:21
  • Prolly would have been quicker to read thru your code than to create a whole new SO account just for this question. – I wrestled a bear once. Oct 31 '14 at 18:22
  • This is my domain if its easier: http://coder.meximas.ocom – Martin Putz Oct 31 '14 at 18:22
  • No, it's not easier. We can't see your PHP by looking at your website. Anyway, what is `$_POST['task']` for anyway. You don't use it anywhere. – I wrestled a bear once. Oct 31 '14 at 18:24
  • To reiterate, make sure you actually reach the correct file on php. You are using a relative path, which means its unlikely to work on sub-pages. – MSTannu Oct 31 '14 at 18:26
  • Ok if $_POST['task'] is not a good solution than how should i get the request from ajax?I know my code is wrong, but could you link like a website or something whiche cleary explains this situations? – Martin Putz Oct 31 '14 at 18:32
  • 1. does your request reach a page that exists? Check your favorite dev tools that allow you to see requests to the server (chrome + firefox hit f12). or print a statement at the top of the page that will let you know your php page is hit 2. add print statements before each if statement to trace where your code actually hits. `$_POST['task' == "get_iron"])` is not valid. So pretty certain you are not getting past this point. 3. use paper and pencil to determine where you are getting and why. – scrappedcola Oct 31 '14 at 18:33
  • `but could you like a website or something whiche cleary explains this situations` one could and in fact there are tons of [web sites that cover basic php and ajax interactions](http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php). `$_POST['task']` is not the issue the issue is that the way you have it is not correct. see Adelphia's answer to see what it should be. – scrappedcola Oct 31 '14 at 18:35
  • i fixed the $_POST['task'] and now i get Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/u268780878/public_html/ajax.php on line 13
    false
    – Martin Putz Oct 31 '14 at 18:37
  • check your sql. Do you have a table `minerals` with a column of `username` and does it also have a column of `iron`? Generally that means your sql statement is bad. – scrappedcola Oct 31 '14 at 18:41
  • I checked the sql and i think its fine, it has a table named minerals and it has a column of username and it also has a column of iron. – Martin Putz Oct 31 '14 at 18:50

1 Answers1

1
if(isset($_POST['task']) && $_POST['task' == "get_iron"])

That is always going to return false because you don't have a post field called 'task' == "get_iron" and since there isnt an else statemnt it's doesn't do anything.

if(isset($_POST['task']) && $_POST['task'] == "get_iron")

That's what you want.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116