0

I am trying to print a value retrieved from a MySQL query with javascript. Here is the code

function editVal(category){

            //alert(category);
            //document.getElementById("editdiv").innerHTML = '<input type = text value='+category+'>';
            <?

                $tagquery = mysql_query("Select latitude from Zones where tag = 'category'");
                while($row=mysql_fetch_array($tagquery)) { ?>
                  alert(category);
                  document.getElementById("editdiv").innerHTML = '<input type="text" value='"+<?=$row['latitude']?>+"'/>'; 
                <?}?>    
        }   

I know the query is working fine as I tested it before in a different form. But for some strange reason this snippet is not entering into the while loop (I tried to find it with alert function). You can have a look of the page here - http://128.233.104.33/gameview/mapv.php

I know it might be good if I would used a web service but for now just trying to solve this problem.

Any help please?

7
  • 1
    Can't you just output the HTML using PHP, instead of using JavaScript for this? Intermingling JS with PHP is generally not a good idea and usually a sign of bad coding practice, let alone you're making it more cumbersome to change your code afterwards. BTW, usability experts recommend placing check boxes before the item names, not after them. Commented Nov 28, 2010 at 21:35
  • OMG ... I can do that.. Trying it! Commented Nov 28, 2010 at 21:36
  • What html/js does this render out as? Commented Nov 28, 2010 at 21:52
  • @zeb - You can anything you want with programing, but this is generally discourage. One of the biggest consequence will be google search engine rank, as google chose to ignore javascript Commented Nov 28, 2010 at 21:54
  • 1
    @zeb - what is not working ? be precise, and keep update your question to make it clearer ... Commented Nov 28, 2010 at 22:12

1 Answer 1

2

You have to make an AJAX call if you want to execute a mysql query with some parameter (here category) in query condition which depends on what you clicked.

Try to understand that, server side code has finished executing and then you clicked on the checkboxes. At this instant client side code is running and even a correct mysql query cannot run if you put it here.

Your query would actually run while the page is being generated by the server, and at that time it would not get the value of category and hence would run with category = '', hence not entering the while loop.

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

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.