Передача набора введенных пользователем данных в другую область формы

0

Я создал две таблицы. Первый - для почтового адреса (почтовая улица, город и т.д.). Вторая таблица - для адреса доставки. Поля почти такие же, как и почтовый адрес. Предположим, что адрес доставки совпадает с почтовым адресом. Как создать переключатель, который скопирует данные в первую таблицу во вторую таблицу?

Данные, введенные пользователем, будут сохранены в базе данных MySQL.

Может ли кто-нибудь показать мне, как это можно сделать? Вот что я пробовал до сих пор:

<script type="text/javascript">
function data_copy(){
    if(document.form1.copy[0].checked){
        document.form1.txtmailing_add2.value=document.form1.txtmailing_add1.value;          document.form1.Address.txtother_street.value=document.form1.Address.txtmailing_street.value;
        document.form1.Address.txtother_city.value=document.form1.Address.txtmailing_city.value;        document.form1.Address.txtother_state.value=document.form1.Address.txtmailing_state.value;
        document.form1.Address.txtother_postcode.value=document.form1.Address.txtmailing_postcode.value;          document.form1.Address.txtother_country.value=document.form1.Address.txtmailing_country.value;
    }
    else{
        document.form1.Address.txtmailing_add2.value="";
        document.form1.Address.txtother_street.value="";
        document.form1.Address.txtother_city.value="";
        document.form1.Address.txtother_state.value="";
        document.form1.Address.txtother_postcode.value="";
        document.form1.Address.txtother_country.value="";
    }
}

</script>

<form name=form1 method=post action="">
</form>
  • 0
    Я думаю, вы можете использовать javascript, когда радио-кнопка нажата, передать почтовый адрес на адрес доставки и сохранить значения в таблице
  • 0
    Я пытаюсь использовать JavaScript, чтобы решить это. но нет работы.
Показать ещё 2 комментария
Теги:

1 ответ

0
<script type="text/javascript">

function data_copy() {

    // assuming naming conventions are consistent.
    var fieldIDs = ['street', 'city', 'state', 'postcode', 'country'];

    var copy = document.getElementsByName('copy'); //using name attribute for reference

    if (copy[0].checked) {

        // note we are expecting the 'id' attribute to be set for the elements.
        var txtmailing_add1 = document.getElementById('txtmailing_add1');
        var txtmailing_add2 = document.getElementById('txtmailing_add2');

        // make sure we have valid objects before accessing their properties
        if (txtmailing_add1 && txtmailing_add2) {
            txtmailing_add2.value = txtmailing_add1.value;
        }

        // for the remaining fields we can look our field id list and generate an id 
        // by combining the expected convention with the given unique string
        for (var j = 0; j < fieldIDs.length; j++) {                
            var mailingField = document.getElementById('txtmailing_' + fieldIDs[j]);
            var otherField = document.getElementById('txtother_' + fieldIDs[j]);
            if (mailingField && otherField) {
                otherField.value = mailingField.value;
            }
        }

    }
    else {
        // same process, just skipping the 'mailing' fields
        var txtmailing_add2 = document.getElementById('txtmailing_add2');

        if (txtmailing_add2) {
            txtmailing_add2.value = '';
        }

        for (var j = 0; j < fieldIDs.length; j++) {
            var otherField = document.getElementById('txtother_' + fieldIDs[j]);
            if (otherField) {
                otherField.value = '';
            }
        }
    }
}

</script>
<form name="form1" method="post" action="" onsubmit="data_copy();">
copy: <br />
<input type="radio" name="copy" value="1" /> yes<br />
<input type="radio" name="copy" value="0" /> no<br />
<!-- form fields -->
</form>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню