简体   繁体   English

Javascript弹出选择器+ PHP

[英]Javascript popup selector + PHP

I have, with the help of the SO community written a javascript and php page that allows me to pass a value from the popup page back to the parent page. 在SO社区的帮助下,我编写了一个javascript和php页面,允许我将弹出页面中的值传递回父页面。

This works 100% on internet explorer but not in google chrome or on my ipad / galaxt tablet. 这在互联网资源管理器上100%有效,但在谷歌浏览器或我的ipad / galaxt平板电脑上没有。

Any idea on how this can be corrected? 关于如何纠正这个问题的任何想法? Any help appreciated as always. 任何帮助一如既往的赞赏。

Below is portions of my code from the parent page(newsale.php) and the popup page(sku.php). 下面是我的代码部分来自父页面(newsale.php)和弹出页面(sku.php)。 I know that other methods are recommended over using popup but I need to get this solution working with the popup page for application reasons. 我知道建议使用其他方法而不是使用弹出窗口,但出于应用原因,我需要使用弹出页面来解决此问题。

newsale.php Parent Page (Code snippets, not entire page) newsale.php父页面(代码片段,而不是整页)

<script type="text/javascript">  
function selectValue(id) 
{ 
    // open popup window and pass field id 
    window.open('sku.php?id=' + encodeURIComponent(id),'popuppage', 
      'width=1000,toolbar=1,resizable=1,scrollbars=yes,height=200,top=100,left=100'); 
} 

function updateValue(id, value) 
{ 
    // this gets called from the popup window and updates the field with a new value 
    document.getElementById(id).value = value; 
} 

</script> 

<table>
<tr id="r1">  
<input size=10  type=number id=sku1 name=sku1 onchange="showUser(1, this.value)" <? if($rows>0){echo "value=".mysql_result($resultorder,0,1);}  ?>><img src=q.png  name="choice" onClick="selectValue('sku1')" value="?">
</td>
</tr>
<tr id="r2"> 
<td>
<input size=10  type=number id=sku2 name=sku2 onchange="showUser(2, this.value)" <? if($rows>1){echo "value=".mysql_result($resultorder,1,1);}  ?> ><img src=q.png  name="choice" onClick="selectValue('sku2')" value="?">
</td>
</tr>
</table>

sku.php Popup Page (entire page) sku.php弹出页面(整页)

<? 

  $con = mysql_connect('localhost', 'username', 'password'); 
 if (!$con) 
   { 
   die('Could not connect to server: ' . mysql_error()); 
   } 
   $db=mysql_select_db("DBName", $con); 

    if (!$db) 
   { 
   die('Could not connect to DB: ' . mysql_error()); 
   } 


$sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode";
$result=mysql_query($sql);

 ?> 

<script type="text/javascript"> 

  function AjaxFunction(cat_id) { 
    var httpxml; 
    try { 
      // Firefox, Opera 8.0+, Safari 
      httpxml = new XMLHttpRequest(); 
    } catch (e) { 
      // Internet Explorer 
      try { 
        httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
        try { 
          httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
        } catch (e) { 
          alert("Your browser does not support AJAX!"); 
          return false; 
        } 
      } 
    } 
    function stateck() { 
      if (httpxml.readyState == 4) { 
        var myarray = eval(httpxml.responseText); 
        // Before adding new we must remove previously loaded elements 
        for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
          document.testform.subcat.remove(j); 
        } 
        for (i = 0; i < myarray.length; i++) { 
          var optn = document.createElement("OPTION"); 
          optn.text = myarray[i]; 
          optn.value = myarray[i]; 
          document.testform.subcat.options.add(optn); 
        }  
      } 
    } 
    var url="dd.php"; 
    url = url+"?cat_id="+cat_id; 
    url = url+"&sid="+Math.random(); 
    httpxml.onreadystatechange = stateck; 
    httpxml.open("GET",url,true); 
    httpxml.send(null); 
  } 

</script> 


 <script type="text/javascript"> 
 function sendValue(value)
 {
 var e = document.getElementById("subcat"); 
 value = e.options[e.selectedIndex].value; 
 var parentId = <?php echo json_encode($_GET['id']); ?>; 
 window.opener.updateValue(parentId, value); 
 window.close(); 
 } 
 </script>


 <script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("subcat"); 
var catSelected = e.options[e.selectedIndex].value; 


document.getElementById("copycat").value=catSelected; 
} 
</script> 


<form name="testform">
Category: &nbsp; <select name=cat id=cat onchange="AjaxFunction(this.value);" style="width=300"> <br>
<option value='' style="width=300">Select One</option> 
<br>
<? 

  require "config.php";// connection to database  
  $q=mysql_query("select * from categories"); 
  while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
  } 

?> 
</select> 
 <br><br>
 Pack Code:
<select name=subcat onchange="updateinput();" > 
 <br><br>
</select>
<br><br>
<input type=hidden name=copycat id=copycat > 
<td><input type=button value="Select" onClick="sendValue(document.getElementById(copycat))" /></td>
</form> 

dd.php (for dynamic drop down list) dd.php (动态下拉列表)

<? 

  $cat_id=$_GET['cat_id']; 
  require "config.php"; 
  $q=mysql_query("select concat(packcode,', ',description) as details from skudata where cat_id='$cat_id'"); 
  echo mysql_error(); 
  $myarray=array(); 
  $str=""; 
  while($nt=mysql_fetch_array($q)){ 
    $str=$str . "\"$nt[details]\"".","; 
  } 
  $str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string 
  echo "new Array($str)"; 

?> 

I dont think dd.php has any influence on the functionality of the parent popup relationship but have included it so you can follow the code. 我不认为dd.php对父弹出关系的功能有任何影响,但已包含它,所以你可以按照代码。

As mentioned this works 100% on Internet explorer but not in google chrome or my ipad. 如上所述,这在互联网浏览器上100%有效,但在谷歌浏览器或我的ipad中没有。

When you use 当你使用

document.getElementById("subcat");

then you should also have an element with such an id. 那么你也应该有一个具有这种id的元素。 Your 您的

<select name=subcat onchange="updateinput();" >

won't do with browsers like chrome, firefox, konqueror and probably lots of others. 不会像chrome,firefox,konqueror和其他很多浏览器这样的浏览器。 Use 使用

<select id="subcat" onchange="updateinput();" >

instead. 代替。

Unfortunately, I can't get your code to work so I can't test this, but changing 不幸的是,我无法让你的代码工作,所以我无法测试,但改变

 window.opener.updateValue(parentId, value); 

to

 window.opener.contentWindow.updateValue(parentId, value);

or 要么

 window.opener.window.updateValue(parentId, value);

may solve this. 可以解决这个问题

If it doesn't, maybe you can post the errors that are showing from the Chrome console and explain better what exactly doesn't work. 如果没有,也许您可​​以发布从Chrome控制台显示的错误,并更好解释究竟什么不起作用。

In below function also,you called for subcat. 在下面的函数中,你也调用了subcat。

 function sendValue(value)
  {
    var e = document.getElementById("subcat"); 
  }
along with one mentioned by Themroc.

You should first have an id="subcat". 你应该首先有一个id =“subcat”。

Still you getting some problem , post the error. 你仍然遇到一些问题,发布错误。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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