PHP实现设置文本框的只读属性

一 代码

conn.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>连接数据库</title>
</head>
<body>
<?php
$conn=mysql_connect("localhost","root","root");		//连接数据库服务器
mysql_select_db("db_database24",$conn);				//连接db_database08数据库
mysql_query("set names utf8");						//设置数据库编码格式
?>
</body>
</html>
 
index.php
<?php include("conn.php");?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>设置文本框的只读属性</title>
<style type="text/css">
<!--
.style1 {font-size: 12px}
.style2 {font-size: 13px}
-->
</style>
</head>
<body>
<table width="300" border="1" cellpadding="0" cellspacing="0" background="images/hhh.JPG">
<?php 
include("conn.php");
$query=mysql_query("select * from tb_stock");
$myrow=mysql_fetch_array($query);
?>
<form name="form1" method="post" action="">
  <tr>
    <td height="22" colspan="4" align="center" class="style2">设置文本框的只读属性</td>
    </tr>
  <tr>
    <td width="67" height="22" align="center"><span class="style1">商品编号:</span></td>
    <td colspan="3">
	<input name="commodity_id" type="text" id="commodity_id" value="<?php echo $myrow['id'];?>" disabled  size="22" maxlength="50">
	</td>
  </tr>
  <tr>
    <td height="22" align="center" class="style1">商品名称:</td>
    <td colspan="3"><input name="commodity_name" type="text" id="commodity_name" value="<?php echo $myrow['name'];?>" size="22" maxlength="50"></td>
  </tr>
  <tr>
    <td height="22" align="center" class="style1">库存数量:</td>
    <td colspan="3">
     <input name="stock_count" type="text" id="stock_count" value="<?php echo $myrow['count'];?>" readonly onClick="alert('此文本框为只读属性,库存数量不能更改!')" size="22" maxlength="50"></td>
  </tr>
  <tr>
    <td height="22" align="center" class="style1">单价:</td>
    <td width="77">	
	<input name="price" type="text"class="text" id="price" value="<?php echo $myrow['price'];?>" size="10" maxlength="50"></td>
    <td width="70" align="center" class="style1">库存金额:</td>
    <td width="76"><input name="stock_money" type="text" id="stock_money" value="<?php echo $myrow['money'];?>" size="10" maxlength="50"></td>
  </tr>
  </form>  
</table>
</body>
</html>
 
二 运行结果

 
三 说明
将文本框设置成只读属性有两种方法,一种是将文本框设置成readonly,一种是将文本框设置成disabled。

猜你喜欢

转载自cakin24.iteye.com/blog/2378633
今日推荐