我试图在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文本框中的字符串(实际上应该是条形码/序列号)。并在数据库中搜索带有该序列号的项目,并将项目名称返回到HTML选项列表框中。我百分之百肯定正确的数据会被返回。我只是无法调用javascript函数将数据添加到列表框中。
My代码结构:一旦单击enter按钮并设置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>
所以我的问题是:
请帮帮忙。谢谢。
发布于 2018-03-03 16:37:26
修复对未定义函数的调用的两种方法。
如果您在页面上加载了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>
https://stackoverflow.com/questions/49086095
复制相似问题