gpt4 book ai didi

javascript - 小故障但可能是一个重要问题

转载 作者:行者123 更新时间:2023-12-03 12:18:29 26 4
gpt4 key购买 nike

目前,我在使用 JavaScript/AJAX 和 PHP 时遇到下拉菜单问题。

下面是 edit.php 文件的代码,该文件应该从下拉列表中的多个选择框中检索数据:

    <td ><input readonly type="text" id="mainlines" value='<?php echo $_SESSION["mainlines"];?>' name="mainlines"/> </td>
<td ><input readonly type="text" id="categories" value='<?php echo $_SESSION["categories"]; ?>' name="categories"/> </td>

<td>
<select name="subcategories" id="menu" onChange="SelectAccountHead_Code(this.value)">
<option value="<?php print $subcategories; ?>"><?php print $subcategories; ?></option>
<?php

$sql1a = "SELECT * FROM subcategory ORDER BY subcategories asc";
$smt1a = $dbs->prepare($sql1a);
$smt1a -> execute();
while($row1a=$smt1a->fetch(PDO::FETCH_ASSOC))
{
if($row1a['subcategories']==$_GET['id2'] )
echo ("<option selected value=$row1a[subcategories]>$row1a[subcategories]</option>");
else
echo ("<option value=$row1a[subcategories]>$row1a[subcategories]</option>");
}
?>
</select>
</td>

对于“主线”和“类别”,我仅在选择时才获取显示输出的数据,但是当编辑该项目时,它不会自动打印出来,除非我使用“print $mainlines”或“打印$类别”。

如何在 if/else 语句中插入 onChange 事件更改器?

我希望它用伪代码声明以下内容:

    if (onChange = "SelectAccountHead_Code(this.value)"){
print $mainlines;

} else {
echo $_SESSION["mainlines"];

}

如何在 JavaScript/AJAX 事件更改器/处理程序的 if/else 子句中插入正确的语法?

更新:

这是我的 JavaScript 代码 - 我在哪里缺少 AJAX 代码?

function SelectAccountHead_Code(i)
{
var k=document.getElementById("menu").selectedIndex;
var l=document.getElementById("menu1").selectedIndex;

if((k>0)&&(l>0))
{
self.location="edit3.php?productsnames=<?php print $productsnames;?>&id="+document.getElementById("menu").options[k].value+"&id2="+document.getElementById("menu").options[k].innerHTML +"&id3="+document.getElementById("menu1").options[l].value+"&id3="+document.getElementById("menu1").options[l].innerHTML;
}
}


function SelectAccountHead_Codes(j)
{
var k=document.getElementById("menu").selectedIndex;
var l=document.getElementById("menu1").selectedIndex;
if((k>0)&&(l>0))
{
self.location="edit3.php?productsnames=<?php print $productsnames;?>&id="+document.getElementById("menu").options[k].value+"&id2="+document.getElementById("menu").options[k].innerHTML +"&id3="+document.getElementById("menu1").options[l].value+"&id3="+document.getElementById("menu1").options[l].innerHTML;

}
}

function SelectUp(i)
{
var k=document.getElementById("menuUp").selectedIndex;
var l=document.getElementById("menuUp").selectedIndex;
self.location="edit3.php?id="+document.getElementById("menuUp").options[k].value+"&id2="+document.getElementById("menu").options[k].innerHTML +"&id3="+document.getElementById("menu1").options[l].value+"&id3="+document.getElementById("menu1").options[l].innerHTML;
}

function SelectUp(j)
{
var k=document.getElementById("menuUp").selectedIndex;
var l=document.getElementById("menuUp").selectedIndex;
self.location="edit3.php?id="+document.getElementById("menuUp").options[k].value+"&id2="+document.getElementById("menu").options[k].innerHTML +"&id3="+document.getElementById("menu1").options[l].value+"&id3="+document.getElementById("menu1").options[l].innerHTML;
}

最佳答案

如果没有进一步的请求(Ajax)和服务器交互,您就无法组合 Javascript 和 PHP。

其他大错误:

echo ("<option selected value=$row1a[subcategories]>$row1a[subca.........

关联数组中有常量

See this example为什么这是错误的:

$array = array('test' => 111);
define('test', 999);

echo $array[test];

正确:

echo ("<option selected value='" . $row1a['subcategories'] . "'>" . $row1a['su..
// consider adding htmlspecialchars() or addslashes() to your output

更多小错误/不好的做法:

  • onChange 应全部为小写字母:onchange
  • if .. else block 应该有 { }

关于javascript - 小故障但可能是一个重要问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24558320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com