AJAX表单转到PHP脚本而不是提交

时间:2017-02-17 23:03:16

标签: javascript php jquery mysql ajax

我正在尝试使用AJAX向数据库发布一个简单的插入查询;但是,我正在使用while循环来生成多个PHP表单,这似乎会导致问题。我对所有的html表单使用相同的类,我一直在尝试在AJAX中使用live函数,但它似乎没有工作。我只是想知道如何异步插入这些数据。

感谢您的帮助。

PHP

 while ($row = $result->fetch_assoc()) {
    ?>  
        <div class='container'>
            <div class='moduleform'>
                <table id="t01">
                <tr><th colspan="3" id='upmost'>
    <?php
                echo 'Module Name: ' . $row['modulename'];
                $innermodname = $row['modulename'];

                echo "</th><th id='upmost'>";
                echo 'CAT Amount: ' . $row['catamount'];
    ?>
                </th></tr>
                <tr>
                    <th>Mark Name</th>
                    <th>Mark Achieved</th>
                    <th>Mark Percentage</th>
                    <th>Comments</th>
                </tr>
    <?php

                $innerquery2 = "SELECT moduleid FROM modules WHERE modulename='$innermodname'";

                if (!$result2 = $db->query($innerquery2)) {
                    die('There was an error running the query [' . $db->error . ']');
                }

                while ($row2 = $result2->fetch_assoc()) {
                    $ninnermodid = $row2['moduleid'];
                }

                $queryinner2 = "SELECT * FROM minimodules WHERE listid='$ninnermodid'";

                if (!$innerresult = $db->query($queryinner2)) {
                    die('There was an error running the query [' . $db->error . ']');
                }

                while ($innerrow = $innerresult->fetch_assoc()) {
                    echo "<tr><td>";
                    echo $innerrow['markname'];
                    echo "</td><td>";
                    echo $innerrow['markachieved'];
                    echo "</td><td>";
                    echo $innerrow['markpercentage'];
                    echo "</td><td>";
                    echo $innerrow['comments'];
                    echo "</td></tr>";
                }

    ?>

            </table>

            <form action='createmark.php' method='post' name='markform'>
                <input name='markname' placeholder='Result Name' maxlength='30'>
                <input name='markachieved' placeholder='Mark Achieved (0 if still to take)' maxlength='4'>
                <input name='markpercentage' step='1' placeholder='Percentage Of Module' maxlength='3' type='number'>
                <input name='comments' placeholder='Comments' maxlength='55'>
                <input name='hiddenid' value='<?php echo $ninnermodid; ?>' type='hidden'>
                <input type="submit" name="submit2" value='Add Mark' class='submit2'>
            </form>
        </div>
    </div>

    <?php

    }

AJAX

   $('document').ready(function() {

       $('.submit2').click(function() { 

             var markname = $('markname').val();
             var markachieved = $('markachieved').val();
             var markpercentage = $('markpercentage').val();
             var comments = $('comments').val();
             var hiddenid = $('hiddenid').val();

             $.ajax({

                url:        'createmark.php',
                type:       'POST',
                dataType:   'json',
                data:       '&markname=' + markname + '&markachieved=' + markachieved + '&markpercentage=' + markpercentage + '&comments=' + comments +'&hiddenid=' + hiddenid

             });         

            return false;

       });
    });

要插入数据库的PHP脚本

$markname = htmlspecialchars($_POST['markname'], ENT_QUOTES, 'utf-8');
$markachieved = htmlspecialchars($_POST['markachieved'], ENT_QUOTES, 'utf-8');
$markpercentage = htmlspecialchars($_POST['markpercentage'], ENT_QUOTES, 'utf-8');
$comments = htmlspecialchars($_POST['comments'], ENT_QUOTES, 'utf-8');
$hiddenid = htmlspecialchars($_POST['hiddenid'], ENT_QUOTES, 'utf-8');

$userid = $_SESSION['userid'];

$stmt = $db->prepare("INSERT INTO minimodules VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param('isiisi', $minimodulesid, $markname, $markachieved, $markpercentage, $comments, $hiddenid);
$minimodulesid = 'NULL';
$stmt->execute();

1 个答案:

答案 0 :(得分:2)

这是错的:

         var markname = $('markname').val();
         var markachieved = $('markachieved').val();
         var markpercentage = $('markpercentage').val();
         var comments = $('comments').val();
         var hiddenid = $('hiddenid').val();

首先,$('markname')是一个选择器,用于查找像<markname>这样的HTML元素。要查找name="markname"的输入,应为$('input[name=markname]')

其次,您需要将选择器限制为提交的表单,因此它应为$(this).find('input[name=markname]')

jQuery提供了一个函数,可以查找表单中的所有输入字段,并以$.ajax$(this).serialize()所需的格式获取所有值。所以代码应该是:

   $('.submit2').click(function() { 
         var data = $(this).serialize();
         $.ajax({
            url:        'createmark.php',
            type:       'POST',
            dataType:   'json',
            data:       data
         });         
        return false;
   });