skip to Main Content

Why SK posted from AJAX is undefined and response is the html of my page?

 <form  class=""  action="" method="post">
                           <form id="kod"  action="" method="post">
                  <input  placeholder="SK"  name="SK" type="text" class="form-control">
                  <button   type="button" name="submit" >Submit form </button>
                  <span ><?php echo $kal ?></span>
                </form>
            <input placeholder="HN" name="HN" type="text" class="form-control">
            <button onclick="skf()"
             type="submit" name="add" value="add" class="btn  btn-md">ADD</button>
                </form>
                <!-- AJAX script -->
    <script>
                let skf = function(){
           var SK = $('#kod input[name=SK]').val();
        console.log('form:', $('#kod').html()); 
         console.log('SK:', SK);
            $.ajax({
              type: 'POST',
              url: 'ajaxRepair.php', 
              data: { SK: SK }, 
              success: function(response){
             alert("hey "+SK);
              }
           });
        };
     </script>       

2

Answers


  1. What is the reason for using two nested forms? This is not the right way to use the form

    Login or Signup to reply.
  2. In your ajax add content type and accept.

               $.ajax({
                  type: 'POST',
                  url: 'ajaxRepair.php', 
                  contentType : application/json // will define content is JSON
                  accept: application/json // always accept only JSON content.
                  data: { SK: SK }, 
                  success: function(response){
                console.log(response);
                  }
               });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search