0

I am getting the following error from the chrome developer tool

Uncaught ReferenceError: searchRequests is not defined searchProcess.php:174 onclick.

When I click on hyperlink produced from engine.php, I don't get the alert from the searchRequests function. I'm not sure what the problem is, I appreciate any advice given. Here is my code:

searchProcess.php

<?php
include '../include/engine.php';
?>
<html>
<head>
<script type="text/javascript" src="../jQuery.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
 var instrID;
 var cat;
 $(window).load(function(){
 });
 var newheight = $(window).height();
 function searchRequests(instr)
 {
 alert("in searchResults");
 instrID = instr;
 alert(instrID);
 }
 });
</script>
</head>
<body>
<?php
 drawSearchResults($var1, $var2, $var3, $var3, $var4); 
?>
</body>
</html>

engine.php

<?php
function drawSearchResults($var1, $var2, $var3, $var4, $var5)
{
while($row = mysql_fetch_assoc($result))
{
 echo ("<tr>");
 echo ("<td id='InstrumentID'><a href='javascript:void(0);' onclick='searchRequests($row[InstrumentID])'>$row[InstrumentID]</a></td>");
 echo ("</tr>");
}
?>
von v.
17.1k5 gold badges62 silver badges89 bronze badges
asked Apr 17, 2013 at 2:02
2
  • 5
    Move function searchRequests to somewhere outside of $(document).ready() Commented Apr 17, 2013 at 2:05
  • or maybe fix the jQuery path ? Commented Apr 17, 2013 at 2:06

2 Answers 2

2

The problem is that the function searchRequests is not in scope outside of the $(document).ready(). Move it outside of $(document).ready().

In general you shouldn't embed your javascript in the html. Much nicer:

$('#InstrumentID a').click(someFunctionThatIsInScope);

And you can put that code in the $(document).ready() block. In addition the function you call will get an event object that you can use to get any values you might need from the markup.

answered Apr 17, 2013 at 2:15
Sign up to request clarification or add additional context in comments.

Comments

0

Because it is private. You are hiding it from global scope since it is inside the ready function. Do not use inline event handlers, use on() to attach events!

answered Apr 17, 2013 at 2:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.