0

enter image description here

I have there an example part of my form, (my form has 4 different fields/div like that) and i cannot think of how i can send the values of those created input fields on php via ajax. Sorry if i can't explain it well. I have searched for this but i can't get the exact answer i'm looking for.

<input type = "text" class = "form-control" placeholder="Knowledgeable/Proficient in.." name = "skills[]" >

I want to use a function which uses some kind of name="skills[]" (or array type) instead of something like name="skills1". tyia!

Jes
  • 27
  • 7
  • I googled "send form data using ajax" and found: https://stackoverflow.com/questions/21662836/send-form-data-using-ajax – KIKO Software Jul 05 '18 at 14:47
  • @KIKOSoftware i checked it, but my problem is i have 4 divs that have multiple created input fields, i'm having problem on how to send it on ajax and fetch it on php. Thanks for answering!! :)) – Jes Jul 05 '18 at 14:51

1 Answers1

0

If you give the skill inputs a class like so

<input type="text" class="form-control skill-input" placeholder="Knowledgeable/Proficient in..">

You can then create an object from your form in javascript (example using jquery)

var skills = [];
$(".skill-input").each(function() {
    skills.push($(this).val());
});
console.log(skills); //final array of skills

var resume = JSON.stringify({
    firstName: $('[name=firstName]').val(),
    lastname: $('[name=lastName]').val(),
    skills: skills
});

$.ajax({
    dataType: 'json',
    url: './submit.php',
    type: 'POST',
    data: {resume:resume}
});

then on your submit.php you can have

$form = json_decode($_POST['resume'], true);
echo $form['skills'];
Prock
  • 410
  • 1
  • 5
  • 20
  • how will i send those values with the other values from my form to my php page via ajax? thasnk you sir :)) – Jes Jul 05 '18 at 14:56
  • updated my post. Just got to my computer, so I will test the code. – Prock Jul 05 '18 at 15:52
  • @Prozky im sorry for the late reply, on $form = json_decode($_POST['resume']); how can i get the value seperately? was it still like $_POST['firstName']; ? and how to get the value of the array skills? thank you so much!! this helps a lot :)) – Jes Jul 11 '18 at 11:47
  • Hi @jes, I edited the bottom of my answer. See if that works for you. – Prock Jul 11 '18 at 18:12
  • thank you so much! will tell you if it worked! sorry for i am busy with finishing this project – Jes Jul 19 '18 at 15:36