通过AJAX和PHP,提交JQuery Mobile表单(两篇)
发布时间:2020-12-16 01:30:30 所属栏目:百科 来源:网络整理
导读:File name: callajax.php[php] view plaincopy ?php $firstName = $_POST[firstName]; $lastName = $_POST[lastName]; echo("First Name: " . $firstName . " Last Name: " . $lastName); ? File name: index.php[html] view plaincopy !DOCTYPE html html h
File name: callajax.php [php] view plaincopy <?php $firstName = $_POST[firstName]; $lastName = $_POST[lastName]; echo("First Name: " . $firstName . " Last Name: " . $lastName); ?> File name: index.php [html] view plaincopy <!DOCTYPE html> <html> <head> <title>Submit a form via AJAX</title> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" /> <script src="http://code.jquery.com/jquery-1.5.2.min.js"></script> <script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js"></script> </head> <body> <script> function onSuccess(data,status) { data = $.trim(data); $("#notification").text(data); } function onError(data,status) { // handle an error } $(document).ready(function() { $("#submit").click(function(){ var formData = $("#callAjaxForm").serialize(); $.ajax({ type: "POST",url: "callajax.php",cache: false,data: formData,success: onSuccess,error: onError }); return false; }); }); </script> <!-- call ajax page --> <div data-role="page" id="callAjaxPage"> <div data-role="header"> <h1>Call Ajax</h1> </div> <div data-role="content"> <form id="callAjaxForm"> <div data-role="fieldcontain"> <label for="firstName">First Name</label> <input type="text" name="firstName" id="firstName" value="" /> <label for="lastName">Last Name</label> <input type="text" name="lastName" id="lastName" value="" /> <h3 id="notification"></h3> <button data-theme="b" id="submit" type="submit">Submit</button> </div> </form> </div> <div data-role="footer"> <h1>GiantFlyingSaucer</h1> </div> </div> </body> </html> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ jQuery mobile 表单提交 http://blog.csdn.net/tjpu_lin/article/details/28394253 最近在做手机页面用jQuery mobile开发,在用到form表单提交到时遇到了问题。 后台是用servlet进行处理的,想通过Servlet里面的重定向进行页面跳转发现在手机上根本没有用,出现errorpage提示信息。 查询网上资料以及jQuery mobile API得知 jQuery mobile表单提交默认是ajax提交,所以页面跳转写在servlet里面根本就不会实现页面跳转功能。 于是我按照教程在form里面加了 data-ajax=“false”这一属性,发现别说是页面跳转不行,就连后台的数据库操作都做不了,报了500错误。 想了好久既然用ajax提交,那么就用ajax进行页面跳转 [html] view plaincopy在CODE上查看代码片派生到我的代码片 <script type="text/javascript"> $(document).ready(function () { $("#submitbtn").click(function(){ cache: false,$.ajax({ type: "POST",url: "feedback",data: $('#feedbackform').serialize(),success:function(data){ $.mobile.changePage("success.html"); } }); }); }); </script> [html] view plaincopy在CODE上查看代码片派生到我的代码片 <form method="post" id="feedbackform"> t;span style="white-space:pre"> </span>//相关表单元素 <input type="button" id="submitbtn" value="提交"> </form> 注意的是js里面的data [html] view plaincopy在CODE上查看代码片派生到我的代码片 $('#feedbackform').serialize() 是必须要有的,不然servlet里面用requset.getParameter接受的数据是null,ajax和后台成功交互后用changePage跳转到成功后显示的页面。 </pre><pre code_snippet_id="1849295" snippet_file_name="blog_20160824_1_2775332" name="code" class="html"> <dt style="list-style: none; word-break: break-all; word-wrap: break-word; margin: 0px; padding: 4px 0px 0px 7px; float: left; width: 77px; height: 60px; font-family: Verdana,'Lucida Grande',Arial,Helvetica,sans-serif;"><a target=_blank href="http://my.csdn.net/" style="list-style: none; text-decoration: none; word-break: break-all; word-wrap: break-word; color: rgb(59,89,152); margin: 0px; padding: 0px;"><img src="http://avatar.csdn.net/2/A/1/3_u011447828.jpg" alt="u011447828" style="list-style: none; word-break: break-all; word-wrap: break-word; border: none; margin: 0px; padding: 0px; width: 48px; height: 48px;" /></a></dt><dd style="list-style: none; word-break: break-all; word-wrap: break-word; margin: 0px 0px 0px -10px; padding: 4px 0px 0px; float: left; width: 500px; font-family: Verdana,sans-serif;"><ul style="list-style: none; word-break: break-all; word-wrap: break-word; margin: 0px; padding: 0px;"><li class="user_name" style="list-style: none; word-break: break-all; word-wrap: break-word; margin: 0px; padding: 0px; font-stretch: normal; font-size: 14px; line-height: 24px; font-family: Arial,sans-serif;"><a target=_blank href="http://my.csdn.net/" class="user_name" style="list-style: none; text-decoration: none; word-break: break-all; word-wrap: break-word; color: rgb(68,68,68); margin: 0px 1em 0px 0px; padding: 0px; font-weight: bold; font-stretch: normal;">afterrains</a><span style="list-style: none; word-break: break-all; word-wrap: break-word; margin: 0px; padding: 0px; color: rgb(58,156,211);">别在熬夜了!</span></li><li class="feed_link" style="list-style: none; word-break: break-all; word-wrap: break-word; margin: 0px 0px 0px -5px; padding: 0px; color: rgb(170,170,170); font-stretch: normal; line-height: 24px; font-family: Arial,sans-serif;"><a target=_blank href="http://my.csdn.net/" style="list-style: none; text-decoration: none; word-break: break-all; word-wrap: break-word; color: rgb(7,81,154); margin: 0px 6px; padding: 0px;">个人主页</a>|<a target=_blank href="http://blog.csdn.net/u011447828" style="list-style: none; text-decoration: none; word-break: break-all; word-wrap: break-word; color: rgb(7,154); margin: 0px 6px; padding: 0px;">我的博客</a></li></ul></dd> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |