1

I have a problem, I want to use jquery ajax to simply input database.

this my code tes.php

<script src='jquery.js'></script>
<script>
  $(document).ready(function(){
     //alert('$'); 

      $('.SAVE').click(function(){
        //alert('&');

        var name = $('.NAME').val();
        var alamat = $('.ALAMAT').val();

        //alert(name);
        //alert(alamat);

           $.ajax({

              type:"POST",
              dataType:"json",
              url:"tes2.php",
              data:$('#COBA').serialize(),
              success: function(response) {

                   alert(response);


               },


           });      

      });


  });


</script>

<form id='COBA'>
  <input type='text' name='NAME' class='NAME' value='septiyo'><br>
  <input type='text' name='ALAMAT' class='ALAMAT' value='semambung'><br>
  <input type='submit' value='SAVE' name='SAVE' class='SAVE'>
</form>

and tes2.php

<?php

$name = $_POST['NAME'];
$alamat = $_POST['ALAMAT'];

print_r($_POST);



?>

But it dont work, I Hope can Get the data On tes2.php so I can Process it with query.

can anyone help me to fix this?

Thanks in Advance

0

3 Answers 3

1

It does work, but you can't see it because when you click on the button, it will refresh the page, you need to prevent default behaviour of the submit button :

 $(document).ready(function(){
  $('.SAVE').click(function(e){
    e.preventDefault(); // ---- this
    var name = $('.NAME').val();
    var alamat = $('.ALAMAT').val();

       $.ajax({
          type:"POST",
          dataType:"json", // remove this if you did't want any response
          url:"tes2.php",
          data:$('#COBA').serialize(),
          success: function(response) {
               alert(response);
           },
       });      
  });
 });

Or give type='button' on save button. Remove dataType:"json" if you doesn't want any response

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

1 Comment

Hi sir, I have try all your advice but still not work and nothing happen.
1

Just add method="post" to your form. Add change input from "submit" to "button".

Demo

8 Comments

stil not work too, is I must use json.stringify or something? for know my data has send to tes2.php
Did you see my demo? Check you console.
I have see your demo, Ok it work, but How can I get it on my php variable? because I can echo ing that variable.
I copied your code as-is added method="post" on the form and changed the url to post to the same page. So my guess would be that there something wrong with your page url. Check if your network tab in the browser when you click the save button
Ok it work, it because json. if i use html it work like charm. :D thanks for helping
|
0

Write data:{NAME:name,ALAMAT:alamat}

This will pass your data to tes2.php.

1 Comment

But I want use serialize because my form full of input text

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.