0

EDIT QUESTION: In my WHILE loop, it creates an readonly input text box, and the values are whatever records it pulls from my Database. After each text box, there is an add button, which is suppose to append the value of the text box beside the button, to another div on my page. The only value being added is the first record. I know why, I just can't fix. Any ideas?

    $query = "SELECT `$posResults` FROM test.departments ";
    $result = mysql_query($query);
                        
    if ($result) {
    //output results from database
                                    
    echo 'Software';
    while($row = mysql_fetch_array($result))
    {
      echo '<tr>';
      echo '<td><input type="text" id="rSoft" size="50" name="reqSoft" value="'.$row[0].'" readonly/> <button  id="toForm" value="Add" >Add</button>';
      echo '</td>';
      echo '</tr>';
     }
      echo "<script type='text/javascript'>$(document).ready(function(){ $('button#toForm').click(function(){ var req = document.getElementById('rSoft').value;  $('<tr><td>' + req + '</td></tr>').appendTo('#empInfo');}); });</script>";                                     
     }

Here is an image. When I Hit Add, I want the value to go to the div. Right now, only the first value is being appended. php js add to div

2
  • 2
    You have multiple entities which share an id. Id's are supposed to be unique. Commented Oct 2, 2013 at 20:12
  • Put your js-code into a separate js-file. It would make it easier to maintain and read your code. Commented Oct 2, 2013 at 20:16

2 Answers 2

1

Found out the answer that covers everything: On my main page:

    $query = "SELECT `$posResults` FROM test.departments ";
    $result = mysql_query($query);

    if ($result) {
    //output results from database

    echo 'Software';
    echo '<table>';

    $i = 0;
    while($row = mysql_fetch_array($result))
    {
      echo '<tr><td><input type="text" id="rSoft'.$i.'" value="'.$row[0].'" name="reqSoft" readonly/>';
      echo '<button  id="toForm'.$i.'"  data="rSoft'.$i.'" >Add</button>';
       $i++;
      }
       echo '</table>';
    }

On my JS page:

    /*Add software to form*/
    $(document).ready(function(){
    $('button').on('click' , function(){  
      var inputID = $(this).attr('data');
      var theValue = $('#' + inputID).val();
        $('#empInfo').append('<tr><td>' + theValue + '</td></tr>')
       });  

This will append the data of the input box to where I need it to go. Thank you OIS and Kristen Jukowski for getting me started on the resolution! });

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

Comments

0

The IDs of your inputs need to be unique. You can pass the values, though, as an array if you change the ID to something like this: echo '<td><input type="text" id="reqSoft[]" size="50" name="reqSoft[]" value="'.$row[0].'" readonly/> <button id="toForm" value="Add" >Add</button>'; Your passed values will be available as an array in $_POST['reqSoft']. (First textbox value would be $_POST['reqSoft'][0], etc.)

Similar question/answer here: Dynamically create unique ID for an array of input fields using Javascript

Comments

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.