【问题标题】:Unset php variables before form submit (post) using jQuery使用 jQuery 在表单提交(发布)之前取消设置 php 变量
【发布时间】:2014-05-22 03:20:08
【问题描述】:

我有两个变量 $get_book_id$_GET['book_id'] 以及一个用于发布其他数据的表单。单击提交按钮时,我想清空上述两个变量并使用表单发布发布剩余的值。

jquery 代码如下所示。

index.php

    $('#submit_button').click(function(){
                $.ajax({
                type: 'POST',
                url: 'empty_variable.php',
                success: function(data) { 
                },
                error: function(ts) { alert(ts.responseText) }
            });
    });

    <form action="process.php" method="post">

            .................

    </form>

empty_variable.php

    <?php
    $get_book_id = '';
    $_GET['book_id'] = '';
    ?>

进程.php

<?php 
echo $_GET['book_id'];
echo $get_book_id;
print_r($_POST);    
?>

我想要做的是,在表单提交时使用 jQuery 清空两个变量。我该怎么做?
在流程页面中,我仍然看到 $_GET 值和 $get_book_id

请不要让我清空 index.php 页面中的值。

在使用 jQuery 提交(发布)表单之前取消设置 php 变量

谢谢

金兹

【问题讨论】:

    标签: php jquery ajax forms unset


    【解决方案1】:

    更新答案,如果字段为空白,请使用发布请求并将变量设置为空白:

    将提交输入更改为这样的按钮: index.php

    <script type="text/javascript">
        $(document).ready(function() {
             $('#submit_button').on('click', function(e) {
                  $('#book_id').val('');
                  $(this).submit();                     
             });
        });                       
    </script>
    <form id="form_id" action="process.php" method="POST">
        <input id="book_id" name="book_id" type="text" />
        <button id="submit_button">Submit</button>
    </form>
    

    进程.php

    if($_POST['book_id'] = '') $get_book_id = $_POST['book_id'];
    echo $_POST['book_id'];
    echo $get_book_id;
    print_r($_POST);
    

    【讨论】:

    • 如果我想传递 id 而不是 name 我应该怎么做? $('input[id="book_id"]').val(''); ??这是正确的吗?
    • 使用你的代码作为“递归过多”后出现错误如何解决?
    【解决方案2】:

    试试这个方法

     $('#submit_button').click(function(){
                $.ajax({
                type: 'POST',
                data:{"clearBit":"YES"},  //use the data to refresh the specified variables on process.php page
                url: 'empty_variable.php',
                success: function(data) { 
                },
                error: function(ts) { alert(ts.responseText) }
            });
    });
    

    Process.php:

     <?php
       if(isset($_POST['clearBit']) && $_POST['clearBit']=='YES')
       {
         $get_book_id = '';
         $_GET['book_id'] = '';
       }    
    ?>
    

    快乐编码:)

    【讨论】:

    • 不工作,正如我所说,我需要使用 jquery ajax 清空/取消设置它,而不是使用传统的 post 方法。有什么想法吗?
    • 从您的客户端脚本中,您无法控制服务器脚本变量。顺便说一句,您所说的不工作是什么意思?上面的代码无法重置变量?
    • 你好兄弟,我又在 SOF stackoverflow.com/questions/22954809/…987654321@问了一个问题
    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多