在上一页中添加变量(指定文本字段)

时间:2012-04-06 00:01:19

标签: php javascript jquery html

我一直在寻找一种方法来从上一页的变量中添加信息(字符串)。

据我所知,应该可以使用javascript以某种方式。

新的无法使旧版本正常工作..

<script type="text/javascript">
function newPopup(url) {
    popupWindow = window.open(
        url,'popUpWindow','height=510,width=350,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
<a href="JavaScript:newPopup('(url)');">Armory Avatar</a>

我有一段代码可以打开链接到一个新的弹出窗口(它记住上一页的URL)。 在这个窗口中,人们可以插入一些关于魔兽角色和这个角色所在领域的信息。在他们这样做并点击提交后,该网站显示从暴风雪军械库中检索到的化身的网址。

http://eu.battle.net/static-render/eu/(imagepath)

弹出页面的代码:更新了当前代码(7-4-2012)

   <?php
if (!isset($_POST['submit'])) { 
?>
<!-- The fill in form -->
<html>
<head>
<title>Armory Avatar</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Character Name:<input type="text" size="12" maxlength="50" name="cname"><br />
Realm Name:
<select name="rname">
<optgroup label="English Realms">
<option value="aerie-peak">Aerie-Peak</option>
<option value="agamaggan">Agamaggan</option>
<option value="aggramar">Aggramar</option>
etc etc etc.
</optgroup>
</select><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
} else {  //If form is submitted execute this

    $charname = $_POST["cname"]; //input character name
    $realmname = $_POST["rname"]; //input realm name

    $charurl = urlencode(utf8_encode($charname)); //prepares character name for url usage
    $realmurl = 'http://eu.battle.net/api/wow/character/'.$realmname.'/';  //combines the basic url link with the realm name
    $toon = $realmurl.$charurl; //adds the charactername behind the realm url
    $data = @file_get_contents($toon); //retrieves the data from the armory api
        if ($data) {
            // if armory data is found execute this 

            $obj = json_decode($data); //converts the data from json to be used in the php code ?>
            <img src='http://eu.battle.net/static-render/eu/<?php echo $obj->thumbnail; ?>'> </img><br /> <?php //Is url link to image 
            $armoryname = utf8_decode($obj->name); //makes the name readable again
            echo "Name: " . $armoryname . "<br />";  //character name
            echo "Level: " . $obj->level . "<br />";  //character level
            echo "Achievement Points : " . $obj->achievementPoints . "<br />"; //Achievement Points
            if ( $obj->gender == 1 ){  //Deteremines the gender of the character
                echo "Gender : Female <br />" ;  //displays gender as female
            }else{
                echo "Gender : Male <br />" ; //dispalays gender as male
            } 
            $image = "http://eu.battle.net/static-render/eu/".$obj->thumbnail;
            ?>
            Image: <a href='http://eu.battle.net/static-render/eu/<?php echo $obj->thumbnail; ?>'> http://eu.battle.net/static-render/eu/<?php echo $obj->thumbnail; ?></a><br /> 


            <!--Button submit code-->

            <script type="text/javascript">
            $('button.cancel_link').click(function() {

            // Change the value of the input with an ID of 'avatarurl'
            // with the dynamic value given to you by the external JSON link

            window.opener.getElementById('avatarurl').value = '<?php echo $image; ?>';

            });
            </script>
            <input> <!-- The button here -->



        <?php   



        }
        else { // if armory data is not found execute this  ?>

            error code stuf
        }
}
?>

现在我需要这行代码:

$ image =“http://eu.battle.net/static-render/eu/”。$ obj-&gt; thumbnail;

在窗口关闭时或仅仅通过点击另一个提交按钮(优选在关闭按钮上发生)时返回。当其中任何一个发生时,它需要将此插入到此字符串中:

<input type="text" class="textbox" name="avatarurl" size="25" maxlength="100" value="{$avatarurl}" /></td>

texbox名为avatarurl。

希望你们中的任何人都知道如何修改或创建一个为您执行此操作的JavaScript。由于我的php已经严重受限,我的javascript知识几乎没有。

2 个答案:

答案 0 :(得分:2)

您需要修改关闭弹出窗口的方式。尝试这样的事情:

// When a BUTTON with the class name 'cancel_link'
// is clicked, it activates the following

$('button.cancel_link').click(function() {

   // Change the value of the input with an ID of 'avatarurl'
   // with the dynamic value given to you by the external JSON link

   window.opener.getElementById('avatarurl').value = '<?php echo $image; ?>';

});

您需要确保结束链接有cancel_link作为其类名,并且您父文档中的输入元素的ID为avatarurl

答案 1 :(得分:0)

所以在寻找尝试并感谢@Jamie后,我知道该去哪里看。

我找到了http://www.codingforums.com/showthread.php?t=213298 这最终是有效的。

在php页面打开它我添加了:

<script type="text/javascript">

function open_pop(){
window.open('armory.php','AvatarArmory','height=510,width=350,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
<html>
.....
<input type="button" value = "OpenArmory" onClick="open_pop()" />

并将id="blogbox"添加到文本框的输入中。

<input type="text" class="textbox" name="avatarurl" size="45" value="{$avatarurl}" id="blogbox"/>

在armory.php页面上,我添加了带有javascrip功能的按钮:

<script type="text/javascript">

            function pops(avatar){
            textcontent=opener.document.getElementById("blogbox").value;
            opener.document.getElementById("blogbox").value = textcontent + " " + avatar;
            }
            </script>
            <input type="button" value = "Insert Avatar.." onClick="pops('<?php echo $image; ?>');window.close()" />

这完美无缺。

谢谢你的帮助。