Jump to content

Recommended Posts

need a little help implementing a couple of php (or js) variables in this function:

 

function toggle_acuras()
{ 
var form   = document.getElementById('Acura'); 
var thanks = document.getElementById('Acura1'); 
((form.checked)? Acura1.style.display='block': Acura1.style.display='none');

} 

 

where i can use

function toggle_cars($variable) // toggle_cars($div_name) ?? //
{ 
var form   = document.getElementById('$checkbox_name'); 
var thanks = document.getElementById('$div_name'); 
((form.checked) ? $div_name.style.display='block' : $div_name.style.display='none');

} 

 

instead of writing 45 js functions... wouldn't this work somehow?

 

the call is made in the checkbox input >>  onclick="toggle_acuras();"

 

thanks in advance

GN

 

Link to comment
https://forums.phpfreaks.com/topic/202855-using-a-php-var-in-a-js-function/
Share on other sites

No, it won't work how you think. You could use PHP to dynamically write the 45 functions, but that is not necessary either.

 

Instead, you should just build a better JS function. I don't see that the var "thanks" is used, but since it is set to an object with a specific ID, I'm assuming you should have used it in the last line instead of specifying the object directly using "Acrura1.style.display".

 

Anyway, this single function should work assuming the forms and divs are all inthe same format where the div ID is the same as the form, except with a "1" appended

function toggle_cars(carType)
{
    var form   = document.getElementById(carType);
    var thanks = document.getElementById(carType+'1');
    thanks.style.display= (form.checked) ? 'block': 'none';
    return;
} 

 

Then change the function calles to this

onclick="toggle_cars('Acura');"

I just found that javascript - i'm a php junkie, not js :o)

 

this is where i got "Acura":

 

<input type="checkbox" value="Acura" id="Acura" name="Acura" onclick="toggle_cars('Acura');">

 

and the div i'm hiding/showing is Acura1, yes.

 

i'll try this and thank you in advance.

 

G

another question related to these divs, Acura1, Audi1, etc...

 

within each div i have a short query calling the models of each of these makes, of course, which means i have ~45 queries in that page, all within hidden divs (initially). i'm certain that those queries are firing anyway, even tho the divs are hidden - is that the case, and is this a bad thing? i notice no slowness, but i surely like to know if what i'm doing is inefficient or not...

 

thanks for you help.

 

GN

another question related to these divs, Acura1, Audi1, etc...

 

within each div i have a short query calling the models of each of these makes, of course, which means i have ~45 queries in that page, all within hidden divs (initially). i'm certain that those queries are firing anyway, even tho the divs are hidden - is that the case, and is this a bad thing? i notice no slowness, but i surely like to know if what i'm doing is inefficient or not...

 

Well, you can either create all the DIVs on page load OR implement AJAX to load only the slected DIV. Each has it's benefits and drawbacks. Loading them all at once, as you are, may take longer for the page to load initially if you have a LOT of data and if you are not loading them efficiently (more on this below). BUt, performance on changing the displayed DIV will typically be faster for the user. As long as you don't have an extraordinary amount of data I would stick to this method.

 

Loading just one DIV and then using AJAX to load new content will make the page load faster, but performance for changing a DIV will degrade since a server request will be needed each time. This is a good method when there is a LOT of data and trying to load it all into the user's browser would not be effective.

 

Having said all that, you should still ensure you are generating the content for all the DIVs efficiently. You state

i have ~45 queries in that page

You should only be running ONE query to get the data for all the DIVs and then use PHP logic to create the DIVs from the single result set. Running multiple queries like that is horribly inefficinet.

 

If you post the code to get the data and create one of the DIVs, I can provide some sample code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.