我是新的PHP(我在android上工作)。我的页面包含一个表,它根据数据库的变化而变化。我的表包含下拉选项,如果我改变下拉选项中的任何东西,我们必须将这些输入数据库。我正在调用脚本函数"updatePrepStatus()"。它不工作了,请帮帮我。
<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'>";
}
?>
看起来你好像把PHP(运行在作为web服务器的计算机上)和javascript(运行在网站访问者运行的浏览器上)搞混了。它们不能直接互动。
但是,它们可以使用通常称为AJAX的机制进行交互。这需要你编写一个接收请求和发送响应的服务器端脚本,以及一个发送请求和处理来自服务器的响应的客户端javascript。使用PHP和jQuery的快速示例:
如果你写了一个服务器脚本并安装在http://www.exampler.com/ajaxserver.php:
<?php
echo (json_encode (array (
'response' => $_GET ['message'],
'luckynumber' => mt_rand ())));
?>
客户端:
$.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)和客户端代码(如Javascript)之间的区别。
不可能从Javascript中调用PHP函数。如果您愿意,您可以使用AJAX调用来调用服务器以响应Javascript事件,并且可以将该AJAX调用提供给您必须专门创建的新PHP页面。然而,不清楚您是否需要这样的东西,因为确定您试图做什么有点困难-如果它不需要对服务器进行新的调用,那么您不需要这个。
我建议你需要对web开发有更多的了解,以便理解客户端和服务器端代码之间的区别,这样你就能正确地理解为什么这在这一点上不起作用。我们可以推荐任何复杂的解决方案,我们给你很多代码,但你需要理解为什么有些东西可以或不可以根据web应用程序的工作方式工作。
<?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;
?>