简体   繁体   English

在html文档中使用表单时,如何在同一页面中显示数据?

[英]How do i display data in the same page while using a form in a html doc?

I've used javascript and php to get data from the database and then posting it on the same page. 我使用javascript和php从数据库中获取数据,然后将其发布在同一页面上。 I've got the output for that. 我已经得到了输出。 But when i'm using tag, the data is not being retrieved instead the page is just being refreshed and nothing happens. 但是,当我使用标签时,不会检索数据,而是只是刷新页面而没有任何反应。 Can someone please help me in getting the output while using the tag. 有人可以在使用代码时帮助我获取输出吗? Thanks in advance. 提前致谢。

HTML FILE: HTML档案:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
Enter MMTI Number: <input type="text" name="EnrNo" id="EnrNo" /><br/><br />
<input type="submit" name="retrieve" value="submit" id="EnrNo-sub" />
<div id="EnrNo-data"></div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">

$('#EnrNo-sub').on('click', function() { 
var EnrNo = $('input#EnrNo').val();
if (EnrNo != '') {
$.post('retrieve.php', {EnrNo: EnrNo}, function(data) { 
    $('div#EnrNo-data').html(data);
});
}
});
</script>
</body>
</html>

PHP FILE: PHP文件:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'DB';
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) 
{
die("connection couldn't be established");
}
if(isset($_POST['EnrNo']) === true && empty($_POST['EnrNo']) === false) {
//$Enr = $_POST['EnrNo'];
$EnrNo = mysql_real_escape_string ($_POST['EnrNo']);
$query = "Select * FROM cert WHERE EnrNo = '$EnrNo'";
    $result = $db->query($query);
    $total_num_rows = $result->num_rows;
    while ($row=$result->fetch_array())
 {
  echo "Enr. No: MMTI- " .$row["EnrNo"].'<BR />';
  echo "Name: " .$row["Name"].'<BR />';
  echo "Batch Code: " .$row["Batch Code"].'<BR />'; 
  echo "Start Date: " .$row["Start Date"].'<BR />';
  echo "End Date: ".$row["End Date"].'<BR />';
  echo "Course: " .$row["Course"].'<BR />';
  echo "Duration: " .$row["Duration"].'<BR />';
  }  mysqli_free_result($result);
    } else {
        echo ('Data not found');
    };
?>

You need to stop form from submitting and refreshing page: You can use event.preventDefault() or return false on your event handler. 您需要停止表单的提交和刷新页面:您可以使用event.preventDefault()或在事件处理程序上return false

$(document).ready(function () {
    $('#EnrNo-sub').on('click', function (e) {
        e.preventDefault(); // Stop default action of submit form
        var EnrNo = $('input#EnrNo').val();
        if (EnrNo != '') {
            $.post('retrieve.php', {
                EnrNo: EnrNo
            }, function (data) {
                $('div#EnrNo-data').html(data);
            });
        }
    });
});

You could use a button instead of a submit for your button-element, or you could prevent the default action for the button in your click-Event handler: 您可以使用按钮而不是按钮元素来提交,或者可以在click-Event处理程序中阻止按钮的默认操作:

$('#EnrNo-sub').on('click', function(ev) {
    ev.preventDefault();
    var EnrNo = $('input#EnrNo').val();
    if (EnrNo != '') {
        $.post('retrieve.php', {EnrNo: EnrNo}, function(data) { 
            $('div#EnrNo-data').html(data);
        });
    }
});

You need to stop page refresh. 您需要停止页面刷新。 Your data is not updated because of page refresh. 由于页面刷新,您的数据未更新。

$('.click').click(function(e){
    e.preventDefault();
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it? 如何使用JavaScript在同一页面上显示包含名字,姓氏电子邮件和密码验证的表单数据 - How do I display a form data containing first name last name email and password validations in the same page using JavaScript 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何在同一页面的表格中显示表单数据 - How can I display form data in a table on the same page Flutter/Firestore:如何在 ListTile 中显示来自 doc 的数据? - Flutter/Firestore : How do I display data from doc in a ListTile? 如何使用 javascript 在单独的 html 页面上显示答案? - How do I display an answer on a separate html page using javascript? 如何使用 jQuery 在同一页面上重用 HTML 元素? - How do I reuse an HTML element on the same page using jQuery? 如何在不提交表单的情况下在同一页面中显示表单数据? - How I can display the form data in the same page without submit the form? 如何使用 ajaxcall 以 html 形式显示数据 - How to display data in html form using ajaxcall 使用 express 时如何访问表单中的 ejs 数据 - How do i access ejs data in a form while using express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM