0

I have a php file with a variable $total as one of its variable.

AS part of my project I have to print a table of results. I call an external javascript whenever i display the table, something like this

echo "<script src='course.js' language='JavaScript'></script>";
echo "<table border='1'>";
echo "<tr>";
echo "<td><td>";

Each row of this table has a checkbox, and i call the script file to check whether the number of checked boxes is not more than 8, for which i need the $total variable which is the total number of rows in the table

My course.js file looks like this (assuming $total)

var count = 0;

for(i=1;i<=$total;i++)
{
    if(document.getElementByTag('course."i"').checked==true)
      count++;
}

if(count==8)
{
    for(i=1;i<total;i++)
    {
        document.getElementById('course."i"').disable=true;
    }
}
}
2

4 Answers 4

4

When you output the HTML from PHP, set a JavaScript variable to equal total before you embed the script. Something like..

echo "<script type="text/javascript">var php_total = '" . $total . "';</script>";
echo "<script src='course.js' type='text/javascript'></script>";
echo "<table border='1'>";
echo "<tr>";
echo "<td>";

Then inside course.js you can just call upon the JS variable php_total.

Honestly though, I'm still not 100% sure what you're trying to do but I don't think it's the right way of doing it. Your JavaScript shouldn't even run either.

You can get the total number of rows with JavaScript already. Also, I'm a bit confused where this "magic number" 8 is coming from.

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

Comments

1

Unless there is a whole heap more to your javascript file than is indicated here, I really don't see why it is needed at all.

It is the php that is outputting the table contents, yes? So it is the php that is initially setting these checkboxes to checked/unchecked? So why can't the php do the count of how many it has to set checked and also set them disabled if the requisite number (8?) are checked?

Comments

0

You could store the variable in a hidden field such as

<input type="hidden" id="total" value="<?=$total?>" />

and then pull the value with javascript with:

var total = document.getElementById("total").value

Comments

0

On the top of my mind there are a couple of ways to do this.

php echo

Just like william said just using echo. This is the easiest way I guess. I also think this looks a little bit dirty if you just past this in your code and it is not platform independent

pass json from url

Almost all languages have the possibility to encode/decode json. in php you could simply do it by calling json_encode and json_decode which transforms PHP code to javascript which you could manipulate

With Jquery.getJson() you could get javascript back and use it. With jsonp you could even work cross domain.

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.