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

Small glitch but could be an important issue

本文关键字:一个 问题 故障      更新时间:2023-09-26

我目前有一个问题,我的下拉菜单使用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>

对于"mainlines"answers"categories",我只在被选中时才得到显示输出的数据,但是当项目被编辑时,它不会自动打印出来,除非我使用"print $mainlines"或"print $categories"。

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

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

    if (onChange = "SelectAccountHead_Code(this.value)"){
    print $mainlines;
    } else {
    echo $_SESSION["mainlines"];
}

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

更新:

这是我的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.........

你的关联数组中有constants

看这个例子为什么是错误的:

$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块应该有{ }