Reputation: 403
im new to javascript, but have to use it for an ajax load of a .php
This is my ajax.js
$(document).ready(function(){
var url = $(location).attr('href');
var uA = navigator.userAgent;
$.ajax({
type: "POST",
url: "neo4j.php",
data: {"url": url, "userAgent": uA}
});
alert(url);
});
It should post the datas to neo4j.php.
My Start.php looks like
<head><title>Start</title></head>
<?php
include("db.php");
$sql = "SELECT * FROM category_paths LIMIT 10";
$pattern = mysql_query($sql);
while($row = mysql_fetch_object($pattern)){
if ($row->descendant_id == 1) {
echo "<a href='http://localhost/2play/Start.php'>$row->descendant_id</a><br>";
}else {
echo "<a href='http://localhost/2play/Section.php/?sec=$row->descendant_id'>$row->descendant_id</a><br>";
}
}
?>
<footer><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="ajax.js"></script></footer>
Here my code works.
But if I click on a link to section.php
<head><title>Section</title></head>
<?php
include("db.php");
echo "<a href='http://localhost/2play/Start.php'>1</a><br>";
if(isset($_GET["sec"])) {
$sec = $_GET["sec"];
}
$sql = "SELECT * FROM category_paths WHERE ancestor_id = $sec AND length = 1";
$pattern = mysql_query($sql);
while($row = mysql_fetch_object($pattern)){
echo "<a href='http://localhost/2play/Game.php/?game=$row->descendant_id'>$row->descendant_id</a><br>";
}
?>
<div id="js">test</div>
<footer><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
</footer>
My ajax.js dont load the js code, instead it load the html-code of the current page. Copy from loaded section.php with firebug:
<html>
<head>
<title>Section</title>
</head>
<body>
<a href="http://localhost/2play/Start.php">1</a>
<br>
<a href="http://localhost/2play/Game.php/?game=123">123</a>
<br>
<a href="http://localhost/2play/Game.php/?game=124">124</a>
<br>
<a href="http://localhost/2play/Game.php/?game=125">125</a>
<br>
<a href="http://localhost/2play/Game.php/?game=156">156</a>
<br>
<a href="http://localhost/2play/Game.php/?game=197">197</a>
<br>
<footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="ajax.js" type="text/javascript">
<--Here the script should be loaded, but it relaods the raw non-php code from section.php-->
<head><title>Section</title></head>
<a href='http://localhost/2play/Start.php'>1</a><br>
<footer><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
</footer>
<--End-->
</script>
</footer>
</body>
</html>
What I already found out: If I manually add some get-vars to the Start.php url like
http://localhost/Start.php?foo=bar
the script wont work and do some failure as above.
SOLVED Ok I found my mistake, and cant believe it. It is because i had an slash in the link before I set the get-vars http://localhost/2play/Section.php**/**?sec=$row->descendant_id'> . After deleting it, the problem disappeared.
Upvotes: 0
Views: 147
Reputation: 403
Ok I found my mistake, and cant believe it. It is because i had a slash in the link before I set the get-vars
http://localhost/2play/Section.php**/**?sec=$row->descendant_id'>
After deleting it, the problems disappeared.
Upvotes: 1
Reputation: 12039
You should nod declare variable inside $.ajax
. Try something like this
$(document).ready(function(){
var data1 = "foo",
data2 = "bar";
$.ajax({
type: "POST",
url: "foobar.php",
data: {"data1": data1, "data2": data2}
});
alert(data1);
});
More about AJAX can follow the link
Upvotes: 1