Andrew
Andrew

Reputation: 243

Ajax updating content from database and display when button clicked

I have a "recruit" button where, when button is clicked via Ajax it has to update the database to "invited" and then the same button should display Invited which is updated to the database. By default the database contains "recruit".

This action happens when button is clicked it updates the database as invited, but another button appears called invited. The recruit button is also there. This is my code.

  <div id='submit_wrp'>        
  <input id='btn_submit' type='submit' class='btn btn-primary active' value= " . $rowchk['recruit'] . "/>
  </div>   ";
 <input type="hidden" type="number" id='REC' value='<?php echo $id ?>'/>
<script>
    $('#btn_submit').click(function () {
        var name = $('#REC').val();
        $.ajax({
            type: 'POST',
            // url: 'testing.php',
            url: 'testing.php?REC=' + name,
            data: 'REC=' + name,
            success: function () {
                // alert("success" + name);
                $('#submit_wrp').load(location.href + ' #btn_submit');

                ///$('#submit_wrp').html(response);




            },
            error: function () {
                alert("error" + name);
            }
        });
    });
</script>

Upvotes: 1

Views: 52

Answers (1)

mplungjan
mplungjan

Reputation: 178094

  1. Use a form submit event OR change the button to a button
  2. after you click you replace the div content with the div containing the submit button from the same page you are on.

So perhaps you mean

<script>
$(function() {
  $('#formID').on("submit",function (e) {
    e.preventDefault()
    var name = $('#REC').val();
    $.ajax({
        type: 'POST',
        // url: 'testing.php',
        url: 'testing.php?REC=' + name,
        data: 'REC=' + name,
        success: function (data) {
            $('#submit_wrp').html(data);
        },
        error: function () {
            alert("error" + name);
        }
      });
    });
  });
</script>

<form id="formID">
  <div id='submit_wrp'>        
   <input id='btn_submit' type='submit' class='btn btn-primary active' value= " . $rowchk['recruit'] . "/>
  </div>   ";
  <input type="hidden" type="number" id='REC' value='<?php echo $id ?>'/>
</form>

Upvotes: 1

Related Questions