Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP和jQueryAjax从foreach循环语句中检索这种类型的代码行?_Php_Jquery - Fatal编程技术网

如何使用PHP和jQueryAjax从foreach循环语句中检索这种类型的代码行?

如何使用PHP和jQueryAjax从foreach循环语句中检索这种类型的代码行?,php,jquery,Php,Jquery,如何将这一行echo代码从php文件检索到jquery,我希望得到一行行值 我的php文件 foreach($stmt as $warrior){ echo '<td>'.$warrior['warrior_id'].'</td><td>'.$warrior['warrior_name'].'</td><td>'.$warrior['warrior_type'].'</td>'; }

如何将这一行echo代码从php文件检索到jquery,我希望得到一行行值

我的php文件

foreach($stmt as $warrior){
            echo '<td>'.$warrior['warrior_id'].'</td><td>'.$warrior['warrior_name'].'</td><td>'.$warrior['warrior_type'].'</td>';
        }
我的js脚本

<script>
    function createWarrior() {
        $.post( "create_warrior.php", { 
        "wname": $("#txtWname").val(),
        "wtype": $("#txtWtype").val()           
        }, 
        function(msg){
            //What should I put here
        });
        return false;
    }
</script>
我的html代码

<table>
        <tr>
            <th colspan="3">Warrior</th>
        </tr>
        <tr>
            <th>Warrior ID</th>
            <th>Warrior Name</th>
            <th>Warrior Type</th>
        </tr>
        <tr>
            //the output should be here
        </tr>

添加了类似于注释的内容

我相信应该有用,但没有尝试。

在开头和结尾添加标签

foreach($stmt as $warrior){
            echo '<tr><td>'.$warrior['warrior_id'].'</td><td>'.$warrior['warrior_name'].'</td><td>'.$warrior['warrior_type'].'</td></tr>';
        }
结果将是

<tr><td>warrior_id_1</td><td>warrior_name_1</td><td>warrior_type_1</td></tr>
<tr><td>warrior_id_2</td><td>warrior_name_2</td><td>warrior_type_2</td></tr>
....
您需要在js中找到这个答案并附加到html中:

<script>
  $.ajax({
    url: 'create_warrior.php',
    //Send data to php if you need. In php use $_POST['wname'] to read this data.
    data: {wname: $("#txtWname").val(),wtype: $("#txtWtype").val() },
    success: function(data) {
      $('#dataTable').append(data);
    }
  });

</script>
HTML


它可以工作,但我在onclick事件上调用了该事件,它仍然显示旧值?我怎么才能弄清楚呢?谢谢
foreach($stmt as $warrior){
            echo '<tr><td>'.$warrior['warrior_id'].'</td><td>'.$warrior['warrior_name'].'</td><td>'.$warrior['warrior_type'].'</td></tr>';
        }
<tr><td>warrior_id_1</td><td>warrior_name_1</td><td>warrior_type_1</td></tr>
<tr><td>warrior_id_2</td><td>warrior_name_2</td><td>warrior_type_2</td></tr>
....
<script>
  $.ajax({
    url: 'create_warrior.php',
    //Send data to php if you need. In php use $_POST['wname'] to read this data.
    data: {wname: $("#txtWname").val(),wtype: $("#txtWtype").val() },
    success: function(data) {
      $('#dataTable').append(data);
    }
  });

</script>
<table id = "dataTable">
        <tr>
            <th colspan="3">Warrior</th>
        </tr>
        <tr>
            <th>Warrior ID</th>
            <th>Warrior Name</th>
            <th>Warrior Type</th>
        </tr>
</table>