Home Backend Development PHP Tutorial 求助如何点击按钮更新数据?

求助如何点击按钮更新数据?

May 12, 2018 pm 03:25 PM

我希望点击“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>
Copy after login

回复讨论(解决方案)

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>
Copy after login

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

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>
Copy after login

第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>
Copy after login

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

XML/HTML code
?
12345678910111213141516171819202122

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to Register and Use Laravel Service Providers How to Register and Use Laravel Service Providers Mar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles