$ .post不起作用

时间:2013-11-08 19:32:23

标签: php jquery

我正在尝试使用jQuery $ .post和php页面将数据发布到Mysql。我的代码没有运行,没有任何东西被添加到mysql表中。我不确定我创建的路径是否错误,但是我们将不胜感激。

jQuery位置:f_js / tasks / TaskTest.js

    <script type="text/javascript">
    $(document).ready(function(){
        $("#AddTask").click(function(){
            var acct = $('#acct').val();
            var quicktask = $('#quicktask').val();
            var user = $('#user').val();

            $.post('addTask.php',{acct:acct,quicktask:quicktask,user:user}, function(data){
                $('#result').fadeIn('slow').html(data);
            });
        });

    });

</script>

addTask.php(运行jQuery代码)

<?php
include 'dbconnect.php';
 include 'sessions.php';

    $acct = $_POST['acct'];
    $task = $_POST['quicktask'];
    $taskstatus = 'Active';
    //get task Creator
    $user = $_POST['user'];
    //query task creator from users table
    $allusers = mysql_query("SELECT * FROM users WHERE username = '$user'");
    while ($rows = mysql_fetch_array($allusers)) {
        //get first and last name for task creator
        $taskOwner = $rows['user_firstname'];
        $taskOwnerLast = $rows['user_lastname'];
        $taskOwnerFull =  $taskOwner." ".$taskOwnerLast;

    mysql_query("INSERT INTO tasks (taskresource, tasktitle, taskdetail, taskstatus, taskowner, taskOwnerFullName)
            VALUES ('$acct', '$task', '$task', '$taskstatus', '$user', '$taskOwnerFull' )");
    echo "inserted";
 }



 ?>

Accountview.php最后是头版

<html>
 <div class="input-cont ">
                                      <input type="text" class="form-control col-lg-12" placeholder="Add a quick Task..." name ="quicktask" id="quicktask">
                                  </div>
                                  <div class="form-group">
                                      <div class="pull-right chat-features">
                                          <a href="javascript:;">
                                              <i class="icon-camera"></i>
                                          </a>
                                          <a href="javascript:;">
                                              <i class="icon-link"></i>
                                          </a>
                                          <input type="button" class="btn btn-danger" name="AddTask" id="AddTask" value="Add" />
                                          <input type="hidden" name="acct" id="acct" value="<?php echo $_REQUEST['acctname']?>"/>
                                          <input type="hidden" name="user" id="user" value="<?php $username = $_SESSION['username']; echo $username?>"/> 
                                          <div id="result">result</div>
                                      </div>
                                  </div>
 <!-- js placed at the end of the document so the pages load faster -->
    <script src="js/jquery.js"></script>
    <script src="f_js/tasks/TaskTest.js"></script>
    <!--common script for all pages-->
    <script src="js/common-scripts.js"></script>

<script type="text/javascript" src="assets/gritter/js/jquery.gritter.js"></script>
 <script src="js/gritter.js" type="text/javascript"></script>
  <script>
</html>

更新:运行firebug后,由于缺少session_start()行,我发现页面运行不正常。添加该行后,问题现已解决。

1 个答案:

答案 0 :(得分:0)

只需将输入的ID更改为帐户和用户。

<input type="hidden" name="acct" id="acct" value="<?php echo $_REQUEST['acctname']?>"/>
<input type="hidden" name="user" id="user" value="<?php $username = $_SESSION['username']; echo $username?>"/>

或更改JS文件

var user = $('#user1').val();
var acct = $('#acct1').val();