php – 使用表单元素多次更新数据库
发布时间:2020-12-13 17:13:47 所属栏目:PHP教程 来源:网络整理
导读:我一直在为医生办公室工作.我很擅长使用 HTML / CSS和一些 PHP,但我仍在尝试学习 Javascript / JQuery. 因此,描述我的问题最简单的方法(我的问题,至少对我而言,非常复杂)是一个图像(如果这不是描述这个的最佳方式,请告诉我,我会更详细地描述它): 我有PHP代
我一直在为医生办公室工作.我很擅长使用
HTML / CSS和一些
PHP,但我仍在尝试学习
Javascript / JQuery.
因此,描述我的问题最简单的方法(我的问题,至少对我而言,非常复杂)是一个图像(如果这不是描述这个的最佳方式,请告诉我,我会更详细地描述它): 我有PHP代码,它接受传递给它的值(即位置编号),并将位置编号与用户时间一起插入数据库. 这是PHP代码: <?php date_default_timezone_set('America/Denver'); $apptid = $_REQUEST['apptid']; $currentlocation = $_REQUEST['currentlocation']; $currentlocationstart = date("Y-m-d H:i:s"); /*** mysql hostname ***/ $hostname = '******'; /*** mysql username ***/ $username = '***********'; /*** mysql password ***/ $password = '***************'; $conn = new PDO("mysql:host=$hostname;dbname=sv",$username,$password); /*** The SQL SELECT statement ***/ $sql = "UPDATE schedule SET currentlocation = ?,currentlocationstart = ? WHERE apptid= ? "; $q = $conn->prepare($sql); $q->execute(array($currentlocation,$currentlocationstart,$apptid)); ?> 这是html代码(填充了一点php,填充): $sql = "SELECT * FROM locations order by patientlocation ASC"; echo "<td><select name=location>"; foreach ($dbloc->query($sql) as $row2) { echo "<option>".$row2['patientlocation']."</option>"; } echo "<option value='Check Out'>Check Out</option>"; echo "</select></td>"; 所以我遇到的问题是如何使用Javascript / JQuery,如果我的PHP将使用Javascript / Jquery.我还想存储从最初的“签到”按钮到用户点击结账时的所有内容. 另一个问题是我需要使用AJAX自动执行此操作,而不是多次刷新页面. 非常感谢所有人和任何帮助.如果我没有充分解释我的问题,我会填写更多细节! 解决方法
首先只是一个简单的问题,你是否能够将这些字段变成一种形式而不是让它们在变化中消失?
这样,当他们点击签入时,您可以显示表单,填写表单,当他们按表单底部的结帐时,我们可以使用AJAX提交表单并显示响应. 所以这将是这样的: <script type="text/javascript"> $(document).ready(function() { // Hide the form on load $('#myForm').hide(); // Hide the completed message $('#finished').show(); }); // When someone clicks checkin this function will be called $('#checkIn').click(function(){ $e = $(this); $.ajax({ type: "POST",url: "[URL OF PHP PAGE HERE]",// this page should add date to the database data: "checkIn="+$(this).val(),// This will send $_POST['checkIn'] success: function(){ // Display the form once the AJAX is finished $('#myForm').show(); } }); }); // This function will be called when someone uses the select field $('.[yourClassName]').change(function(){ $e = $(this); $.ajax({ type: "POST",data: "yourFieldName="+$(this).val(),success: function(){ // Display the form once the AJAX is finished alert('Done'); } }); }); // When someone clicks checkOut this function will be called $('#checkOut').click(function(){ $e = $(this); $.ajax({ type: "POST",// this page should save data to db data: "example="+$('select#exampleId').val(),success: function(){ // Hide the form again $('#myForm').hide(); // Show the finished div with your success msg $('#finished').show(); } }); }); </script> <input type="button" value="Check In" id="checkIn" /> <form method="post" id="myForm" action=""> // Build the rest of your form here <select name="example" id="exampleId"> <option value="1">Test</option> </select> <input type="button" value="Check Out" id="checkOut" /> </form> <div id="finished" style="color:#ff0000;">Checked Out</div> 那应该是这样,除了你当然需要建立你自己的形式,我的例子是.使用PHP页面发送AJAX,这应该是一个普通的PHP页面,接受$_POST数据就像正常发布表单一样. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |