0

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

2
  • Have you found solution? Please vote for the answer(s) that helps you. Commented Feb 6, 2018 at 6:55
  • No solution yet, I have tried all solution provided and its not working Commented Feb 6, 2018 at 11:13

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

By the way.. your code is very confusing. You try to make a GET request on your link whilst trying to submit some kind of form and then you got an ajax request where I have no idea how you submit that one.
<a href="?id=1&pid=238874" id="day" onclick="this.form.submit();><button type="button" class="label label-danger" >Select</button></a> id="day" miss out. I have change it to POST not working yet
Have you checked the output of $("select.day").serialize(). Will this return any data at all?
No... I have use some values to serialize it. My aim is to get those id=1&pid=238874 on a click
1

Try with this :

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

Comments

0

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); 
    ?>

Comments

0
<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); 
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.