php 在表格内创建按钮并在 onclick 事件时更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8493955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create button inside table and change while onclick event
提问by
I have a problem creating a button on the fly within its function. I've created a table perfectly in phpbut I don't know how to create a button inside phpcode using a function on each button.
我在其功能中动态创建按钮时遇到问题。我已经在php中完美地创建了一个表,但我不知道如何使用每个按钮上的函数在php代码中创建一个按钮。
Example
ID NAME PRICE ADD
1 STRAW 100 BUTTON 1
2 STRAWRE 300 BUTTON 2
3 STRAWTO 200 BUTTON 3
The button add is dynamically using looping inside the htmltable. and when I process a click on the button, then the id, name, and price item will be changed with the textbox and each textbox has an auto value using the old one, and button 1 will be added one button left button 1, or button 2, or button 3.
按钮添加动态地使用html表内的循环。当我单击按钮时,ID、名称和价格项目将随文本框一起更改,每个文本框都有一个使用旧文本框的自动值,按钮 1 将添加一个按钮左按钮 1,或按钮 2 或按钮 3。
function cetakdata()
{
if(is_array($_SESSION['pembelian']))
{
$total = "";
echo "Here's your chart" . "<br>";
$max = count($_SESSION['pembelian']);
$th1 = "<th>" . "No" . "</th>";
$th2 = "<th>" . "Nama Barang" . "</th>";
$th3 = "<th>" . "Harga Barang" . "</th>";
$th4 = "<th>" . "Jumlah" . "</th>";
$th5 = "<th>" . "Subtotal" . "</th>";
$th6 = "<th>" . "Add Edit" . "</th>";
echo "<table border=1>";
echo "<tr>";
echo $th1 ;
echo $th2;
echo $th3;
echo $th4;
echo $th5;
echo $th6;
echo "</tr>";
for ($indexo = 0; $indexo < $max; $indexo++) {
echo "<tr>";
echo "<td>" . $indexo."</td>";
echo "<td>" . $_SESSION['pembelian'][$indexo]['namabarang']."</td>";
echo "<td>" . $_SESSION['pembelian'][$indexo]['hargabarang']."</td>";
echo "<td>" . $_SESSION['pembelian'][$indexo]['jumlahbarang']."</td>";
echo "<td>" . $_SESSION['pembelian'][$indexo]['subtotal']."</td>";
echo "<td>" .THIS IS HOW I PUT THE BUTTON ON THE FLY DELETE BUTTON."</td>";
$total += $_SESSION['pembelian'][$indexo]['subtotal'];
echo "</tr>";
}
echo "TOTAL BELANJA = ". $total;
echo "</table>";
}else
{
echo "Chart is still Empty";
}
}
This is the code. When the button created I the other td will be changing into textbox with its value and after user finished edit user can also save it within the save button beside the add button but the save button will be appeared when user click on edit button like the image above :
这是代码。创建按钮时,另一个 td 将更改为其值的文本框,用户完成编辑后,用户还可以将其保存在添加按钮旁边的保存按钮中,但当用户单击图像等编辑按钮时,将出现保存按钮以上 :
采纳答案by Khairu Aqsara
just add like a normal button in the last td
只需像最后一个普通按钮一样添加 td
<button id="1" name="button">BUTTON 1</button>
and for action do with some jquery function
并使用一些 jquery 函数进行操作
$(function(){
$('button[name=button]').click(function(){
var id= $(this).attr("id");
//some action here
//ex:
window.location.href="ex.php?id="+id;
//or an ajax fungtion
});
});
oh... may be you can do with this
哦...也许你可以用这个
<script>
$(function(){
$('button[name=button]').click(function(){
var id= $(this).attr("id");
//some action here
//ex:
window.location.href="ex.php?id="+id;
//or an ajax fungtion
});
});
</script>
<?php
$query = mysql_query("select * from barang");
echo '<table><tr><th>Nama Barang</th><th>Harga Barang</th><th>Stok</th><th>Action</th></tr>';
while($data = mysql_fetch_array($query)){
echo "<tr>
<td>".$data['nama_barang']."</td>
<td>".$data['harga_barang']."</td>
<td>".$data['stok_barang']."</td>
<td><button id=".$data['kode_barang']." name=\"button\">Edit</button></td>
</tr>";
}
echo '<table>';
?>
EDITED
已编辑
changes this
<td><button id=".$data['kode_barang']." name=\"button\">Edit</button></td>
改变这个
<td><button id=".$data['kode_barang']." name=\"button\">Edit</button></td>
to
到
<td><button id=".$data['kode_barang']." name=\"button\" style=\"display:none\">Edit</button><button id=".$data['kode_barang']." name="..$data['kode_barang'].">Edit2</button></td>
<td><button id=".$data['kode_barang']." name=\"button\" style=\"display:none\">Edit</button><button id=".$data['kode_barang']." name="..$data['kode_barang'].">Edit2</button></td>
and on the javascript just add modify just like this
并在 javascript 上添加修改就像这样
$(function(){
$('button[name=button]').click(function(){
var id= $(this).attr("id");
$("button[name="+id+"]").show();
//some action here
//ex:
window.location.href="ex.php?id="+id;
//or an ajax fungtion
});
});
回答by jcmitch
Here is an example of creating a button on the fly with javascript.
这是使用 javascript 即时创建按钮的示例。
var buttonnode = document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('id','ActionButton');
buttonnode.setAttribute('value','DoAction');
buttonnode.onclick = function(){javascriptFunct();};
I'm a little confused about what you're question is asking but maybe this will help a little. You could set your javascript function to then redirect to a php file on your server.
我对您要问的问题有些困惑,但也许这会有所帮助。您可以将 javascript 函数设置为重定向到服务器上的 php 文件。