2014-04-23 81 views
0

我把代码从W3学校方面jQuery的Ajax和修改稍微:简单的jQuery AJAX的例子:变量

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"> 
</script> 
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    $("#div1").load("test/test.php"); 
    }); 
}); 
</script> 
</head> 
<body> 
<? $a=5; $b=5; $a + $b = $c;?> 
<div id="div1"><h2>Let jQuery AJAX Change This Text???</h2></div> 
<button>Get External Content</button> 

</body> 
</html> 

在我的test.php的我这个小行:

<h2>The answer is: <?=$c?></h2> 

当我点击按钮,虽然我没有得到任何东西,只是“答案是:”

我知道这是愚蠢的简单,但我是JQuery的AJAX新手,并找不到解决方案,回答这个问题。这更多的是我的理解是一个真正的基于项目的问题。

Thankss

+1

你从来没有定义在''test.php' $ C' ...'test.php'不能访问瓦尔在'指数.php“,除非你明确地将它包含在'test.php'中(在这种情况下你不想这么做)。 –

+0

@KevinB那么我该怎么做才能获得这种效果?如果我有上面的变量...将不得不创建一个新的文件,其中的逻辑处理,然后包括它test.php? – Shade

+0

你只要移动'<? $ 1 = 5; $ B = 5; $ a + $ b = $ c;?>'test.php。 –

回答

1

正如上面指出的,你不能使用jQuery load方法访问test.php的一个页面的局部变量。如果您想将$ c变量的值传递给test.php,您可以通过将其作为get变量或使用ajax进行传递来实现。

请看下面的例子:

您的客户端文件:

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">  </script> 
<?php 
$a=5; 
$b=5; 
$c = $a + $b; 
?> 
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    $("#div1").load("test.php?c=<?php echo $c; ?>"); 
    }); 
}); 
</script> 
</head> 

<body> 
<div id="div1"><h2>Let jQuery AJAX Change This Text???</h2></div> 
<button>Get External Content</button> 

</body> 
</html> 

你的服务器端的文件:

<h2>The answer is: <?php echo $_GET['c']; ?></h2> 
0

您指定通过这些标签

<?php some code here ?> 

而且你会想回声出值代码是php。

<h2>The answer is: <?php echo $c ?></h2> 

这显然也适用于a,b和c变量的赋值。此外,分配变量在PHP中从左到右为:

$c = $a + $b; 
+0

不...还是不行。仍然得到“答案是:”[空白]。还有其他建议吗?你可以直接观看它:http://crinno.com/test/ – Shade

1

根据您的要求,您可以使用任何的三种方法:

get,post或ajax

<html> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script> 

    function readyFunction() { 
     $('#result').html("All iss well.."); 
     $.ajax({ 
      url: "ajax_response.php", 
      method: "POST", 
      data: {id: '12345', 'name': 'AJAX'}, 
      cache: true 
     }).done(function(data){ 
      data = $.parseJSON(data); 
      console.log("I am in done..." + data.id); 
     }).success(function(data){ 
      console.log("I am in success..." + data); 
     }).fail(function(data){ 
      console.log("I am in fail..." + data); 
     }).always(function(data){ 
      console.log("I am in always..." + data); 
     }); 

     $.post({ 
      url: "ajax_response.php", 
      data: {id: '12345', 'name': 'POST'}, 
      cache: true 
     }).done(function(data){ 
      data = $.parseJSON(data); 
      console.log("I am in done..." + data.id); 
     }).success(function(data){ 
      console.log("I am in success..." + data); 
     }).fail(function(data){ 
      console.log("I am in fail..." + data); 
     }).always(function(data){ 
      console.log("I am in always..." + data); 
     }); 

     $.get({ 
      url: "ajax_response.php", 
      data: {id: '12345', 'name': 'GET'}, 
      method: "POST", 
      cache: true 
     }).done(function(data){ 
      data = $.parseJSON(data); 
      console.log("I am in done..." + data.id); 
     }).success(function(data){ 
      console.log("I am in success..." + data); 
     }).fail(function(data){ 
      console.log("I am in fail..." + data); 
     }).always(function(data){ 
      console.log("I am in always..." + data); 
     }); 
    } 

    $(document).ready(readyFunction); 

</script> 

ajax_response.php

<?php 
    echo json_encode($_REQUEST); 
?>