0

My experience with jQuery is very limited, and im on a very steep learning cure.

I have a dynamic form which is generated based on main and sub menu, placing marker points in each row which has an inputfield, I have managed to create a loop which looks for all the marker points and return the field_name and the field_value of any input field.

$('td[edit="1"]').each(function(i, el) {
    field_name = $(el).attr('fieldname');
    field_val = $('#'+field_name).val();

    alert(field_name + " = " + field_val);
});

What I am having trouble with now, is converting this in to an array or JSON and pushing it to a PHP file which can then pick up the results.

Here is an example of how I currently submit a forms data to a PHP file, however is not dynamic as I have to specify which fields and values I want to send.

$.ajax({
    type: "POST",
    url: "form.php",
    data: {
        title : title,
        age : age

    }).done(function(data) {
        results = $(data).find('data').html();

    }).fail(function() {
        alert("error");
    }
);

2 Answers 2

2

If i have clearly understand your problem then You will have to use serialize method of jquery to send fields to php file. You can do like

data:$('form').serialize(),

It will send all the fields to php file.

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

Comments

0

As data you can user Jquery on the form to create an array of all the data: https://api.jquery.com/serializeArray/

    $.ajax({
        type: "POST",
        url: "form.php",
            data: $('#idOfForm').serializeArray()
    }).done(function(data) {
        results = $(data).find('data').html();
    }).fail(function() {
        alert("error");
    });

1 Comment

With serializeArray the name of the field is used to be posted so <input name="fieldName" ... /> --> access in PHP $_POST['fieldName']

Your Answer

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