如何编辑循环MySQL数据?

时间:2015-10-09 04:43:49

标签: javascript php jquery mysql

我得到了正确的saveID但是TemplateData和TitleData它只复制了第一行数据,即使我改变了别的东西。 saveID是数据的唯一ID。我需要的是当我改变它时捕获数据而不是第一列数据。

        <script type="text/javascript">
        $(document).ready(function() {

            $(".save_button").click(function (e) {
                e.preventDefault();
                var saveID =$(this).val(); 
                var url_tna = "<?php echo $_SESSION['url_address'];?>mods/agent_management/code/";


                var TitleData =$("#TitleData").val(); //build a post data structure
                var TemplateData =$("#TemplateData").val(); 
                var IdData =$("#IdData").val(); 
                alert(saveID);
                alert(TemplateData);
                alert(TitleData);
                var JsonData = {TitleData: TitleData, TemplateData: TemplateData, saveID: saveID};  

                jQuery.ajax({
                    type: "POST", // Post Get method
                    url: url_tna+"email_template_response.php", //Where form data is sent on submission
                    dataType:"text", // Data type, HTML, json etc.
                    data:JsonData, //Form variables
                    success:function(response){
                        window.location.reload(true); // reload before append
                        $("#responds").append(response);
                        document.getElementById("contentText").value = "";
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(thrownError);
                    }
                });
            });
        });
        </script>

    <table>

<tr>
                <td>No</td>
                <td>Title</td> 
                <td>Template</td>
                <td>Recipents</td> 
                <td>Schedule</td>
              </tr>
    <?php $i=1; while ($row = mysql_fetch_array($result)) { ?>


    <tr> 
    <td><?php echo $i; ?></td>

         <td><textarea type="textarea" id="TitleData" name="TitleData" class="1"><?php echo $row['title'];?></textarea></td> 
         <td><textarea type="textarea" id="TemplateData" name="TemplateData" class="1"><?php echo $row['template'];?></textarea></td> 
         <td><button id="recipant"><a id="myLink" href="email_template.php?id=<?php echo $row['id']; ;?>" target="_blank">Open Recipant</a> </button></td> 
         <td><button  id="shedule"><a id="myLink" href="email_schedule.php?id=<?php echo $row['id']; ;?>" target="_blank">Open Shedule</a></button></td> 
         <td><input type="hidden" id="IdData" class="save_button1" name="txtLink" value="<?php echo $row['id']; ?>"></input></td>       
        <td><button id="SaveTemplate" name="SaveTemplate" class="save_button" value="<?php echo $row['id'];?>"><a href="#" style="display:block" class="button insertcolumn" id="<?php echo $row['id'];?>">Save</a></button></td>
         <td><button >Sent Mail Now</button></td> 
    </tr>
    <? $i++; }?>

</table>

        if (!empty($saveID)) {
        $sql2 = "UPDATE at_agent_template set title = '$TitleData', template= '$TemplateData' where id = '$saveID'";
        $result2 = mysql_query($sql2);
        }

enter image description here

3 个答案:

答案 0 :(得分:1)

您的文本框ID是相同的,因此请将id添加到文本框中 喜欢

<td><textarea type="textarea" id="TitleData<?php echo $row['id'];?>" name="TitleData" class="1"><?php echo $row['title'];?></textarea></td>

并获取

var TitleData = $("#TitleData"+saveID).val();

答案 1 :(得分:0)

问题是你的html中再次出现重复的ID,因此当你使用ID选择器时,你只会得到第一个元素引用。

解决方案是使用类或任何其他选择器来选择这些元素,因为当您单击行上的编辑按钮时有一个表格结构,您需要获取同一行中元素的值。另外我看到你的输入元素有名字,所以我们可以使用它而不是ID来获得像

这样的值
$(".save_button").click(function (e) {
    e.preventDefault();
    var saveID = $(this).val();
    var url_tna = "<?php echo $_SESSION['url_address'];?>mods/agent_management/code/";


    var $tr = $(this).closest('tr');
    var TitleData = $tr.find("input[name='TitleData']").val(); //build a post data structure
    var TemplateData $tr.find("input[name='TemplateData']").val();
    var IdData = $tr.find("input[name='dData']").val();
    alert(saveID);
    alert(TemplateData);
    alert(TitleData);
    var JsonData = {
        TitleData: TitleData,
        TemplateData: TemplateData,
        saveID: saveID
    };

    jQuery.ajax({
        type: "POST", // Post Get method
        url: url_tna + "email_template_response.php", //Where form data is sent on submission
        dataType: "text", // Data type, HTML, json etc.
        data: JsonData, //Form variables
        success: function (response) {
            window.location.reload(true); // reload before append
            $("#responds").append(response);
            document.getElementById("contentText").value = "";
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(thrownError);
        }
    });
});

答案 2 :(得分:0)

试试这部分:

<?php $i=1; while ($row = mysql_fetch_array($result)) { ?>
<td><?php echo $i; ?></td>

         <td><textarea type="textarea" id="TitleData_<?php echo $i; ?>" name="TitleData" class="1"><?php echo $row['title'];?></textarea></td> 
         <td><textarea type="textarea" id="TemplateData_<?php echo $i; ?>" name="TemplateData" class="1"><?php echo $row['template'];?></textarea></td> 
         <td><button id="recipant_<?php echo $i; ?>"><a id="myLink" href="email_template.php?id=<?php echo $row['id']; ;?>" target="_blank">Open Recipant</a> </button></td> 
         <td><button  id="shedule_<?php echo $i; ?>"><a id="myLink_<?php echo $i; ?>" href="email_schedule.php?id=<?php echo $row['id']; ;?>" target="_blank">Open Shedule</a></button></td> 
         <td><input type="hidden" id="IdData_<?php echo $i; ?>" class="save_button1" name="txtLink" value="<?php echo $row['id']; ?>"></input></td>       
        <td><button id="SaveTemplate_<?php echo $i; ?>" name="SaveTemplate" class="save_button" value="<?php echo $row['id'];?>"><a href="#" style="display:block" class="button insertcolumn" id="<?php echo $row['id'];?>">Save</a></button></td>
         <td><button >Sent Mail Now</button></td> 
    </tr>
    <? $i++; }?>

if (!empty($saveID)) {
$sql2 = "UPDATE at_agent_template set title = '$TitleData', template= '$TemplateData' where id = '$saveID'";
$result2 = mysql_query($sql2);
}