-1

I have a HTML form

<html>
<head></head>
<form>
     <input type="text" name="question[]" />
     <input type="text" name="question[]" />
     <input type="file" name="image" />
     <input type="submit" name="save" /> 
</form>
</html>

Now to submit form with ajax I have ajax code but its not working. It's get only one value.

$("#submit").click(function() { 
    var que_id  = $("input[type='text'][name='question[]']").val();
    $.ajax({
       type: "POST", 
       url:  "action.php",
       data: {"que_id": que_id},
       success: function(result) {
       $("#question_wrap").html(result);
    }
  });
});

How do I do it?

node_man
  • 1,359
  • 4
  • 23
  • 50
  • 5
    Possible duplicate of [Uploading both data and files in one form using Ajax?](https://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax) – Randy Casburn Oct 17 '18 at 04:32

1 Answers1

-1

Send form data to php file using ajax

  1. add enctype to your form

    <form id="questionForm" action="" method="post" enctype="multipart/form-data">
        <input type="text" name="question[]" />
        <input type="text" name="question[]" />
        <input type="file" name="image" />
        <input type="submit" name="save" />
    </form>
    
  2. Pass form data to php file using serialize

    $.ajax({
     url: 'action.php',
     data: $("#questionForm").serialize(),
     method: "post",
     success: function (result) {
             $("#question_wrap").html(result);
           }
    });
    
  3. access form values in PHP file using the field name

    <?php 
       foreach($_POST['question'] as $key => $value){
             // your logic
       }
      $filedata= $_FILES['image'];
    ?>
    
Sachin
  • 789
  • 5
  • 18