简体   繁体   中英

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.

This works 100% on internet explorer but not in google chrome or on my ipad / galaxt tablet.

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

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

<? 

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

<? 

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

As mentioned this works 100% on Internet explorer but not in google chrome or my ipad.

When you use

document.getElementById("subcat");

then you should also have an element with such an id. Your

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

won't do with browsers like chrome, firefox, konqueror and probably lots of others. 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.

In below function also,you called for subcat.

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

You should first have an id="subcat".

Still you getting some problem , post the error.

Thanks.

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