Javascript 弹出选择器 + PHP
Javascript popup selector + PHP
在SO社区的帮助下,我编写了一个javascript和php页面,允许我将值从弹出页面传递回父页面。
这在IE浏览器上100%有效,但在谷歌浏览器或我的iPad/galaxt平板电脑上无效。
关于如何纠正这一点的任何想法?一如既往地感谢任何帮助。
下面是我的部分代码来自父页面(newsale.php)和弹出页面(sku.php)。我知道建议使用其他方法而不是使用弹出窗口,但是出于应用程序原因,我需要让此解决方案与弹出页面一起使用。
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 弹出页面(整页)
<?
$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: <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(用于动态下拉列表)
<?
$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)";
?>
我不认为 dd.php 对父弹出关系的功能有任何影响,但已包含它,以便您可以遵循代码。
如前所述,这在Internet Explorer上100%有效,但在谷歌浏览器或我的ipad中则不然。
当您使用
document.getElementById("subcat");
那么你也应该有一个带有这样一个 id 的元素。你
<select name=subcat onchange="updateinput();" >
不会使用Chrome,Firefox,Konqueror等浏览器以及许多其他浏览器。用
<select id="subcat" onchange="updateinput();" >
相反。
不幸的是,我无法让您的代码工作,所以我无法对此进行测试,但是更改
window.opener.updateValue(parentId, value);
自
window.opener.contentWindow.updateValue(parentId, value);
或
window.opener.window.updateValue(parentId, value);
可能会解决这个问题。
如果没有,也许您可以发布从Chrome控制台显示的错误,并更好地解释究竟哪些不起作用。
在下面的函数中,你也调用了子猫。
function sendValue(value)
{
var e = document.getElementById("subcat");
}
along with one mentioned by Themroc.
你应该首先有一个id="subcat"。
你仍然遇到一些问题,发布错误。
谢谢。
相关文章:
- 使用PHP通过HTML表单选项选择器过滤MYSQL结果
- 获取PHP代码中日期选择器的值
- 在jquery选择器中获取id或类名,使用PHP的regex
- 从 mysql 获取行集,并使用 php 和 Codeigniter 中的 jquery 生成动态范围选择器框
- Javascript 弹出选择器 + PHP
- 使用 Ajax/jQuery 将 PHP 内容输入到 CSS 选择器中
- 通过PHP或Javascript从单选按钮和显示日期选择器中选择选项
- 光谱颜色选择器将id和颜色发布到php
- 关于php/javascript中的日期选择器
- 当我的html主体在php中时,Id选择器在jquery中不起作用
- 使用下拉选择器更改/覆盖PHP变量
- 如何在php/codeigniter中为日期选择器做$_POST
- 使用script.alicio.us作为颜色选择器->php GD
- 通过php循环自动生成的JQuery ID选择器
- 时间和日期选择器(Javascript/PHP)
- 使用JavaScript和PHP设置日期选择器
- 如何在jquery中使用日期选择器并在php中捕获它
- 在php foreach中使用jquery动态选择器删除单个图像
- PHP动态变量作为jQuery选择器
- PHP + jQuery:将日期选择器应用于具有特定模式的输入