【问题标题】:Create button inside table and change while onclick event在表格内创建按钮并在 onclick 事件时更改
【发布时间】:2018-07-13 05:59:59
【问题描述】:

我在其功能中动态创建按钮时遇到问题。我已经在 php 中完美地创建了一个表格,但我不知道如何使用每个按钮上的函数在 php 代码中创建一个按钮。

Example 
ID              NAME            PRICE               ADD
1               STRAW            100                BUTTON 1
2               STRAWRE          300                BUTTON 2
3               STRAWTO          200                BUTTON 3

按钮添加是动态使用 html 表内的循环。当我处理按钮上的单击时,id、name 和 price 项将随文本框一起更改,每个文本框都有一个使用旧文本框的自动值,并且按钮 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";
    }
}

这是代码。当创建按钮时,另一个 td 将使用其值更改为文本框,并且在用户完成编辑后,用户还可以将其保存在添加按钮旁边的保存按钮中,但是当用户单击编辑按钮时会出现保存按钮,如图像以上:

【问题讨论】:

  • 您所描述的某些内容听起来像是需要在 javascript 而不是 php 中完成。客户端与服务器端。
  • 任何答案都将不胜感激 :) 但我应该在 php 代码中连接按钮,因为它将在会话中处理
  • 您能否包含一些您的 php 代码,以便我们查看您已有的内容?
  • 我认为您需要发布一些代码或清理问题。你已经有了一个按钮,你想点击它做什么?
  • 非常感谢,但是如何使用 php 示例连接此函数,例如此示例 imageshack.us/photo/my-images/831/exampleog.png

标签: php html-table


【解决方案1】:

只需在最后一个td 中添加普通按钮即可

<button id="1" name="button">BUTTON 1</button>

并使用一些 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
    });
});

哦...也许你可以这样做

<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>';
?>

已编辑

改变这个 &lt;td&gt;&lt;button id=".$data['kode_barang']." name=\"button\"&gt;Edit&lt;/button&gt;&lt;/td&gt;

&lt;td&gt;&lt;button id=".$data['kode_barang']." name=\"button\" style=\"display:none\"&gt;Edit&lt;/button&gt;&lt;button id=".$data['kode_barang']." name="..$data['kode_barang']."&gt;Edit2&lt;/button&gt;&lt;/td&gt;

然后在 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
    });
});

【讨论】:

  • 非常感谢,但如何使用 php 示例连接此函数,例如此示例 imageshack.us/photo/my-images/831/exampleog.png
  • 使用 php 连接函数是什么意思?我不明白你的问题?,一些案例研究或示例会很好:)
  • 回显“";这是什么错误?
  • oo 很抱歉,只需在 name=\"button\" 上添加斜线,看看是否可行?
  • 很棒的代码!我想投票给你,但不幸的是它需要 15 名声望,但该代码运行良好,但我的问题是如何在单击编辑按钮时在编辑按钮旁边添加另一个按钮。你有解决方案吗?
【解决方案2】:

这是一个使用 javascript 动态创建按钮的示例。

var buttonnode = document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('id','ActionButton');
buttonnode.setAttribute('value','DoAction');
buttonnode.onclick = function(){javascriptFunct();};

我对你的问题有点困惑,但也许这会有所帮助。您可以设置您的 javascript 函数,然后重定向到您服务器上的 php 文件。

【讨论】:

  • 非常感谢,但是如何使用像这个示例imageshack.us/photo/my-images/831/exampleog.png 这样的 php 示例来连接这个函数
  • 那个“示例”只是表格的图像。我认为什么是客户端和什么是服务器端可能会有一些混淆。您的 php 文件中可以包含 html 和 javascript。当用户访问您的代码时,php 将在服务器上运行,然后浏览器将启动结果。然后可以使用 Javascript 进行进一步的计算,我们甚至可以在某个时候重定向到服务器上的另一个 php 文件。诸如按钮之类的东西是客户端元素,不能访问 php 函数。在 javascript 中,我们可以获取存储在 php 变量中的值,但仅此而已。
猜你喜欢
  • 2020-03-24
  • 2021-12-07
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2020-06-11
  • 2016-06-29
  • 2017-03-30
  • 2018-04-03
相关资源
最近更新 更多