【问题标题】:Pass value from iFrame to parent's php variable将值从 iFrame 传递到父级的 php 变量
【发布时间】:2015-09-24 14:10:06
【问题描述】:

我对 JS 不太擅长,我一直在寻找一个小时,但找不到可以改变工作的解决方案。 在我的父框架中,我有这个

<input type="hidden" name="starter_name_available" id="starter_name_available">

在我的 iFrame 中有

$starter_name_available = "0";

0 可能是不同的值,具体取决于父级的输入) 现在,当用户在父项上输入一些详细信息时,iFrame 会刷新,这决定了$starter_name_available 的值以及其他内容,我如何将$starter_name_available 的值传递回它的父项? iframe 上没有任何交互。 我无法让 iFrames 与 JS Fiddle 一起使用,所以这里有更多代码可以帮助理解: 父 javascript:

function SLTUpdateIframe()
{
var first = document.getElementById("starter_first").value;
if (first == ""){
first = "xxx";
}
var concatString = "";
var url = concatString.concat("newcall/SLT/sltusercount.php?first=", first, "&last=", last);
document.getElementById('SLTiframe').src = url;
}

父 html:

<iframe src="newcall/SLT/sltusercount.php?first=xxx&last=xxx" name="SLTiframe" id="SLTiframe" seamless width="650px" height="45px" scrolling="no" frameborder="0"></iframe>

<label for="starter_first" class="label">First name</label><br />
<input type="text" class="textbox" name="starter_first" id="starter_first" onkeyup="SLTUpdateIframe()" />   
<input type="hidden" name="starter_name_available" id="starter_name_available">

iframe 代码:

<html>
    <head>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>
        <?
            $first = "xxx";
            if(isset($_GET['first'])){
                $first = strtolower($_GET['first']);
            }

            $last = "xxx";
            if(isset($_GET['last'])){
                $last = strtolower($_GET['last']);
            }
            $account = ">".$first.".".$last."@";
            //the > and the @ surrounding the name are there to ensure we are finding the whole name and not just a fragment
            // for example if we search Joe.Blo it shouldn't respond that it's found that account because Joe.Bloggs exists

            $userlist = file_get_contents('../../lists/aduserlist.php');
            $userlist = strtolower($userlist);
            if(strpos($userlist,$account) !== false) {
                if(!($account == '>xxx.xxx@')){
                    $first = ucfirst(strtolower($first));
                    $last = ucfirst(strtolower($last));
                    $account = $first.".".$last;
                    echo("<font face='Verdana' color='#303030' size='3'>There is already a user account with the name ".$account." </font>");
                    echo("<font face='Verdana' color='#f56a00' size='2'><br>Please try a different name (e.g. Steven to Steve) or add a number (e.g. John Smith2)</font>");
                    $starter_name_available = "0";

                }
            }else{
                if(!($account == '>xxx.xxx@')){
                    $first = ucfirst(strtolower($first));
                    $last = ucfirst(strtolower($last));
                    $account = $first.".".$last;
                    echo("<font face='Verdana' color='#303030' size='3'>The name ".$account." is available</font>");
                    $starter_name_available = "1";

                }   
            }
        ?>
    </body>
</head>

【问题讨论】:

  • 对我来说不是很清楚...更清楚地解释您的要求,或/并在jsfiddle.net做一些示例
  • 不像@Julo0sS 说的那么清楚,但是您是否尝试通过javascript访问父窗口?
  • 当用户在父节点上输入用户名时,iframe 会在每次击键时刷新,说明用户名是否可用。如果 iframe 说它不可用,我希望父框架中的变量用于表单验证
  • jsfiddle 是关键:P 你为什么不直接对父级进行可用性检查?
  • @Julo0sS 我不知道 jsfiddle 可以与 iFrame 一起使用,我在原来的调用中添加了更多内容,希望它可以提供帮助,谢谢

标签: javascript php html forms iframe


【解决方案1】:

终于弄明白了,感谢那些看的人,我在 if/else 语句的 iframe 中添加了这一行(更改每个中的 val)

echo("<script>$(document).ready(function(){ $('#starter_name_available', window.parent.document).val('1');});</script>");

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2011-08-27
    相关资源
    最近更新 更多