" method="post">

PHP - How to refresh data in MySQL on radio button click

Gender saving example

<form action="save.php?id=<?=$id?>" method="post">
    <p><label><input name="gender" type="radio" value="male" <?php if($gender=='male'){?>checked="checked"<? }?> /> Male</label></p>
    <p><label><input name="gender" type="radio" value="female" <?php if($gender=='female'){?>checked="checked"<? }?> /> Female</label></p>
</form>

      

Here's an example of updating a value

  if ($_REQUEST['gender']) {
  mysql_query("UPDATE users SET gender='$gender' WHERE id='" . $id . "'") or die(mysql_error());
  }

      

How to do when we click on the floor, the value will be automatically saved to db. Let me know.

+2


source to share


3 answers


Something to turn you off in a prettier way:

  // $_POST is way cooler than $_REQUEST
  if (isset($_POST['gender']) && !empty($_POST['gender'])) {

      // sql injection sucks
      $gender = my_real_escape_string($_POST['gender']);

      // cast it as an integer, sql inject impossible
      $id = intval($_GET['id']);

      if($id) {
          // spit out the boolean INSERT result for use by client side JS
          if(mysql_query("UPDATE users SET gender=$gender WHERE id=$id")) {
              echo '1';
              exit;
          } else {
              echo '0';
              exit;
          }
      }
  }

      



Assuming the same markup, ajaxy's solution (using jQuery ):

<script>
var id = <?=$id?>;

// when the DOM is ready
$(document).ready(function() {

    // 'click' because IE likes to choke on 'change'
    $('input[name=gender]').click(function(e) {

        // prevent normal, boring, tedious form submission
        e.preventDefault();

        // send it to the server out-of-band with XHR
        $.post('save.php?id=' + id, function() {
            data: $(this).val(),
            success: function(resp) { 
                if(resp == '1') {
                    alert('Saved successfully');
                } else {
                    alert('Oops, something went wrong!');
                }
            }
        });
    });
});
</script>

      

+7


source


You cannot do this with PHP alone ... you need JavaScript on this page, which executes onchanged

from radioobutton (s) and executes the PHP script. This is called asynchronous JavaScript and XML or "AJAX" and a quick introduction would be http://www.w3schools.com/ajax/default.asp



+3


source


+1 for karim79 for specifying jQuery / AJAX and $ _POST thingy. Very important.

Here is a solution without jQuery (if you're not interested in learning jQuery right now)

Step 1: Add onchange even to checkbox tags like this:

<p><label><input name="gender" type="radio" value="male" onchange="do_submit()" <?php if($_POST['gender']=='male'){?>checked="checked"<? }?> /> Male</label></p>
<p><label><input name="gender" type="radio" value="female" onchange="do_submit()" <?php if($_POST['gender']=='female'){?>checked="checked"<? }?> /> Female</label></p>

      

Step 3: Add a name attribute to create a tag like this:

<form name="myform" action="check.php" method="post">

      

Step 3: Write an onchange event handler function in javascript:

<script type="text/javascript">
function do_submit() {
  document.forms['myform'].submit();
}
</script>

      

Several important notes.

  • $ _ POST is better than $ _REQUEST.
  • Use <?php

    php tag instead of short form <?

    . It will be deprecated in future php versions.
  • The time to invest in jQuery / AJAX training is 100% worth the time and effort.
+1


source







All Articles