Javascript 函数在 PHP 内部调用时没有响应。 (试图将项目添加到选项列表框)
Javascript function not responding when called inside PHP. (Trying to add items to option listbox)
我正在尝试在 php 代码中调用 javascript 函数。但是,它不仅有效。我已经测试了无数的东西来找出问题的确切位置,而且我没有发现我尝试过的东西有任何问题它似乎并没有响应。
完整代码如下:(我将在下面解释)
<?php
//Connect to Database
include 'init.php';
if (!isset($_SESSION['user_id']))
{
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_POST['insert']))
{
$barcode = mysqli_real_escape_string($db, $_POST['barcode']);
$query = "SELECT * FROM item_info WHERE serial_no = '$barcode'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1)
{
$row = mysqli_fetch_array($results);
$itemname = $row[1];
?>
<script type="text/javascript">
var one = "<?php echo $itemname; ?>";
insertValue(one);
</script>
<?php
}
else
{
array_push($errors, "Unable to find an item with that Serial Number");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="CSS/mainoperations.css">
</head>
<body>
<div class="header">
</div>
<div id="container">
<div id="leftcolumn">
<ul>
<h1>PartsCribber</h1>
<li><a class="active" href="mainoperations.php">Main Operations</a></li>
<li><a href="vieweditprofile.php">Profile Settings</a></li>
<li><a href="#contact">Change Password</a></li>
<li><a href="#about">Student Cart</a></li>
<li><a href="#about">Student Possession</a></li>
<li><a href="#about">Update Inventory</a></li>
<li><a href="#about">Log Out</a></li>
</ul>
</div>
<div id="rightcolumn">
<form method="post" action="mainoperations.php">
<div class="title">
<h3>
<?php echo "Main Operations (1/3)"; ?>
</h3>
</div>
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success">
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<?php include('errors.php'); ?>
<div class="input-group">
<label>Scan Barcode:</label>
<input type="text" id="barcode" name="barcode">
</div>
<div class="input-group">
<button class="btn" name="insert" >Enter</button>
</div>
<div class="input-group">
<button class="btn" onclick="deleteValue(); return false;">Delete Item</button>
</div>
<select id="myselect" size="15" class="select">
</select>
</form>
</div>
<div style="clear: both;"></div>
</div>
<script>
function insertValue(itemname)
{
var x = document.getElementById("myselect");
var option = document.createElement("option");
option.text = itemname;
if(option.text.length == 0)
{
//Do Nothing.
}
else
{
option.className = ('option');
x.size = "8";
x.add(option);
document.getElementById("barcode").value = "";
}
}
function deleteValue()
{
var x = document.getElementById("myselect");
x.size = "8";
x.remove(x.selectedIndex);
}
</script>
</body>
</html>
我想做什么?
我的代码应该将输入到 html 文本字段中的字符串(实际上应该是 barcode/serial 数字)。并在数据库中搜索具有该序列号的项目,并将 return 项目名称搜索到 HTML 选项列表框中。我 100% 确定 returned 的数据是正确的。我只是在调用 javascript 函数将数据添加到列表框时遇到问题。
我的代码结构:
单击输入按钮后,将设置 post 数据:
<div class="input-group">
<label>Scan Barcode:</label>
<input type="text" id="barcode" name="barcode">
</div>
<div class="input-group">
<button class="btn" name="insert" >Enter</button>
</div>
我使用 php 使用输入的数据获取项目名称。像这样:
if (isset($_POST['insert']))
{
$barcode = mysqli_real_escape_string($db, $_POST['barcode']);
$query = "SELECT * FROM item_info WHERE serial_no = '$barcode'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1)
{
$row = mysqli_fetch_array($results);
$itemname = $row[1];
?>
<script type="text/javascript">
var value = "<?php echo $itemname; ?>";
insertValue(value);
</script>
<?php
}
else
{
array_push($errors, "Unable to find an item with that Serial Number");
}
}
如你所见,我在上面调用了 javascript 函数,如下所示:
<script type="text/javascript">
var value = "<?php echo $itemname; ?>";
insertValue(value);
</script>
我的实际 javascript 函数结构如下:
<script>
function insertValue(itemname)
{
var x = document.getElementById("myselect");
var option = document.createElement("option");
option.text = itemname;
if(option.text.length == 0)
{
//Do Nothing.
}
else
{
option.className = ('option');
x.size = "8";
x.add(option);
document.getElementById("barcode").value = "";
}
}
function deleteValue()
{
var x = document.getElementById("myselect");
x.size = "8";
x.remove(x.selectedIndex);
}
</script>
所以我的问题是:
数据已在文本字段中输入,正确的项目名称是从数据库中return输入的,但由于某些未知原因无法在选项列表框中显示。
我什至不知道在哪里包含 "return false;" 似乎在任何地方都不起作用。
请帮忙。谢谢。
修复未定义函数调用的两种方法。
如果您在页面上加载了 jquery,您可以将您的特殊插入内容包裹在 .ready
中,如下所示:
?>
<script type="text/javascript">
$(function() {
var one = "<?php echo $itemname; ?>";
insertValue(one);
});
</script>
<?php
但是如果你没有jquery,一个简单的解决办法是你可以调整这部分:
?>
<script type="text/javascript">
var one = "<?php echo $itemname; ?>";
insertValue(one);
</script>
<?php
改为将其分配给变量:
$scriptstuff = '
var one = "'. addslashes($itemname) .'";
insertValue(one);
';
然后在实际 <script>
块的页面中进一步向下,在底部回显该变量(在函数定义之后):
<script>
// all your other javascript and function defines
<?php echo $scriptstuff;?>
</script>
我正在尝试在 php 代码中调用 javascript 函数。但是,它不仅有效。我已经测试了无数的东西来找出问题的确切位置,而且我没有发现我尝试过的东西有任何问题它似乎并没有响应。
完整代码如下:(我将在下面解释)
<?php
//Connect to Database
include 'init.php';
if (!isset($_SESSION['user_id']))
{
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_POST['insert']))
{
$barcode = mysqli_real_escape_string($db, $_POST['barcode']);
$query = "SELECT * FROM item_info WHERE serial_no = '$barcode'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1)
{
$row = mysqli_fetch_array($results);
$itemname = $row[1];
?>
<script type="text/javascript">
var one = "<?php echo $itemname; ?>";
insertValue(one);
</script>
<?php
}
else
{
array_push($errors, "Unable to find an item with that Serial Number");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="CSS/mainoperations.css">
</head>
<body>
<div class="header">
</div>
<div id="container">
<div id="leftcolumn">
<ul>
<h1>PartsCribber</h1>
<li><a class="active" href="mainoperations.php">Main Operations</a></li>
<li><a href="vieweditprofile.php">Profile Settings</a></li>
<li><a href="#contact">Change Password</a></li>
<li><a href="#about">Student Cart</a></li>
<li><a href="#about">Student Possession</a></li>
<li><a href="#about">Update Inventory</a></li>
<li><a href="#about">Log Out</a></li>
</ul>
</div>
<div id="rightcolumn">
<form method="post" action="mainoperations.php">
<div class="title">
<h3>
<?php echo "Main Operations (1/3)"; ?>
</h3>
</div>
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success">
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<?php include('errors.php'); ?>
<div class="input-group">
<label>Scan Barcode:</label>
<input type="text" id="barcode" name="barcode">
</div>
<div class="input-group">
<button class="btn" name="insert" >Enter</button>
</div>
<div class="input-group">
<button class="btn" onclick="deleteValue(); return false;">Delete Item</button>
</div>
<select id="myselect" size="15" class="select">
</select>
</form>
</div>
<div style="clear: both;"></div>
</div>
<script>
function insertValue(itemname)
{
var x = document.getElementById("myselect");
var option = document.createElement("option");
option.text = itemname;
if(option.text.length == 0)
{
//Do Nothing.
}
else
{
option.className = ('option');
x.size = "8";
x.add(option);
document.getElementById("barcode").value = "";
}
}
function deleteValue()
{
var x = document.getElementById("myselect");
x.size = "8";
x.remove(x.selectedIndex);
}
</script>
</body>
</html>
我想做什么? 我的代码应该将输入到 html 文本字段中的字符串(实际上应该是 barcode/serial 数字)。并在数据库中搜索具有该序列号的项目,并将 return 项目名称搜索到 HTML 选项列表框中。我 100% 确定 returned 的数据是正确的。我只是在调用 javascript 函数将数据添加到列表框时遇到问题。
我的代码结构: 单击输入按钮后,将设置 post 数据:
<div class="input-group">
<label>Scan Barcode:</label>
<input type="text" id="barcode" name="barcode">
</div>
<div class="input-group">
<button class="btn" name="insert" >Enter</button>
</div>
我使用 php 使用输入的数据获取项目名称。像这样:
if (isset($_POST['insert']))
{
$barcode = mysqli_real_escape_string($db, $_POST['barcode']);
$query = "SELECT * FROM item_info WHERE serial_no = '$barcode'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1)
{
$row = mysqli_fetch_array($results);
$itemname = $row[1];
?>
<script type="text/javascript">
var value = "<?php echo $itemname; ?>";
insertValue(value);
</script>
<?php
}
else
{
array_push($errors, "Unable to find an item with that Serial Number");
}
}
如你所见,我在上面调用了 javascript 函数,如下所示:
<script type="text/javascript">
var value = "<?php echo $itemname; ?>";
insertValue(value);
</script>
我的实际 javascript 函数结构如下:
<script>
function insertValue(itemname)
{
var x = document.getElementById("myselect");
var option = document.createElement("option");
option.text = itemname;
if(option.text.length == 0)
{
//Do Nothing.
}
else
{
option.className = ('option');
x.size = "8";
x.add(option);
document.getElementById("barcode").value = "";
}
}
function deleteValue()
{
var x = document.getElementById("myselect");
x.size = "8";
x.remove(x.selectedIndex);
}
</script>
所以我的问题是:
数据已在文本字段中输入,正确的项目名称是从数据库中return输入的,但由于某些未知原因无法在选项列表框中显示。
我什至不知道在哪里包含 "return false;" 似乎在任何地方都不起作用。
请帮忙。谢谢。
修复未定义函数调用的两种方法。
如果您在页面上加载了 jquery,您可以将您的特殊插入内容包裹在 .ready
中,如下所示:
?>
<script type="text/javascript">
$(function() {
var one = "<?php echo $itemname; ?>";
insertValue(one);
});
</script>
<?php
但是如果你没有jquery,一个简单的解决办法是你可以调整这部分:
?>
<script type="text/javascript">
var one = "<?php echo $itemname; ?>";
insertValue(one);
</script>
<?php
改为将其分配给变量:
$scriptstuff = '
var one = "'. addslashes($itemname) .'";
insertValue(one);
';
然后在实际 <script>
块的页面中进一步向下,在底部回显该变量(在函数定义之后):
<script>
// all your other javascript and function defines
<?php echo $scriptstuff;?>
</script>