0

I'm unable to run my php script at the click of the button.

If I run the script via the terminal, 'php get_funny_status.php' it returns the correct output. However, I'm not able to do this via AJAX. The beginning ajax alert shows up, but I'm not getting any responseText.

Am I missing something?

EDIT: I am testing this application view Preview Browser in Adobe Dreamweaver (I'm not sure if this has anything to do with the issue)

<script>

$( document ).ready(function() {

    $( ".nextStatus" ).click(function() {

        alert('beginning ajax');

        var statusNumber = '5';

        $.get('get_funny_status.php?' + statusNumber, function(responseText) {
            alert(responseText);
        });

    });
});

</script>

Here's my php script:

  <?php

    //initialize DB stuff

    $status_text_query = "SELECT * FROM funny_status WHERE STATUS_NUM = '". $_GET['statusNumber']."'";

    $result = mysql_query($status_text_query);

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

        echo 'num likes: ' . $row['NUM_LIKES'];
        echo 'num dislikes: ' . $row['NUM_DISLIKES'];
        echo 'status text: ' . $row['STATUS_TEXT'];
        echo 'status num: ' . $row['STATUS_NUM'];   

     }  
 ?>
Mark Kennedy
  • 1,751
  • 6
  • 30
  • 53
  • `_GET` should be `$_GET`. Is the path to the file correct? – Felix Kling Feb 08 '14 at 01:02
  • If I were you, I would use mysqli or PDO instead of mysql_ functions. The first line is a hacker delight, prone to SQL Injection. – Robson França Feb 08 '14 at 01:07
  • It looks like yo have some good answers to work from below. I should also point out you have a HUGE SQL injection vulnerability that you should think about fixing. – Mike Brant Feb 08 '14 at 01:08
  • NONE of these answers are helping. I already realized the syntax errors and TRIPLE CHECKED the path name. Could it have something to do with running a .php from Adobe Dreamweaver? I'm able to run the script from my terminal just fine. Just not through Dreamweaver! – Mark Kennedy Feb 08 '14 at 01:43
  • Can you use XAMPP or something similar to that to see if it works? Also, you didn't mention an error log; does it get populated with anything when you run the script? – Chris Forrence Feb 08 '14 at 13:45
  • use your browser's inspector and check the Network tab, there should be the request and the response, the response should give you usefull information about your bug – arieljuod Feb 08 '14 at 15:38

2 Answers2

1

Fix this from:

    $.get('get_funny_status.php?statusNumber', function(responseText) {
        alert(responseText);
    });

to

    $.get('get_funny_status.php?statusNumber='+statusNumber, function(responseText) {
        alert(responseText);
    });
DeepBlue
  • 684
  • 7
  • 23
0

There are a few issues that I see:

  1. In your PHP, _GET should be $_GET.

    $status_text_query = "SELECT * FROM funny_status WHERE STATUS_NUM = '"
        . $_GET['statusNumber']."'";
    
  2. In your AJAX call, you need to send your parameter ?statusNumber=' + statusNumber, ...

    $.get('get_funny_status.php?statusNumber=' + statusNumber, 
        function(responseText) {
    

Not necessarily a syntax error: I noticed that you're using mysql_* to access the database. This is a bad idea; I'd advise switching to either the mysqli_* functions or to the PDO class. Related to this issue, if I were to set statusNumber to something like "'; DELETE FROM funny_status;--", I would be able to delete all of the data in that table. This is called SQL Injection, and it is a serious security issue.

Community
  • 1
  • 1
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64