获取选中的表行文本值并发送到API

时间:2017-10-10 03:23:56

标签: javascript php jquery

当选择特定行时,行文本将获得例如电话号和消息



$("#btn").on("click", function () {
    var result = $("tr:has(:checked)")

    if (result.length < 2) {
        alert("Please Select 2 to 4 Models");
    } else if (result.length > 4) {
        alert("Please Select 2 to 4 Models");
    } else {
        console.log(result);
    }
    
    var json = result.map(function () {
        return [$(this).children().slice(1).map(function () {
            return $(this).text().trim()
        }).get()]
    }).get()
  alert(JSON.stringify(json,0,"\t"))
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<table border="1" width="100%">
    <thead>
       <th>select</th> <th>phone no</th> <th>message</th>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type='checkbox' />
            </td>
            <td>123456</td>
            <td>hi</td>
        </tr>
        <tr>
            <td>
                <input type='checkbox' />
            </td>
            <td>1234567</td>
            <td>hello</td>
        </tr>
        <tr>
            <td>
                <input type='checkbox' />
            </td>
            <td>4561234</td>
            <td>hey</td>
           
        </tr>
    </tbody>
</table>
<input id="btn" type="button" label="button" value="send" />
&#13;
&#13;
&#13;

现在,我想使用php将这些数据发送到API以发送消息。 API需要2个字段

   {

   "phone": "examplestring",
   "message": "examplestring"

    }

上表是硬编码的,所以你们可以更好地了解我的桌子设置。我的真实表使用while循环来回显记录的每一行

 <table>
<?php while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {?>
  <td><input type="text"value="<?php echo $row['phone'] ?>"></td>
   <td><input type="text"value="<?php echo $row['message'] ?>"></td>

    $specific[] = [
                 "phone" => $row["phone"],
                 "message" =>$row["message"]
               ];
    $result = json_encode($specific,JSON_UNESCAPED_UNICODE)              
     ];

    <?php } ?>
    </table>

变量$ result是一个由多个对象文字组成的数组,这些文字将被发送到API以发送消息

所以现在我试图将这些消息发送给那些已被检查过的电话。但它不起作用。任何想法都会很棒,谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用jquery创建json对象。给td打电话号码&#39;电话&#39;和td有消息类&#39; msg&#39;。然后使用jquery,您可以提取要在json对象中推送的细节。希望这会有所帮助..

   <form method="post" action="test.php" id="msgForm">
   <table border="1" width="100%">
        <thead>
           <th>select</th> <th>phone no</th> <th>message</th>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input type='checkbox' />
                </td>
                <td class='phone'>123456</td>
                <td class="msg">hi</td>
            </tr>
            <tr>
                <td>
                    <input type='checkbox' />
                </td>
                <td class='phone'>1234567</td>
                <td class="msg">hello</td>
            </tr>
            <tr>
                <td>
                    <input type='checkbox' />
                </td>
                <td class='phone'>4561234</td>
                <td class="msg">hey</td>

            </tr>
        </tbody>
    </table>
   <input type="hidden" id="jsonMsgs" name="jsonMsgs" />
    <input id="btn" type="button"  value="send" />
   </form>
    <script>
    $(document).ready(function () {
        var jsonObj = [];

        $("#btn").on("click", function () {
            var result = $("tr:has(:checked)");

            if (result.length < 2) {
                alert("Please Select 2 to 4 Models");
            } else if (result.length > 4) {
                alert("Please Select 2 to 4 Models");
            } else {
                console.log(result);
            }

            $.each( result, function(key, value) {
                var field1 = $(this).find('td.phone').text();
                var field2 = $(this).find('td.msg').text();

                var item = {};
                item["phone"] = field1;
                item["message"] = field2;

                jsonObj.push(item);
            });
            console.log(jsonObj);
            alert(JSON.stringify(jsonObj));

            $("#jsonMsgs").val(JSON.stringify(jsonObj));
            $("#msgForm").submit();
        });
    });      
    </script>

test.php可能是:

<?php     
$json = $_POST["jsonMsgs"];

//var_dump(json_decode($json));
var_dump(json_decode($json, true));

$data = json_decode($json, true);


foreach($data as $ind) {
    echo $ind['phone'] . "<br/>";
    echo $ind['message'] . "<br/>";
}
?>