1. 程式人生 > >Tp5提交form表單

Tp5提交form表單

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title></title>  
        <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>  
        <script>  
              
            function ajaxPost(){  
                var formData = $("#myform").serialize();  
                //serialize() 方法通過序列化表單值,建立 URL 編碼文字字串,這個是jquery提供的方法  
                $.ajax({  
                    type:"post",  
                    url:"{:url('Index/index/test')}",  
                    data:formData,//這裡data傳遞過去的是序列化以後的字串  
                    success:function(data){  
                        $("#content").append(data);//獲取成功以後輸出返回值  
                    }  
                });  
            }  
              
        </script>  
    </head>  
    <body>  
          
        <form id="myform"><!--這裡給表單起個id用於獲取表單並序列化-->  
            <input type="text" name="mess" />  
            <input type="text" name="id" />  
            <button onclick="ajaxPost()">---------</button>  
        </form>  
        <div id="content">  
              
        </div>  
    </body>  
</html>

 

//控制器方法

public function test($mess,$id){  
        if($mess == '123'){  
            return json("ajax成功!".$mess."---".$id);  
        }else{  
            return json("你輸出的是其他值:".$mess."---".$id);  
        }  
    }