0

long time answer-seeker, first time question-asker.

I am configuring a small snippet of js which is referenced in the page footer:

<script type='text/javascript' src='http://test.site.com/wp-content/themes/spacious/js/jsfile.js?ver=1.0.5'></script>

Here is the html for the inputs:

<div>
    <form id="search" action="" method="POST">
        <input type="text" id="str" name="str" value="" />
        <input type="submit" value="search" />
    </form>
    <div id="search_results"></div>
</div>

js script file as follows:

(function func() {
    $("#search").bind('submit', function () {
        var value = $('#str').val();

        $.post('db_query.php', {
            value: value
        }, function (data) {
            $("#search_results").html(data);
        }); 

        return false;
    });
});

php file as follows:

<?php
    try {
        $db = new PDO('sqlsrv:Server=xx.xx.xx.xx;Database=xxxx','user','pass');
    }
    catch (Exception $e) {
        echo 'PDO connection error: ' . $e->getMessage();
        exit(1);
    }

    $sql=$db->prepare("SELECT top 20 Timestamp,Information,Location FROM Table WHERE JobReference = :val");
    $sql->execute(array(':val'=>$_REQUEST['value']));

    echo '<table>';
    echo '<th>Date Time</th><th>Information</th><th>Location</th>';

    while ($row=$sql->fetch())
    {
        echo "<tr><td>$row[Timestamp]</td><td>$row[Information]</td><td>$row[Location]</td></tr>";
    }
    echo '</table>';
?>

When I try to use Chrome to debug, it stops on the first '(' and then jumps to the end of the function. At first I thought it was because of the .php file reference, but it seems like it is not even hitting it?

I don't know where to go with this...

Many thanks, Cameron.

1
  • You don't need that ( at the beginning. It can just be function func(){. You can also remove the extra ) at the end of the function. Commented Apr 14, 2016 at 4:54

2 Answers 2

1

You are just declaring a JavaScript Function called func. But you also have to call that function in order to make your code work.

Add another bracket to the end of your function like this:

(function func() {
$("#search").bind('submit', function () {
    var value = $('#str').val();
    $.post('db_query.php', {
        value: value
    }, function (data) {
        $("#search_results").html(data);
    }); return false;
  });
})();
Sign up to request clarification or add additional context in comments.

8 Comments

Might as well remove func as well. To create a closure you put your code inside (function(){ /*in here*/ })().
Wow you're fast - this moved the cursor into the function, then sprouted another error on line 2: Uncaught TypeError: $ is not a function
Yes, he could remove the Function name as well. But it's not neccessary to make the code work. Maybe he need's the function name later.
@CameronB Have you included jQuery into you HTML?
Yes, well before; Bhojendra below has corrected this issue using $ in the initial parentheses and jQuery in the last parentheses. now the function is working but the php file is not found -- looks like I need to find the correct path to the file..
|
0

In addition to @Nano answer, you can fix like this:

(function($) { //pass $ as jQuery in this scope
$("#search").bind('submit', function () {
    var value = $('#str').val();
    $.post('db_query.php', {
        value: value
    }, function (data) {
        $("#search_results").html(data);
    }); return false;
  });
})(jQuery); //$ is jQuery

2 Comments

Unfortunately this gave the same error as without jQuery within the parentheses, as per Nano's above. -- EDIT -- I missed the $ in the initial parentheses - now I have the PHP file not being found..
There might be something odd.

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.