使用php脚本验证访问者的年龄,并在不刷新的情况下根据结果加载iframe


Using a php script to verify age of a visitor and loading an iframe based on result without refresh

是的,这是一个很长的标题,让我解释一下。

我想做的有点复杂,我有一个网页,我想在那里验证访问者的年龄,根据验证结果,我想把一个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");