user1206724
user1206724

Reputation: 13

How to submit forms with answers in each different result of a 'select mysql query' and insert the answers in mysql database

I have tried this code:

<?php   
$sql="SELECT id, comment FROM comments 
ORDER BY comments.id DESC";

$result = mysql_query($sql) or trigger_error ( mysql_error ( )); 

while($row = mysql_fetch_array($result))
{
  echo "<div>";
  echo $row['comment'];
  echo "</br>";

  // Answer to comment
    echo "<a id='href".$row['id']."' href='#'> Answer </a>";


    echo "<div>";
    echo "Your answer is: ";
    echo "<span id='answerdone'"; 
    echo "</span>";


  echo "</div>";
}
?>

<script>

       $('#href".$row['id']."').click(function() {
    $(this).empty().next.append(<div> <input type='text' value='' name='answer'   
                                 id='".$row['id']."'
                                 <input type='submit' value='Answer' />
                                 </div>);
        }); 


var input = document.getElementById('".$row['id']."'),
    placeholder = document.getElementById('answerdone');

input.onsubmit = function() {
   placeholder.innerHTML = input.value
} 

</script>

In order to make a form appear in each result of a MySQL query and what is written in this form to be written on the same page, after "Your answer is: ". Unfortunately, this isn't working and I don't know why. In addition I want what is written after "Your answer is: " to be inserted as an additional column, in the comments table.

How do I do that and what is wrong?

Upvotes: 0

Views: 89

Answers (1)

Daniel Blaichinger
Daniel Blaichinger

Reputation: 348

You are mixing php and javascript variables.

For example:

echo "<a id='href".$row['id']."' href='#'> Answer </a>";

will appear as:

<a id='href1' href='#'> Answer </a>";

So you can't find this element with javascript:

$('#href".$row['id']."').click(function() {

It has to be something like

$('#href1').click(function() {

You should probably have a look at your javascript-console. There you get the right error-description.

Upvotes: 1

Related Questions