简体   繁体   中英

Pass value from iFrame to parent's php variable

I'm not too good with JS, I've been searching for an hour but cannot find a solution I can alter to work. In my parent frame I have this

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

in my iFrame I have

$starter_name_available = "0";

( 0 could be a different value depending on input from parent) Now, the iFrame is refreshed when a user types in some details on the parent, this determines the value of $starter_name_available as well as other things, how can I pass the value of $starter_name_available back to it's parent? Nothing is being interacted with on the iframe. I cannot get iFrames working with JS Fiddle so here is more code to help understand: parent 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;
}

parent 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 code:

<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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM