gpt4 book ai didi

javascript - PHP在同一页面显示结果

转载 作者:行者123 更新时间:2023-12-03 08:48:34 26 4
gpt4 key购买 nike

 <form action="" method="post">
<div id="wrapper">
<p>
<label> Please Enter the JCID No </label>
<input type="text" name="txt_jcid_no" id="txt_jcid_no"/>
<input type="submit" name="submit" value="Search" class="bg-primary"/>
</p>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th style="display: none"></th>
<th style="display: none"> </th>
<th style="display: none"></th>
</tr>
</thead>
<?php
if (isset($_POST['submit'])) {
echo 'Hi';
echo '<tbody>';
echo '</tbody>';
}
?>
</table>
<!-- /#page-content-wrapper -->
</div>
</form>

亲爱的 friend ,我希望我的 php 代码在同一页面上显示该值,所以我在这里所做的是从用户那里获取 input(txt_jcid_no) 并处理查询并在表中显示值格式 。但是当我使用 if isset() 函数时,它会在页面加载时显示值,即在单击 submit 按钮之前,但我的要求是单击后该值应显示在表格中提交按钮。

最佳答案

最好使用 ajax 来完成此操作。您仍然可以将所有代码放在一个页面中,然后让 ajax 调用该页面的第二个副本,如下所示:

Working demo

<?php
// at the top of your file have your response logic
if (isset($_POST['txt_jcid_no'])){
echo '<tbody><tr><td colspan="6">I was echoed here because you submitted the form</td></tr></tbody>';
exit; // dont load the rest of the page if this was hit, we just want the above to be returned
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<form action="index.php" method="post">
<div id="wrapper">
<p>
<label> Please Enter the JCID No </label>
<input type="text" name="txt_jcid_no" id="txt_jcid_no">
</p>
</form>
<input type="button" value="Search" id="submit" class="bg-primary">
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tbody id="result">
</tbody>
</table>
</div>
</div>
<!-- /#page-content-wrapper -->
</div>
<script>
$(function(){
$('#submit').click(function(){
var dataString = 'txt_jcid_no=' + $('#txt_jcid_no').val();;

$.ajax({
type: "POST",
url: "withajax.php",
data: dataString,
success: function(result) {
$('#result').html(result);
},
error: function() {
}
});


});
});
</script>
</body>
</html>
<小时/>

如果您确实想通过发布表单来完成此操作,则可以这样做:

Working demo

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form action="index.php" method="post">
<div id="wrapper">
<p>
<label> Please Enter the JCID No </label>
<input type="text" name="txt_jcid_no" id="txt_jcid_no">
<input type="submit" name="submit" value="Search" class="bg-primary">
</p>
</form>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th style="display: none"></th>
<th style="display: none"> </th>
<th style="display: none"></th>
</tr>
</thead>
<?php
if (isset($_POST['submit']))
{
echo '<tbody><tr><td colspan="6">I was echoed here because you submitted the form</td></tr></tbody>';
}
?>
</table>
</div>
</div>
<!-- /#page-content-wrapper -->
</div>
</body>
</html>

关于javascript - PHP在同一页面显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com