从html到jquery再到php,再从php到jquery到html,将数据传递到php文件并将数据获取到php文件中


pass data and get data into php file from html to jquery to php and then from php to jquery to html

好的,我有这个代码

HTML:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">                      </script>
<script type="text/javascript">
function get() {
$.post('tt.php',
{ 
$('#name')
},
    function(output) {
        $('#age').html(output).show();
        });
}
</script>
</head>
<body>
<input type="text" id="name" name="name"/>
<a href="javascript:get()">haller</a>
<div id="age"></div>
</body>
</html>

然后是这个php文件。

PHP:

<?
$name=$_POST['name'];
if ($name=="juliver")
{
echo "haller";
}
elseif ($name==NULL)
{
echo "Please put a name!";
}
else
{
echo "wrong name";
}
?>

场景是这样的,无论输入有什么值,它都会使用jquery传递到php文件中,当然,正如你所看到的,每当用户点击具有get()函数的锚标记时,php文件就会处理数据(执行),然后将其传递回jquery,然后jquery就会将数据输出到html中。到目前为止,我还不能让这件事成功。请帮帮我。提前谢谢。

function get() {
    $.post('tt.php', { name : $('#name').val() }, function(output) {
        $('#age').html(output).show();
    });
}

不要使用函数绑定事件,可以使用jQuery的原生点击函数。但是,您必须确保在链接中添加类名,并将其指定为选择器:

JavaScript

$('a.class_name').on('click', function() {
   $.post('tt.php', { name: $('#name').val() }, function(data) {
      $('#age').html(data);
   });
});

PHP

if(isset($_POST['name']))
{
   $name=$_POST['name'];
   if($name == "juliver")
   {
      echo "haller";
   }
   elseif ($name==NULL)
   {
      echo "Please put a name!";
   }
   else
   {
      echo "wrong name";
   }
}