javascript - 执行php而不刷新页面
问题描述
我想在单击提交按钮时通过 php 执行 javascript 代码......这段代码可以执行但页面刷新......我不希望页面刷新......我可以实现吗它通过ajax或jquery以及如何?
<html>
<head>
<style>
.popup
{
visibility:hidden;
}
</style>
</head>
<body>
<form method="post">
<div id="Popup" class="popup">
Sorry! The vehicle is already booked on the corresponding date.
Select a different date or book another vehicle.
</div>
<input type="submit" name="submit" value="display">
</form>
<?php
if(isset($_POST["submit"]))
{
echo '<script>document.getElementById("Popup").style.visibility="visible";</script>';
}
?>
</body>
</html>
解决方案
你不能用 PHP 做到这一点。正如 PaulProgrammer 所说,PHP 只能在生成页面时执行,因为它是一种服务器端语言。您需要一种客户端语言(例如 JavaScript)来执行此操作。这是您可以尝试的代码示例。不要只是举个例子,而是要了解每个组件的作用:
<html>
<head>
<style>
#popup
{
display:none;
}
</style>
</head>
<body>
<button id="showbtn">Show Popup</button>
<div id="popup">
Sorry!The vehicle is already booked on the corresponding date.
Select a different date or book another vehicle.
</div>
<script>
const mypopup = document.getElementById("popup");
const mybutton = document.getElementById("showbtn");
mybutton.addEventListener('click', function(event) {
mypopup.style.display = "block";
})
</script>
</body>
</html>
推荐阅读
- python - 基于 Pandas 中的两列的分桶?
- java - 绘制平行线段
- python - 命名模块“random.py”会导致 numpy 属性错误
- javascript - react JS中的私有路由jsx
- vb.net - 替换word文件中的图片
- matlab - 未能在库“powerlib”中找到“电力电子/全桥转换器”?
- elasticsearch - 基于文本相似度的 Elasticsearch 折叠
- amazon-s3 - 无法通过 CDK 创建具有 KMS_MANAGED 密钥和 bucketKeyEneabled 的 S3 存储桶
- google-bigquery - 尝试从 AVRO 文件创建表时 BQ 加载作业失败
- python - 使用 Python 删除控制字符