jquery PHP提交表单而不刷新页面并调用函数

0tdrvxhp  于 2022-11-22  发布在  jQuery
关注(0)|答案(2)|浏览(122)

我们已经创建了一个反馈表单,一旦用户提交了反馈,我们希望运行将其提交给Airtable的函数,然后显示Next按钮。
问题:jQuery运行正常,在提交后显示按钮,但是(isset($_POST ['submit'])中的函数根本不保存。
我已经看了很多帖子,但找不到答案。任何帮助都是伟大的!
这是我们当前的代码

public function airtable_function() {
        ?>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function(){ 

                jQuery("#nameFrm").submit(function (e) {
                    e.preventDefault();
                    var frm = jQuery('#nameFrm');
                    var outPut = jQuery('#results');
                    var loadButton = jQuery('#loadingImage');

                    var comments = jQuery('#comments').val();
                    var reason = jQuery('#reason').val();

                    jQuery.ajax({
                    type: 'POST',
                        data:'action=submitForm&comments='+comments+'&reason='+reason,
                        url: 'requests.php',
                        beforeSend: function(){ 
                            loadButton.show(); 
                        },
                        complete: function(data){ 
                            loadButton.show();
                            frm.hide();
                        },
                        success: function(data) { 
                            frm.hide();
                            outPut.html(data);
                    }
                    });
                });

            });
        </script>
        <div> 
            <form action="requests.php" id="nameFrm" name="frmName" method="POST" >
            <p>Please give us feedback</p>
            <select id="reason" name="reason" required>
                        <option value="Choose a reason">Choose a reason</option>
                        <option value="Reason1">Reason1</option>
                        <option value="Reason2">Reason2</option>
                        <option value="Reason3">Reason2</option>
                        <option value="Other">Other</option>
                    </select>
                    <input id="comments" type='text' name='comments' required />
        <input type="submit" value="submit" name="subbtn" >
    </form>
    <div id="loadingImage" style="display:none; text-align:center;">
    <a href="#" class="button-primary">Yes, Cancel Account</a>
    </div>
    </div>
    <div id="results"></div>    
</div>
        <?php
        if (isset($_POST['submit'])){
            $reason = $_POST['reason'];
            $comments = $_POST['comments'];
            save($reason, $comments);
            }
        ?>
        <?php
}
9lowa7mx

9lowa7mx1#

我假设您希望将条目“reason”和“comment”转移到页面“requests.php”中,那么就不需要第二个post请求了,因为您使用 AJAX :

<?php
function airtable_function() {
    ?>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function(){ 

            jQuery("#nameFrm").submit(function (e) {
                e.preventDefault();
                var frm = jQuery('#nameFrm');
                var outPut = jQuery('#results');
                var loadButton = jQuery('#loadingImage');

                var comments = jQuery('#comments').val();
                var reason = jQuery('#reason').val();

                jQuery.ajax({
                type: 'get',
                    data: { 'result' : comments +'*'+reason, 'feedback' : 'true' },
                    url: 'requests.php',
                    beforeSend: function(){ 
                        loadButton.show(); 
                    },
                    complete: function(data){ 
                        loadButton.show();
                        frm.hide();
                    },
                    success: function(data) { 
                        frm.hide();
                        outPut.html(data);
                }
                });
            });

        });
    </script>
    <div> 
        <form action="requests.php" id="nameFrm" name="frmName" method="POST" >
        <p>Please give us feedback</p>
        <select id="reason" name="reason" required>
                    <option value="Choose a reason">Choose a reason</option>
                    <option value="Reason1">Reason1</option>
                    <option value="Reason2">Reason2</option>
                    <option value="Reason3">Reason3</option>
                    <option value="Other">Other</option>
                </select>
                <input id="comments" type='text' name='comments' required />
    <input type="submit" value="submit" name="subbtn" >
</form>
<div id="loadingImage" style="display:none; text-align:center;">
<a href="#" class="button-primary">Yes, Cancel Account</a>
</div>
</div>
<div id="results"></div>    
</div>
<?php
}

“request.php”看起来像这样:

<?php
if(isset($_GET['feedback'])) 
{
$result = $_GET['result'];
$parts = explode("*", $result);
print "reason: ".$parts[1]."<br>";
print "comments: ".$parts[0]."<br>";
}
?>
pn9klfpd

pn9klfpd2#

我可以从片段中看到:

if (isset($_POST['submit'])){

而提交按钮则是:

<input type="submit" value="submit" name="subbtn" >

只需修复以下行:isset($_POST['submit']isset($_POST['subbtn']
希望这对你有帮助。

相关问题