我看网上说是用ajax,但是我不会用,求一段jquery $.ajax()方法写的一个小例子

解决方案 »

  1.   

    PHP run on server. JQuery(javascript) run on browser. That's different.
      

  2.   

    测试过了直接用,把文件也保存为utf-8格式的,中文也不会乱码,希望对你有用,jquery要包含,
    例子中我包含到线上的http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
    ,实际从官网下载//a.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <title> ajax 提交 </title>
      <meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 
     </head> <body><input name="txt_name" id="txt_name" type="text" value="">
    <input name="txt_age"  id="txt_age" type="text" value="">
    <input type="button" onclick="btnClick();" value="提交"><script type="text/javascript"> 

    function btnClick(){
    var txtName = $('#txt_name').val();
    var txtAge = $('#txt_age').val();
    dataVal={txt_name:txtName,txt_age:txtAge};
    $.ajax({
       type: "GET",
       url: "a.php",
       data:dataVal,
      // data: "name=John&location=Boston",
       success: function(msg){
    alert(msg);

       }
    }); 
    }
    </script>
     </body>
    </html>//a.php
    //打印出从a.html传递过来的表单数据,用来测试用,
    //实际中做相应处理,如提交到表操作等
    var_dump($_GET);
      

  3.   

    恩 看到了 谢谢 我还想弄明白,我不传值,只是想点击之后执行一句sql语句写入数据库,比方说我前台有个按钮叫新生入库(就是把所有报到的学生信息载入到学生信息表中),后台有段php代码我想不跳转页面不刷新的前提下直接点击就执行这个操作
      

  4.   

    不跳转的话就是ajax异步传输喽。花半天时间看看吧http://www.w3school.com.cn/ajax/index.asp
      

  5.   

    AJAX吧。不然就是要客户端的DLL
      

  6.   

    http://www.w3school.com.cn/ajax/index.asp   里面的例子很详细。另外你可以看看php100.com里面有专门的关于jquery  ajax的视频,讲的比较详细,好像是146节左右。
      

  7.   

    兄弟赶紧耐心学习吧。这些问题都是基本的码。不会这些,怎么混饭吃。
    例子,有没有错我的就不知道了:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6/jquery.js"></script>
     
    </head><body>

    <input type="button" id="submit" value="提交">
    <script type="text/javascript"> 
        $("#submit").click(function(){
    $.ajax({url: 'p.php', 
    type: 'GET',
    dataType: 'html', 
    timeout: 1000,  error: function(){alert('Error');},   success: function(result){alert(result);}   });  
    return false;
    });
         
    </script>
    </body>
    </html><?php$link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    //mysql_select_db('mydb');//mysql_query("INSERT INTO mytable (product) values ('kossu')"); 
    exit("ok");
      

  8.   


    <!-- 当前页面xxx.php -->
    <?php
    if(isset($_GET['new'])){
      $sql="……";
      ……
    }
    ?>
    <form name="form" action="xxx.php">
      <input type="button" name="new" value="新生入库" onclick="location.href='xxx.php?new=1'" />
    </form>
      

  9.   

    用异步传输可以实现。流行点的就用ajax,去下个jquery的库,使用很简单的。
    如果不想麻烦,也可以用旧式的,就是在页面中使用iframe的方式实现。
      

  10.   

    <script type="text/javascript"> 
        $("#submit").click(function(){
                $.ajax({url: 'p.php', 
                type: 'GET',
                dataType: 'html', 
                timeout: 1000, 
                error: function(){alert('Error');},              success: function(result){alert(result);}              });  
                return false;
            });
         
    </script>