4

I am trying to send a php file some values using ajax but in the call for ajax I am getting the following error

Uncaught ReferenceError: $ is not defined 

at the beginning line for the ajax request as follows:

$.ajax({
  type: "POST",
  url: 'program3.php',
  data: {
    player1name: player1name.value,
    player2name: player2name.value,
    playtopoints: playtopoints.value,
    delay: delay.value,
    numgames: numgames.value,
    gamesplayed: gamesplayed.value,
    p1turn: p1turn.value,
    p2turn: p2turn.value,
    p1total: p1total.value,
    p2total: p2total.value
  },
  success: function (data) {
    rolling = data;
  }
});            

I first thought that it might need the refrence to ajax so i added the following line before the javascript on the html page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

but i am still getting the erro can anyone offer any insight?

Also i have the data variables all defined as follow:

var player1name = document.JForm.p1name.innerHTML;

is that the correct way to assign them?

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
user2793027
  • 83
  • 1
  • 3
  • 15

2 Answers2

7

The src on your script tag is invalid—at least if you're not running this from http or https. Replace

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

with

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • well take a look at this, this has nothing to do with his problem at all: http://stackoverflow.com/questions/9646407/two-forward-slashes-in-a-url-src-href-attribute double slashes helps to avoid http/https mess, it will work for both. – holms Oct 24 '13 at 23:00
  • It should have worked with just the slashes. See this post.. http://stackoverflow.com/questions/9646407/two-forward-slashes-in-a-url-src-href-attribute – Charlie Martin Oct 24 '13 at 23:03
  • 1
    @CharlieMartin: Not if you're using your local file system; you must specify the protocol in that scenario. – Adrift Oct 24 '13 at 23:05
  • `//` will try to use the same protocol of the current page, if its `file://` or `ftp://` it will fail. – Havenard Oct 24 '13 at 23:06
  • @Charlie - I actually didn't know about `//` though - thanks for the info. – Adam Rackis Oct 24 '13 at 23:09
  • Ah yes, the local file protocal fails. Nice catch. – Charlie Martin Oct 24 '13 at 23:14
3

You are probably accessing the file locally, which won't work with a protocol-relative script tag.

<!-- access from http resolves to this -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- local access resolves to this -->
<script src="file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

The file wouldn't have existed locally, and the script would've never been loaded. Therefore, the variable $ would then be undefined.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162