Blogger Information
Blog 42
fans 5
comment 0
visits 38502
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jq ajax提交
张浩刚
Original
918 people have browsed it

post前端部分(ajax)

  1. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  2. <form>
  3. <p><label for="user">姓名:<input type="text" name="user" id="user"></label></p>
  4. <p><label for="password">密码:<input type="password" name="password" id="password"></label></p>
  5. <button onclick="ajax()" type="button">提交</button> //type='button'才能ajax
  6. </form>
  7. //post提交
  8. <script>
  9. function ajax(){
  10. //serialize() 获取所有能填写的数据的input等
  11. $.post('/demo.php',$('form').serialize(),function(reg){
  12. if(reg.code>0){
  13. console.log(reg.msg);
  14. }else{
  15. console.log(reg.msg);
  16. }
  17. },'json')
  18. }
  19. </script>

post后端部分(ajax)

  1. class users
  2. {
  3. public function show()
  4. {
  5. $user = $_POST['user'];
  6. $password = $_POST['password'];
  7. if($user !== 'admin'){
  8. $date = json_encode(['code'=>1; 'msg'=>'账户不正确']); //转换json数组,才可返回
  9. die($date);
  10. }
  11. if($password !== '123456'){
  12. $date = json_encode(['code'=>1; 'msg'=>'密码错误']);
  13. die($date);
  14. }
  15. $date = json_encode(['code'=>0; 'msg'=>'登陆成功']);
  16. die($date);
  17. }
  18. }
  19. $user = new users;
  20. $user->show();

get前端部分(ajax)

  1. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  2. <form>
  3. <p><label for="user">姓名:<input type="text" name="user" id="user"></label></p>
  4. <p><label for="password">密码:<input type="password" name="password" id="password"></label></p>
  5. <button onclick="ajax()" type="button">提交</button> //type='button'才能ajax
  6. </form>
  7. //post提交
  8. <script>
  9. function ajax(){
  10. //serialize() 获取所有能填写的数据的input等
  11. //$.get('/demo.php?user=user&password=password',function(reg){},'json')
  12. $.get('/demo.php',$('form').serialize(),function(reg){
  13. if(reg.code>0){
  14. console.log(reg.msg);
  15. }else{
  16. console.log(reg.msg);
  17. }
  18. },'json')
  19. }
  20. </script>

get后端部分(ajax)

  1. class users
  2. {
  3. public function show()
  4. {
  5. $user = $_GET['user'];
  6. $password = $_GET['password'];
  7. if($user !== 'admin'){
  8. $date = json_encode(['code'=>1; 'msg'=>'账户不正确']); //转换json数组,才可返回
  9. die($date);
  10. }
  11. if($password !== '123456'){
  12. $date = json_encode(['code'=>1; 'msg'=>'密码错误']);
  13. die($date);
  14. }
  15. $date = json_encode(['code'=>0; 'msg'=>'登陆成功']);
  16. die($date);
  17. }
  18. }
  19. $user = new users;
  20. $user->show();
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!