【发布时间】:2019-12-06 01:20:52
【问题描述】:
我有一个问题,是否可以使用存储在document.getElementById().value、getElementsByName().value、getElementsByClassName().value 中的值作为通过 PHP 进行 SQL 查询的参数?
示例。
我有这条线<input type="text" class="myinput" id="myinput" name="myinput" value="999-AAA-000">
然后我将数据存储在这个元素中。
<script>
function myFunction() {
var foroutput = document.getElementsByClassName("myinput");
}
</script>
有没有办法将document.getElementsByClassName("myinput") 或var foroutput 用作通过PHP 进行SQL 查询的参数?
场景:SQL 查询与document.getElementsByClassName("myinput") 在同一页面内,不使用<form> 是否可以工作?
这是我的代码
<input type="text" class="decid" id="decid" name="decid">
// the data passed into this input box will be used as a parameter for SQL Query $id
<table id="example2" class="table table-bordered">
<thead>
<th>Schedule Date</th>
<th>Schedule Name</th>
<th>Recorded In</th>
<th>Recorded Out</th>
<th>Day Count</th>
<th>Day Value</th>
<th>N.D. Value</th>
<th>Leave Count</th>
<th>R.H. Count</th>
<th>R.H. Value</th>
</thead>
<tbody>
<?php
$id=$_POST['id'];
$sql = "SELECT fingerscanno, scheduledate, schedulename, recordin, recordout, noofdays, rate, nightdifferential, leaveday, regularholiday, specialholiday, referenceno
FROM payrollrecords WHERE fingerscanno='$user' and referenceno='$id'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
echo "
<tr>
<td>".$row['scheduledate']."</td>
<td>".$row['schedulename']."</td>
<td>".$row['recordin']."</td>
<td>".$row['recordout']."</td>
<td>".$row['noofdays']."</td>
<td>".$row['rate']."</td>
<td>".$row['nightdifferential']."</td>
<td>".$row['leaveday']."</td>
<td>".$row['regularholiday']."</td>
<td>".$row['specialholiday']."</td>
</tr>
";
}
?>
</tbody>
</table>
一个样本会非常棒。谢谢。
编辑:
为什么这不起作用?
<input type="text" class="decid" id="decid" name="decid">
<script type="text/javascript">
var abc = document.getElementById("decid").value;
<?php $abc = "<script>document.write(abc)</script>"?>
</script>
<?php echo $abc;?>
【问题讨论】:
-
是的,异步发送,例如通过 AJAX。
-
@Qirel 如果你不介意,你能给我举个例子吗?或者一个链接也许可以学习?我在 AJAX 方面很糟糕。
-
@LouysPatriceBessette 我的情况是数据被传递到模式中。并且 PHP 代码也在模态框内。
-
你应该做准备语句来逃避sql注入,看看php中的PDO库
标签: php jquery html xampp sqlsrv