doushu5451 2014-04-10 08:13
浏览 49

将php数组传递给父javascript main

I'm stack in something that I don't understand really. I got a Javascript page as parent of an Iframe, the iframe executes its rutine in php and then sends back some html info and some variable and an array, here is the prob, How do I send properlly the array respecting as much as possible the code I have?

JAVASCRIPT:

function showPlaceFrame(user_id,page_id){
  var number = user_id;
  var locationId = page_id;
  var iFrameOF = document.createElement('iframe');
  iFrameOF.id = 'locationFrame';
  iFrameOF.src = '/php_fyles/openPlace.php?user_id='+number+'&page_id='+locationId;       
  iFrameOF.style.float = 'top';
  iFrameOF.frameBorder='0';             
  var homeExtendedDiv_ = document.getElementById("homeExtendedDiv");
  homeExtendedDiv_.appendChild(iFrameOF);
}

PHP:

$result=mysql_query("SELECT * FROM places WHERE page_id=" . $page_id, $link);
$fqlPlace = 'SELECT page_id,name,description FROM place WHERE page_id=' . $page_id;
$ret_obj_place = $facebook->api(array(
    'method' => 'fql.query',
    'query' => $fqlPlace,
));

echo '<script type="text/javascript" charset="UTF-8">';
echo 'var result1 = ' . json_encode($result) . ';';
echo 'var result2 = ' . json_encode($ret_obj_place) . ';';
echo 'parent.parsePlaceFrame()'; 
echo '</script>';

BACK TO JAVASCRIPT:

function parsePlaceFrame(){
    var locationFrame_ = document.getElementById("locationFrame");                              
    FB.XFBML.parse(locationFrame_);
    autoResize("locationFrame");
    var iFrameOF = document.getElementById('locationFrame');
    var innerDoc = iFrameOF.contentWindow.document;
    var result1 = innerDoc.getElementById('result1'); //These are tries
    var result2 = innerDoc.getElementById('result2'); //These are tries
    alert(result2);                                   // 
    var ge1 = iFrameOF.contentWindow['result1'];      //
    var ge2 = iFrameOF.contentWindow['result2'];      //
    alert(ge1);                                       //These are tries
    alert(ge2);                                       //
}

What I'm getting right now is a "null" value and I'm sure it is not empty in php because I've checked it. Any idea?

  • 写回答

1条回答 默认 最新

  • dsghpgmay31938863 2014-04-10 08:21
    关注

    Rather than using var result = try setting a global variable there, using window['result']= or parent['result] to set a global javascript variable. When parsePlaceFrame is called, you can immediately access the variable, rather than trying to claim it from the iFrame.

    EDIT: UPDATE

    In your PHP, you have this:

    echo '<script type="text/javascript" charset="UTF-8">';
    echo 'var result1 = ' . json_encode($result) . ';';
    echo 'var result2 = ' . json_encode($ret_obj_place) . ';';
    echo 'parent.parsePlaceFrame()'; 
    echo '</script>';
    

    try changing it to this:

    echo '<script type="text/javascript" charset="UTF-8">';
    echo 'window[\'result1\'] = ' . json_encode($result) . ';';
    echo 'window[\'result2\'] = ' . json_encode($ret_obj_place) . ';';
    echo 'parent.parsePlaceFrame()'; 
    echo '</script>';
    

    Then, in your second javascript excerpt:

    var ge1 = iFrameOF.contentWindow['result1'];      //
    var ge2 = iFrameOF.contentWindow['result2'];      //
    

    Should be

    var ge1 = window['result1'];      //
    var ge2 = window['result2'];      //
    

    to access the global variables we set with the PHP.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图