0

Hi I have the following code and was wondering if its possible to make the PHP part repeat so the javascript var is kept upto date even if a new row has been add to the table?

<script type="text/javascript" >
        var total_rows = [];
 <?php
 $con = mysql_connect("localhost", "user", "password");
    $total = mysql_query("SELECT * FROM table");
$num_rows = mysql_num_rows($total);
 ?> 
var total_rows = "<?php echo $num_rows ; ?>";
</script>
6
  • Repeat.... where? When? Commented Oct 21, 2011 at 15:47
  • 4
    PHP: Server side, JavaScript: Client side. Once the data leaves the server there is no PHP anymore. Commented Oct 21, 2011 at 15:48
  • 2
    I think you need to read up on AJAX Commented Oct 21, 2011 at 15:48
  • Could you add an example of desired output? Commented Oct 21, 2011 at 15:48
  • you would have to move the php part to a separate script and use AJAX to access it within your javascript Commented Oct 21, 2011 at 15:48

4 Answers 4

2

Not directly, because PHP is server side only, and has finished executing by the time your JS is executed.

While you could check it without reloading the page using AJAX, I would question whether what you are doing is useful anyway - the client should not care about the number of rows in a table as a general rule - the only real exception being if you are making a database management page, and even then it's usefulness is debatable. If you need to update/delete a row from client input, all you need is the ID of the row, and if you are adding one you should let the database generate a row ID for you with an auto-incrementing primary key.

I suspect you would be better looking at your application's implementation as whole if you have reach a point where you feel you need this information at the client side.

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

Comments

1

You can use AJAX to update parts of your page without reloading the entire page. You should look into that. A good tutorial to start will be http://www.w3schools.com/php/php_ajax_intro.asp.

Repeating the PHP code in HTML itself is not possible, because the PHP code is not available in the HTML page. The HTML page is the result of executing a PHP script.

Comments

0

You would need to use AJAX to hit a file which does the PHP select and returns the desired row.

Comments

-2

You would have to refresh your query to get the latest count, so I would do something like this. this example uses jquery: http://api.jquery.com/jQuery.get/

function get_updated_rows(){       
    $.get('count.php', function(data) {
        var total_rows = data;
    }) 
    return total_rows;    
}

You count.php file would contain the php:

 <?php $con = mysql_connect("localhost", "user", "password");
           $total = mysql_query("SELECT * FROM table");
           $num_rows = mysql_num_rows($total); 
           echo $num_rows;
 ?> 

then when you need the updated count, just use your function: get_updated_rows();

1 Comment

Just no. PHP is server side, you can't just execute from JS like this. Your function will always return the value that was generated when the page was loaded.

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.