0

I have this function http://jsfiddle.net/wnFsE/ . As you can see every time the user writes a languages on the textfield and then hits the button add a new language is added to the list. Then when the user hits save, all the info is sent with POST.

Until now, everything is ok. BUT, when the user sends the info, the info is stored on my MySQL database. So, every time when the user comes again to the page, he needs to see the languages already saved in the last session.

In my PHP script every time the user comes, I retrieve the languages from my database, but I don't know how to add them to the list. The solution would be call the $("#add").click(function() { from the PHP script who retrieve the data from the MySQL, but I don't know how to do this.

If it serves for any help, this is the simple PHP function which retrieves the languages from the user:

function list_lang() {
    $this->DBLogin();
    $result = mysql_query("SELECT * FROM ".table_lang." WHERE id_user=1");
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo $row['lang']." ".$row['level']."<br>";
        //instead of the echo, here will be the call to jquery (if possible)
    } 
}

2 Answers 2

1

A simple soloution:

$buildHTML = '';

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $buildHTML .= '<li>'.$row['lang'].'<input type="hidden" name="languages[]" value="'.$row['lang'].'" /><a href="#" class="remove_lang">Remove</a></li>';
}

And then have

<script type="text/javascript">
    var UserLangs = <?php echo $buildHTML; ?>;

    $('#langs').append(UserLangs);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this way:

<?php
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<script type="text/javascript">
...
...
listitem_html += <?php echo $row['lang']. $row['level'] . '<br>'?>;
...
...
</script>
<?php
} 
?>

1 Comment

There you would output the script for each language, so it has to be outside the loop. But otherwise it would work like he wants it to :-)

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.