skip to Main Content
<tr>
        <td>Credit Account No: </td>
        <td><select name="debitacc">
        <?php
            $sql = "SELECT * FROM account";
            $res = mysqli_query($conn, $sql);
            $count = mysqli_num_rows($res);

            if($count>0)
            {
                while($row=mysqli_fetch_assoc($res))
                {
                    $code = $row['code'];
                    $id = $row['id'];

                    ?>
                    <option value="<?php echo $id;?>"><?php echo $code; ?></option>
                    <?php
                }
            }
            else
            {
                echo "<option value='0'>No Account.</option>";
            }
        ?>

        </select>
        </td>

        <td>Account Name: </td>
        <td><input type="text" name="creditaccname" placeholder="Enter Your Account Name"></td>
        </tr>

I have saved my an account in phpMyAdmin

id  code name
1   1234 bank charge
2   9988 CIMB

How do I if select the code 1234 will automatic echo in <td> Acc name of the code

2

Answers


  1. Code concept sample: Generate HTML/JabaScript like the following

    <select name="DebitAccount" onchange="document.getElementById('AccountName').innerHTML = this.options[this.selectedIndex].getAttribute('AccountName');">
        <option value="AccountID" AccountName="AccountName">AccountCode</option>
        ...
    </select>
    
    Account name: <span id="AccountName"></span>
    

    DISCLAIMER: There are JavaScript conformation concerns regarding inline JavaScript usage and proper HTML definition. You should follow them accordingly, I just wanted to help here with the concept.

    Login or Signup to reply.
  2. It’s easy to store the code name value as a data-attribute in the option, and then reference it at the time of selection using a change event.

    function showAccName(v) {
      let name = document.querySelector('select[name=debitacc] option:checked').dataset.name;
      document.querySelector('#accName').innerHTML = name ? `Code Type: <strong>${name}</strong>` : '';
    }
    <table><tr>
      <td>Credit Account No: </td>
      <td>
        <select name="debitacc" onchange='showAccName(this.value)'>
          <option value='0'>No Account.</option>
          <option value='1' data-name="bank charge">1234</option>
          <option value='2' data-name="CIMB">9988</option>
        </select>
      </td>
      <td>Account Name: </td>
      <td><input type="text" name="creditaccname" placeholder="Enter Your Account Name"></td>
    </tr></table>
    
        <div id='accName'></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search