是的,这是一个很长的标题,让我解释一下。
我想要做的有点复杂,我有一个网页,我想在其中验证访问者的年龄,并根据验证的结果,我想在同一页面上加载 iframe。
因此,我将使用 jquery 隐藏 iframe 包装器,根据验证结果设置 iframe URL,然后再次使用 jquery,显示具有正确 URL 的 iframe。
我完全陷入困境,我不知道如何在不刷新页面的情况下执行此操作。
请允许我展示我当前编写的内容,它无法正常运行,但它可能会给您对我正在尝试做的事情的正确印象。
index.php:
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).onload(function(){
$("#iframe").hide();
});
$(document).ready(function(){
$('#myForm').on('submit',function(e) {
$.ajax({
url:'action.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#iframe").show(); //=== Show Success Message==
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
});
});
</script>
</head>
<form>
Please input your date of birth:
<input type="date" name="dob"><br>
<input type="submit" name="submit" value="Verify Age" />
</form>
<div id="iframe">
<iframe src="<?php include 'action.php'; $iframe ?>"></iframe>
</div>
action.php:
<?php
$minAge = 18; // You might read this from a file/database.
$minAge *= 3600*24*365.25; // $minAge in seconds
if(isset($_POST['submit'])){
$birth_date = strtotime($_POST['dob']);
$now = strtotime("now");
$age = $now - $birth_date; // age is in seconds
if($age > $minAge)
$iframe_url = 'http://URL1.COM';
else
$iframe_url = 'http://URL2.COM';
} ?>
我试图做的是根据验证设置 iframe URL,然后包含该 php 以在 iframe 中显示正确的 URL。
请有人理解我想要什么,如果你能够提供帮助,我将永远感激不已:)
最佳答案
如果您不想刷新页面,则不需要action.php。 将提交按钮类型设置为“仅按钮”。 单击按钮时,将值传递给 JavaScript 函数,并在此函数中验证年龄并相应地显示 iframe。
根据年龄设置iframe url的代码:$("#myiframe").attr("src", "newwebpage.html");
关于javascript - 使用 php 脚本验证访问者的年龄并根据结果加载 iframe,无需刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29474755/