user3350885
user3350885

Reputation: 749

Unset php variables before form submit (post) using jQuery

I have two variables $get_book_id and $_GET['book_id'] and a form to post some other data. When the submit button is clicked I want to empty the above two variables and to post the remaining values using form post.

The jquery code looks like this.

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'] = '';
    ?>

process.php

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

What I want to do is, empty two variables using jQuery on form submit. How can I do that ?
In the process page I still see the $_GET values and $get_book_id.

Please don't ask me to empty the value in index.php page.

Unset php variables before form submit (post) using jQuery

Thanks

Kimz

Upvotes: 0

Views: 1966

Answers (2)

ifthiselsethat
ifthiselsethat

Reputation: 206

Updated Answer, use a post request and set the variable to blank if the field is blank:

Change submit input to a button like this: 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>

process.php

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

Upvotes: 1

dreamweiver
dreamweiver

Reputation: 6002

try this way

 $('#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'] = '';
   }    
?>

Happy Coding :)

Upvotes: 0

Related Questions