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

标签 javascript php events if-statement handler

目前,我在使用 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/

相关文章:

javascript - 1 div中所有图像的加载函数

javascript - 在 Outlook 中发送电子邮件 + 附加代码文件(javascript/AJAX 和 html 内容)

PHP gethostbyname 返回主机名而不是 IP 地址

jQuery - onClick 事件上的颤抖图像

asp.net - 从 Asp.Net 的代码隐藏控制 Lightbox(模态)

php - 确定更新时间

php - 需要一个相当复杂的正则表达式来处理一个简单的文本文档

php - Javascript 魔术方法

javascript - jQuery:取消绑定(bind)事件处理程序以稍后再次绑定(bind)它们

javascript - JQuery/Javascript Hook