0

I created a little script which allows users to shadow some input values from other inputs. The JavaScript part is working fine, but now when I'm trying to send the form, it doesn't send it, saying that the field the value is copied to is empty. What's the problem here? Thanks!

$(document).ready(function() 
{
    $(":text").blur(function() 
    {
        var input = $(this);
        var id = input.attr("id");
        var fieldname = "#".concat(id);

        for (i = 0; i < fields.length; i++)
        {
            if (fields[i][4] == id)
            {
                var boxname = "#copy_".concat(fields[i][0]);
                if ($(boxname).prop("checked"))
                {
                    var fieldname2 = "#".concat(fields[i][0]);
                    $(fieldname2).val($(fieldname).val());
                }
            }
        }
    });

    $(":checkbox").change(function()
    {
        var input = $(this);
        var id = input.attr("id");
        id = id.substring(id.indexOf("_") + 1, id.length);
        var fieldname = "#".concat(id);

        var checked = input.prop("checked");
        $(fieldname).prop("disabled", checked);

        var field = FindField(0, fields[FindField(0, id)][4]);

        if (checked)
        {
            var fieldname2 = "#".concat(fields[field][0]);
            $(fieldname).val($(fieldname2).val());
        }
    });
});

function FindField(index, value)
{
    for (i = 0; i < fields.length; i++)
    {
        if (fields[i][index] == value) return i;    
    }
    return -1;
}

The form (without some irrelevant stuff):

<form method="post" action="sendform.php">
    <table>

        <?php
            foreach ($fields as $field)
            {
                if ($field[0] == "divider") echo('<tr><td colspan="2"><hr /></td></tr>');
                else 
                {   
                    switch ($field[2])
                    {
                        case FIELD_TEXT:
                        {
                            echo('<tr><td><label for="' . $field[0] . '">' . $field[1] . ': </label></td><td>');

                            if ($field[4] != "") 
                            {
                                foreach ($fields as $field2)
                                {
                                    if ($field2[0] == $field[4])
                                    {
                                        echo('<input type="checkbox" id="copy_' . $field[0] . '" title="Kopeeri väljalt &quot;' . $field2[1] . '&quot;" />');
                                    }
                                }
                            }

                            echo('<input type="text" id="' . $field[0] . '" name="' . $field[0] . '" placeholder="' . $field[1] . '" /></td></tr>');

                            break;
                        }
                    }
                }
            }
        ?>

        <tr><td colspan="2"><hr /></td></tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="submit" value="Saada avaldus" name="saadaavaldus" />
            </td>
        </tr>

    </table>
</form>

A sample of the fields array:

var fields = <?php echo json_encode($fields); ?>

$fields = array
(
    //      ID/name         label/placeholder                       type            regex                                                          field copied from
    array(  "saatjanimi",   "Teie täisnimi",                        FIELD_TEXT,     "^[-,.'\s\pL]*\pL[-,.'\s\pL]\s+[-,.'\s\pL]*\pL[-,.'\s\pL]*$",   ""),
    array(  "meiliaadress", "Teie meiliaadress",                    FIELD_TEXT,     "^([\pL-.\d]+)@([\pL-.\d]+)((\.(\pL){2,63})+)$",                ""),
    array(  "isanimi",      "Lapse isa täisnimi",                   FIELD_TEXT,     "^[-,.'\s\pL]*\pL[-,.'\s\pL]\s+[-,.'\s\pL]*\pL[-,.'\s\pL]*$",   "saatjanimi")
);
11
  • 1
    Do you have a sample of the form? Do all the fields have a name attribute? Commented Apr 13, 2016 at 16:57
  • How is the data being sent from jQuery to PHP? The only real way to send info from JavaScript to PHP (without using a framework like Angular) is through AJAX. PHP should receive the information from the form and not from the jQuery unless you use $.ajax Commented Apr 13, 2016 at 16:58
  • @Baruch It's sent by a regular HTML form, added it to the OP. Commented Apr 13, 2016 at 17:04
  • @ZakariaAcharki Do you mean where it's sent or where it's received? Anyway, added the form. Commented Apr 13, 2016 at 17:05
  • @JonathanKuhn Added the form. Commented Apr 13, 2016 at 17:05

0

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.