简体   繁体   中英

Onclick link to save multiple data without refresh

With this code I can see empty data saving into my db once I click on the link button. But i need to the GET's data in my database. Any solution to this.

<a href="?id=1&pid=238874" id="day" onclick="this.form.submit();><button type="button" class="label label-danger" >Select</button></a>

<script>
$('#day').click(function(e) 
       {
         e.preventDefault();
          $.ajax({
               type: 'post',
               url: "index.php",
               data: $("select.day").serialize(),
           });
            return false;
       });
</script>

For PHP Code to save data

<?php
$a = $_GET['id'];
$b = $_GET['pid'];
// query
$sql = "INSERT INTO hos_patient(reg_id,pid) VALUES ('$a','$b')";
mysqli_query($db, $sql); 
?>

Thanks

You specified using POST in your ajax function. But then you try to get the data by GET in your PHP-Script. I'd suggest you just use POST for this.

Alter your PHP-Script to use $_POST instead of $_GET

Try with this :

...
$.ajax({
    type: 'post',
    url: "index.php?id=1&pid=238874",
    data: $("select.day").serialize(),
});
...

Use this in script file and in PHP file use below code

<script>
$('#day').click(function(e) 
       {
         e.preventDefault();
          $.ajax({
               type: 'post',
               url: "index.php?id=1&pid=238874",
               data: $("select.day").serialize(),
           });
            return false;
       });
</script>


    <?php
    $a = $_POST['id'];
    $b = $_POST['pid'];
    // query
    $sql = "INSERT INTO hos_patient(reg_id,pid) VALUES ('$a','$b')";
    mysqli_query($db, $sql); 
    ?>
<a href="?id=1&pid=238874" class="login_form_1"><button type="button" class="label label-danger" >Select</button></a>

<script>
 $(function () {
    $('.login_form_1').on('click', function (e) {
      e.preventDefault();
          $.ajax({
            type: 'GET',
            url: 'show.php?id=22&pid=33',
            data: $('.login_form_1').serialize(),
            success: function (data) {
              $('div.logerrors').html(data);
            }
          });
    });
  });
</script>

For PHP output

<?php
$a = $_GET['id'];
$b = $_GET['pid'];
// query
$sql = "INSERT INTO hos_patient(reg_id,pid) VALUES ('$a','$b')";
mysqli_query($db, $sql); 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM