I want to combine three javascript variable and display this in a textbox as default value

Viewed 30

html code for form

<form action="code.php" method="POST" enctype="multipart/form-data">
    <div class="form-group col-md-12 mb-2">
    <label for="fname" class="form-label">Select District Name<span class="font-weight-bold text-danger">*</span></label>
    <select id="dist" name="distcode" class="form-select" require>
        <option class="active">Select District Name</option>
        <?php
            include '../mysqli_connect.php';
            $sql = "SELECT * FROM tb_district";
            $result = $con->query($sql);
            if ($result->num_rows > 0) { 
                // output data of each row
                while($row = $result->fetch_assoc()) { 
                    ?>

                    <option value="<?php echo $row['dist_code']; ?>"><?php echo $row['dist_name']; ?></option>";
                    <?php
                }
            } 
        ?>
    </select>
    </div>

    <div class="form-group col-md-12 mb-2">
        <label for="fname" class="form-label">Select Block Name<span class="font-weight-bold text-danger">*</span></label>
        <select id="block" name="blockname" class="form-select">
            <option class="active">Select Block Name</option>
        </select>
    </div>

    <div class="form-group col-md-12 mb-3">
        <label for="fname" class="form-label">Select Panchayat Name<span class="font-weight-bold text-danger">*</span></label>
        <select id="panchayat" name="panchayatname" class="form-select">
            <option class="active">Select Panchayat</option>
        </select>
    </div>
    
    <div class="form-group col-md-12 mb-2">
        <label for="fname" class="form-label">USERID<span class="font-weight-bold text-danger">*</span></label>
        <input type="text" class="form-control" id="panchayat" name ="blockcode"  readonly >
    </div>

    <div class="form-group col-md-12">
        <button type="submit" name="save_excel_data" class="btn btn-primary mt-3">Import</button>
    </div>
</form>

And the Javascript for load dependent dropdown list is

$('#dist').click(function() {
    //Get selected Country ID
    var distid = $(this).val();
    $.ajax({
          type      : 'POST',
          url       : '../adddistblockpanchayat/load-distblockajax.php',
          data      : 'dist='+distid, //pass country data
          success   : function(data) {
               $('#block').html(data);
          }
     });
});

$('#block').click(function() {
    //Get selected Country ID
    var blockid = $(this).val();
    $.ajax({
       type      : 'POST',
       url       : '../adddistblockpanchayat/load-distblockajax.php',
       data      : 'block='+blockid, //pass country data
       success   : function(data) {
             $('#panchayat').html(data);
         }
    });
});

now I want to combine the value of the dependent dropdown and display this is a textbox.

1 Answers

You have two ways to do it.

  1. Separate all the values in back end and join them with concatenate operator. Then pull that value with a requests.

  2. Pull the values in front end and join these values with +.Here is a small demo:

    value1=data.val1; //data object is fetched on successful request value2=data.val2; $("textboxselector").val(value1+value2);

[Again] Sorry I missed another part from your code. It seems you want to get values from two different requests. In this case you should write two variables globally in this case(before two requests) and join them together.

Related