作为我的ajax帖子更好的解决方案?

时间:2018-06-04 23:24:56

标签: php jquery ajax

我有以下php代码:

                            <tr>
                            <td class="vacancyID">
                                <?php echo $vacancyID; ?>
                            </td>
                            <td class="roleLongTitle">
                                <?php echo $roleLongTitle; ?>
                            </td>
                            <td class="roleRequirements">
                                <?php echo $roleRequirements; ?>
                            </td>
                            <td class="roleResponsibilities">
                                <?php echo $roleResponsibilities; ?>
                            </td>
                            <td class="roleQualifications">
                                <?php echo $roleQualifications; ?>
                            </td>
                            <td class="closeDate">
                                <?php
                                    //Convert the mySQL date to PHP date
                                    echo convertToDate($closeDate);
                                ?>
                            </td>
                            <td>

以下jquery代码:

$(document).ready(function(){
var btn = $('a.btn-primary'); 
var closeDate = $('td.closeDate');
var applyBtn = $('<input type="button" value=" Apply " class="toggleButton" />');
//var applyLnk = $('<a href="index.php" class="btn btn-primary" role="button">Apply</a>');

//Rmove a link
btn.remove();

//Add button
applyBtn.insertAfter(closeDate);
//applyBtn.appendTo(closeDate);
  //applyLnk.insertAfter(closeDate);

$('.toggleButton').click(function(){
    var obj = $(this).closest('tr');
    var data = {
            vacancyID: obj.find('td.vacancyID').text(),
            closeDate: obj.find('td.closeDate').text(),
            roleLongTitle: obj.find('td.roleLongTitle').text(),
            roleRequirements: obj.find('td.roleRequirements').text(),
            roleResponsibilities: obj.find('td.roleResponsibilities').text(),
            roleQualifications: obj.find('td.roleQualifications').text(),
        }

         //Post the data to the page
        $.ajax({
             type: 'POST',
             url: 'vacancy.php',
             data: data,
             success: function(data){
                window.location.assign("applyForVacancy.php");
             }
        });
        //console.log(data);
    });

});

我有什么方法可以让它变得更好吗?我尝试了很多选项,这是唯一可以使用的选项,可以根据我点击的按钮访问这些发布的值。

0 个答案:

没有答案