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'];   

     }  
 ?>
6
  • _GET should be $_GET. Is the path to the file correct? Commented Feb 8, 2014 at 1: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. Commented Feb 8, 2014 at 1: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. Commented Feb 8, 2014 at 1: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! Commented Feb 8, 2014 at 1: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? Commented Feb 8, 2014 at 13:45

2 Answers 2

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);
    });
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.