如何在html的onchange方法中调用php函数?
我是PHP新手(我在android上工作)。我的页面包含一个表,它根据数据库的变化而变化。我的表包含下拉选项,如果我在下拉选项中更改任何内容,我们必须将其输入数据库。我正在调用脚本函数“UpdatePresstatus()”。它不起作用,请帮助我如何在html的onchange方法中调用php函数?,php,Php,我是PHP新手(我在android上工作)。我的页面包含一个表,它根据数据库的变化而变化。我的表包含下拉选项,如果我在下拉选项中更改任何内容,我们必须将其输入数据库。我正在调用脚本函数“UpdatePresstatus()”。它不起作用,请帮助我 <script > function updatePrepStatus(itemName) { alert(itemName); <?php e
<script >
function updatePrepStatus(itemName) {
alert(itemName);
<?php
echo " updatePrepStatus-- ";
?> ;
}
</script>
<?php
//$Order_no=$_GET['OrderId'];
$result = mysql_query("select Menu_Item_Name, Menu_Item_quantity,Menu_Item_Price,PreparationStatus from order_details where order_id='$Order_no' && status!='C' ");
$rows_count = mysql_num_rows($result);
$i=0;
$ItemName="";
while( $row=mysql_fetch_array($result) )
{
if($i==0)
{
echo "<table id='table1' style='WIDTH: 400px;' border='3' cellSpacing=0 cellPadding=3 width=693 align=center>
<tbody>";
echo "<tr><td><font color='#0000e' size='2'><strong>Item
Name</strong></font></td>
<td><font color='#0000e'
size=2><strong>Quantity</strong></font></td>
<td><font color='#0000e'
size=2><strong>Price</strong></font></td>
<td><font color='#0000e'
size=2><strong>Status</strong></font></td></tr>";
}
if($row['PreparationStatus']=='NEW')
$ps2='READY';
else
$ps2='NEW';
$ItemName=$row['Menu_Item_Name'];
echo $ItemName;
echo"<tr>
<td><font color='#0000e' size='2'>".$row['Menu_Item_Name']."</font></td>
<td><font color='#0000e' size='2'>".$row['Menu_Item_quantity']."</font></td>
<td><font color='#0000e' size='2'>".$row['Menu_Item_Price']."</font></td>
<td><SELECT NAME='Select[]' id='PrepStatus' onChange=updatePrepStatus($ItemName)><OPTION VALUE='NEW'><font color='#0000e' size='2'>".$row['PreparationStatus']."
</OPTION><OPTION VALUE='READY'><font color='#0000e' size='2'>$ps2</OPTION></SELECT> </font></td></tr>";
$i++;
}
if($i==0)
{
echo "<p align='center'>NO RECORDS<strong><font color='white' size='3'>";
}
else
{
echo "</tbody></table></p>
<p> </p>
<p>
<p></p>
<input value='Submit' type='submit' name='submit'>";
}
?>
函数updatePresstatus(itemName){
警报(项目名称);
;
}
看起来你把PHP(在作为web服务器的计算机上运行)和javascript(在网站访问者运行的浏览器中运行)混淆了。他们不能直接互动
但是,它们可以使用通常称为AJAX的机制进行交互。这需要您编写一个服务器端脚本来接收请求并发送响应,以及一个客户端javascript来发送请求并处理来自服务器的响应
使用PHP和jQuery的快速示例:
如果编写服务器脚本并将其安装在:
您误解了服务器端代码(如PHP)和客户端代码(即Javascript)之间的区别
从Javascript调用PHP函数是不可能的。如果需要,您可以使用AJAX调用来调用服务器以响应Javascript事件,并且可以将AJAX调用输入到您必须专门创建的新PHP页面中。但是,还不清楚您是否需要这样的东西,因为确定您试图做什么有点困难-如果不需要对服务器进行新的调用,那么您就不需要这样做
我建议您需要了解更多关于web开发的知识,以便理解客户端代码和服务器端代码之间的区别,以便您正确地理解为什么在这一点上这不起作用。我们可以推荐任何数量的复杂解决方案,在这些解决方案中,我们为您提供了大量的代码,但您需要根据web应用程序的工作方式来理解为什么有些东西能工作或不能工作
只是让你开始。你了解服务器端PHP和客户端Javascript之间的区别吗?@IgnacioVazquez Abrams已经没有小猫了?@suresh还有,请清理你的html,过多的内容和字体属性看起来很混乱。改用CSS吧!以防询问者不知道;1-在加载页面之前在服务器上执行PHP。2-Javascript在页面加载后运行。@Damien:小猫仍然有他们以前的死亡触发器。我不太确定这类问题是否算得上……它似乎不起作用——你不能在“onselect”上调用php函数,正如我所知,onselect用于javascript或jquery。
<?php
include 'db_config.php';
$query = "SELECT * FROM Category ORDER BY c_id ASC";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());?>
<select id='cat' name='cat' class='input2' onselect="function myfunction()"> <option value='Select '>Select Category</option>;
<?php while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['c_id']}'>{$row['c_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
$.ajax ({
url: 'http://www.example.com/ajaxserver.php',
datatype: 'json',
method: 'GET',
data: {
message: 'Hello, this is the message that will be returned by AJAX!'
},
success: functioon (data, status, xhr)
{
alert (data.response);
alert ('Your lucky number is: ' + data.luckynumber);
}
});
<?php
include 'db_config.php';
$query = "SELECT * FROM Category ORDER BY c_id ASC";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());?>
<select id='cat' name='cat' class='input2' onselect="function myfunction()"> <option value='Select '>Select Category</option>;
<?php while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['c_id']}'>{$row['c_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>