搜索
首页后端开发php教程求助如何点击按钮更新数据?

我希望点击“updqte”按钮后按钮变成“save”,然后单元格变成文本框,编辑完文本框后点击“save”结果提交给了数据库。按钮改变用js做完了,其他的实在是不回了,各位大神帮帮忙吧。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" 
content="text/html; 
charset=UTF-8"><title>Insert title here</title><script language="javascript" type="text/javascript">         
function showtd(num) {                         
document.getElementById("bg"+num).style.display = "none";             
document.getElementById("show"+num).style.display = "block";         
}         
function hidetd(num) {             
document.getElementById("bg"+num).style.display = &#39;block&#39;;             
document.getElementById("show"+num).style.display = &#39;none&#39;;         
}         
function change()          
{              
var tds = document.getElementsByTagName("td");             
var t1 = document.getElementById("a").innerHTML;             
var txt = document.createElement("input");              
txt.type = "text";              
txt.value = t1;             
txt.id = "sort_t";              
tds[29].appendChild(txt);              
txt.select();          
}        
function mouseup(){             
if (document.getElementById("sort_t") && event.srcElement.id != "sort_t")             
{                 
var obj = document.getElementById("sort_t").parentElement;             
var txtValue = document.getElementById("sort_t").value;             
obj.innerText = txtValue;             
}         
}      
</script></head><body><form><table border="1">
<?php
$db = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;);
mysql_select_db(&#39;ec&#39;,$db);
if (!$db)
  {  
  die(&#39;Could not connect: &#39; . mysql_error());  
  }
  $result = mysql_query("select * from goods");
  echo "<tr><th>GoodsID</th><th>BarCode</th>
  <th>GoodsName</th>
  <th>Category</th>
  <th>Specifications</th>
  <th>Manufacturers</th>
  <th>Numbers</th>
  <th>Instructions</th>
  <th>Pictures</th>
  <th>Update</th>
  <th>Delete</th></tr>";
  $n = 0;while($row = mysql_fetch_array($result))  
  {  
  echo "<tr>";  
  echo "<td>" . $row[&#39;id&#39;] . "</td>";  echo "<td>" . $row[&#39;barcode&#39;] . "</td>";  
  echo "<td>" . $row[&#39;goods_name&#39;] . "</td>";  
  echo "<td>" . $row[&#39;category&#39;] . "</td>";  
  echo "<td>" . $row[&#39;specifications&#39;] . "</td>";  
  echo "<td>" . $row[&#39;manufacturers&#39;] . "</td>";  
  echo "<td>" . $row[&#39;number&#39;] . "</td>";  
  echo "<td>" . $row[&#39;instruction&#39;] . "</td>";  
  echo "<td>" . "<img src = &#39;$row[picture_url]&#39; style=&#39;width:80px;height=60px&#39;/>" . "</td>";  
  echo "<td id=&#39;bg$n&#39;>".     
  "<input id=&#39;btnshow$n&#39; type=&#39;button&#39; value=&#39;Update&#39; onclick=&#39;showtd($n)&#39;/>".     
  "</td>".     
  "<td id=&#39;show$n&#39; style=&#39;display:none&#39;>".         
  "<input id=&#39;btnclose$n&#39; type=&#39;button&#39; value=&#39;Save&#39; onclick=&#39;hidetd($n)&#39;/>".     
  "</td>";  echo "<td>"."<a href=&#39;delete.php?id=$row[id]&#39;>"."Delete"."</a>"."</td>";   
  echo "</tr>";  
  ++$n;  
  }
  mysql_close($db);
  ?>
  </table></form>
  </body></html>

回复讨论(解决方案)

1.给你的save按钮一个onclick方法 点击后触发 
 2.用ajax 提交参数到后台,调用php程序 
 3.然后将接收内容提交到数据库 
 4.成功or失败返回到页面

你这里要用到ajax来提交数据

1.给你的save按钮一个onclick方法 点击后触发 
 2.用ajax 提交参数到后台,调用php程序 
 3.然后将接收内容提交到数据库 
 4.成功or失败返回到页面 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8"><title>Insert title here</title><script language="javascript" type="text/javascript">         
function showtd(num) {                        
document.getElementById("bg"+num).style.display = "none";             
document.getElementById("show"+num).style.display = "block";         
}         
function hidetd(num) {             
document.getElementById("bg"+num).style.display = &#39;block&#39;;            
 document.getElementById("show"+num).style.display = &#39;none&#39;;         
 }         
 function change()          
 {              
 var tds = document.getElementsByTagName("td");            
  var t1 = document.getElementById("a").innerHTML;             
  var txt = document.createElement("input");              
  txt.type = "text";              
  txt.value = t1;             
  txt.id = "sort_t";              
  tds[30].appendChild(txt);              
  txt.select();          
  }        	
  function mouseup(){            
  if (document.getElementById("sort_t") && event.srcElement.id != "sort_t")            
  {                
  var obj = document.getElementById("sort_t").parentElement;            
  var txtValue = document.getElementById("sort_t").value;            
  obj.innerText = txtValue;            
  }         
  }      
  </script></head><body>	
  <form>	
  <table border="1">	
  <?php	
  $db = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;);	
  mysql_select_db(&#39;ec&#39;,$db);	
  if (!$db)	 
  {	 
  die(&#39;Could not connect: &#39; . mysql_error());  
  }	
  $result = mysql_query("select * from goods");	
  echo "<tr>	
  <th>GoodsID</th>	
  <th>BarCode</th> 
  <th>GoodsName</th>	
  <th>Category</th>	
  <th>Specifications</th>	
  <th>Manufacturers</th>	
  <th>Numbers</th> 
  <th>Instructions</th>	
  <th>Pictures</th>	
  <th>Update</th>	
  <th>Delete</th>	
  </tr>";	
  $n = 0;	
  while($row = mysql_fetch_array($result))
  	 {	 
  	 echo "<tr>";	 
  	 echo "<td>" . $row[&#39;id&#39;] . "</td>";	 
  	 echo "<td id=&#39;a&#39;>" . $row[&#39;barcode&#39;] . "</td>";	 
  	 echo "<td>" . $row[&#39;goods_name&#39;] . "</td>";	 
  	 echo "<td>" . $row[&#39;category&#39;] . "</td>";	 
  	 echo "<td>" . $row[&#39;specifications&#39;] . "</td>";	 
  	 echo "<td>" . $row[&#39;manufacturers&#39;] . "</td>";	 echo "<td>" . $row[&#39;number&#39;] . "</td>";	 
  	 echo "<td>" . $row[&#39;instruction&#39;] . "</td>";	 
  	 echo "<td>" . "<img src = &#39;$row[picture_url]&#39; style=&#39;width:80px;height=60px&#39;/>" . "</td>";	 
  	 echo "<td id=&#39;bg$n&#39;>".     
  	 "<input id=&#39;btnshow$n&#39; type=&#39;button&#39; value=&#39;Update&#39; onclick=&#39;showtd($n);change()&#39; />".     
  	 "</td>".     
  	 "<td id=&#39;show$n&#39; style=&#39;display:none&#39;>".         
  	 "<input id=&#39;btnclose$n&#39; type=&#39;button&#39; value=&#39;Save&#39; onclick=&#39;hidetd($n);mouseup()&#39;/>".     
  	 "</td>";	 
  	 echo "<td>"."<a href=&#39;delete.php?id=$row[id]&#39;>"."Delete"."</a>"."</td>";	  
  	 echo "</tr>";	 	
  	 ++$n;	 
  	 }	
  	 mysql_close($db);
  	 ?>	
  	 </table></form>
  	 </body></html>

我表上红色的部分是一个数组,我想让这个数组变成动态的,要不然所有按钮都只操作一条数据。有什么好办法没?

1.给你的save按钮一个onclick方法 点击后触发 
 2.用ajax 提交参数到后台,调用php程序 
 3.然后将接收内容提交到数据库 
 4.成功or失败返回到页面 
 不好意思是第24行,刚才没标上颜色。

要是我做的话就是都设为隐藏的 
 一个.show() 
 一个.hide() 
 当点击完一个操作就触发函数转换到另一个按钮

建议lz选择jq选择器都很强大 
 搭配ajax 足够完成大量input提交工作

你这里要用到ajax来提交数据 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8"><title>Insert title here</title><script language="javascript" type="text/javascript">         
function showtd(num) {                         
document.getElementById("bg"+num).style.display = "none";             
document.getElementById("show"+num).style.display = "block";         
}         
function hidetd(num) {             
document.getElementById("bg"+num).style.display = &#39;block&#39;;             
document.getElementById("show"+num).style.display = &#39;none&#39;;         
}         
function change()          
{             
 var tds = document.getElementsByTagName("td");             
 var t1 = document.getElementById("a").innerHTML;             
 var txt = document.createElement("input");              
 txt.type = "text";              
 txt.value = t1;             
 txt.id = "sort_t";              
 tds[29].appendChild(txt);             
 txt.select();          
 }        
 function mouseup(){             
 if (document.getElementById("sort_t") && event.srcElement.id != "sort_t")             
 {                 
 var obj = document.getElementById("sort_t").parentElement;             
 var txtValue = document.getElementById("sort_t").value;             
 obj.innerText = txtValue;             
 }         
 }      
 </script></head><body><form><table border="1">
 <?php
 $db = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;);
 mysql_select_db(&#39;ec&#39;,$db);
 if (!$db)  
 {  
 die(&#39;Could not connect: &#39; . mysql_error());  
 }
 $result = mysql_query("select * from goods");
 echo "<tr><th>GoodsID</th>
 <th>BarCode</th><th>GoodsName</th><th>Category</th><th>Specifications</th><th>Manufacturers</th>
 <th>Numbers</th><th>Instructions</th><th>Pictures</th><th>Update</th><th>Delete</th></tr>";
 $n = 0;
 while($row = mysql_fetch_array($result))
   {  
   echo "<tr>";  
   echo "<td>" . $row[&#39;id&#39;] . "</td>";  
   echo "<td id=&#39;a&#39;>" . $row[&#39;barcode&#39;] . "</td>";  
   echo "<td>" . $row[&#39;goods_name&#39;] . "</td>"; 
    echo "<td>" . $row[&#39;category&#39;] . "</td>";  
    echo "<td>" . $row[&#39;specifications&#39;] . "</td>";  
    echo "<td>" . $row[&#39;manufacturers&#39;] . "</td>";  
    echo "<td>" . $row[&#39;number&#39;] . "</td>";  
    echo "<td>" . $row[&#39;instruction&#39;] . "</td>";  
    echo "<td>" . "<img src = &#39;$row[picture_url]&#39; style=&#39;width:80px;height=60px&#39;/>" . "</td>";  
    echo "<td id=&#39;bg$n&#39;>".     
    "<input id=&#39;btnshow$n&#39; type=&#39;button&#39; value=&#39;Update&#39; onclick=&#39;showtd($n);change()&#39; />".     
    "</td>".     
    "<td id=&#39;show$n&#39; style=&#39;display:none&#39;>".         
    "<input id=&#39;btnclose$n&#39; type=&#39;button&#39; value=&#39;Save&#39; onclick=&#39;hidetd($n);mouseup()&#39;/>".    
     "</td>"; 
      echo "<td>"."<a href=&#39;delete.php?id=$row[id]&#39;>"."Delete"."</a>"."</td>";   
      echo "</tr>"; 
       ++$n;  
       }
       mysql_close($db);
       ?>
       </table></form></body></html>

第24行的那个数组能不能改成动态的,要不然所有按钮都只操作同一条数据,应该怎么改啊?

<html><head><script type="text/javascript" src="/jquery/jquery.js">
</script><script type="text/javascript">
$(document).ready(function(){  $(".btn1").click(function(){  
$(".btn1").hide();  $(".btn2").show();  });  
$(".btn2").click(function(){  
$(".btn1").show();  
$(".btn2").hide();  
});
});
</script></head><body><button class="btn1" style="display:none">update</button><button class="btn2">save</button></body></html>

刚才描述的不是很清楚 写了个测试的代码 你可以参考

XML/HTML code 
 ? 
12345678910111213141516171819202122 

100db36a723c770d327fc0aef2ce13b1 93f0f5c25f18dab9d176bd4f6de5d30e 5046bdce3c6c6f0cad63c086163d68ef2cacc6d41bbb37262a98f745aa00fbf0 5cd6e472395e766622bc5d31b556eb7a $(document).ready(function…… 
 谢谢你,我没学过ajax和jq。

items = tds.join(&#39;,&#39;);

好固执的孩纸啊

function change(obj)      
    {   
    //可以传递个当前对象过来             
    var tds = document.getElementsByTagName("td");             
    var t1 = document.getElementById("a").innerHTML;             
    var txt = document.createElement("input");              
    txt.type = "text";              
    txt.value = t1;             
    txt.id = "sort_t";              
    obj.parentNode.childNodes[1].appendChild(txt);   
    //添加至第二个td,可根据需要修改                         
    txt.select();          
    }

调用时, 
 change(this) 
 还有你的id命名重复了,这样只能获取第一个


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在Laravel中使用Flash会话数据在Laravel中使用Flash会话数据Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP记录:PHP日志分析的最佳实践PHP记录:PHP日志分析的最佳实践Mar 10, 2025 pm 02:32 PM

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

php中的卷曲:如何在REST API中使用PHP卷曲扩展php中的卷曲:如何在REST API中使用PHP卷曲扩展Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

简化的HTTP响应在Laravel测试中模拟了简化的HTTP响应在Laravel测试中模拟了Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12个最佳PHP聊天脚本在Codecanyon上的12个最佳PHP聊天脚本Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

解释PHP中晚期静态结合的概念。解释PHP中晚期静态结合的概念。Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

自定义/扩展框架:如何添加自定义功能。自定义/扩展框架:如何添加自定义功能。Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境