duckstar Posted November 20, 2009 Share Posted November 20, 2009 Hi there, i would really appreciate some help on the following matter. Been stuck for hours trying to do this simple process. I have a php page, with includes a js page full of my functions. On my php page i have a button which calls a javascript function from my js file parsing varibles. All is good and happy. I want to add a if statement at the bottom of my php page. If (myvalue == 1) { (Run my function, the same one that is being ran on my button) } There is no click event or anything.. its just if by the end of my page that varible equals 1 i want the button to press without the user having to click it. I have tried using jQuery, but to no luck. Thanks in advance, Duckstar Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/ Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 if your using jQuery it will be <script type="text/javascript"> $(document).ready(function(){ your_function(); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961786 Share on other sites More sharing options...
duckstar Posted November 20, 2009 Author Share Posted November 20, 2009 So that would go into my if command ? Like so ? if($numOfDrill == 1) { echo("<script type='text/javascript'>"); echo("$(document).ready(function()"); echo("{"); echo("alert('loaded');"); echo("});"); echo("</script>;"); } Because this isn't working at the moment I have included the jQuery.js at the top of my page. I am not sure if it is correct to use all the echo's. But if i don't use them, it errors on the first '<' Cheers, Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961787 Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 should be if($numOfDrill == 1) { echo("<script type='text/javascript'>"); echo('$(document).ready(function()'); echo("{"); echo("alert('loaded');"); echo("});"); echo("</script>;"); } Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961788 Share on other sites More sharing options...
duckstar Posted November 20, 2009 Author Share Posted November 20, 2009 Hi, This still isn't working for me.. I will quickly explain a little more about my php pages.. I have a main page, in which my .js files are added to the $gsPageHeadInc Then this page starts a java function which uses AJAX to display a php page in a div. It is this page loaded up in the div which i want to include this if statement. I copied the statement you posted above but it isn't working. The if statement is working though as i can see a ";" appear. I don't think the jquery function is being read properly Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961882 Share on other sites More sharing options...
duckstar Posted November 20, 2009 Author Share Posted November 20, 2009 This is in my first php $gsPageHeadInc = "<script language=\"javascript\" src=\"".$basepath."script/ssCalendar.js\"></script>\n"; $gsPageHeadInc .= "<script language=\"javascript\" type=\"text/javascript\" src=\"".$basepath."script/ajax_inv_check.js\"></script>\n"; $gsPageHeadInc .= "<script language=\"javascript\" type=\"text/javascript\" src=\"".$basepath."script/jquery.js\"></script>\n"; $gsPageHeadInc .= "<script language=\"javascript\"><!--\n"; This is in the php page loaded as a div if($numOfDrill == 1) { echo("<script type='text/javascript'>"); echo('$(document).ready(function()'); echo("{"); echo("alert('loaded');"); echo("});"); echo("</script>;"); } I have double checked that the file is there.. located at C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\TLCMain\script\jquery.js Thanks again. Duckstar Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961886 Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 loads the divs is it being called through ajax ? Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961887 Share on other sites More sharing options...
duckstar Posted November 20, 2009 Author Share Posted November 20, 2009 Sorry i don't understand. I have blank divs in my main page, in which ajax fills with another php page on my functions. So for instance. (phpPage1) When i run myFunction(); it goes to my ajax_inv_check.js, runs the function picking up the different element values and then parses them to phpPage2 which opens in my phpPage1 div It is phpPage2 which has the if statement which will load a function from my ajax_inv_check.js. I think my brain is going to fry in a minute.. Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961894 Share on other sites More sharing options...
duckstar Posted November 20, 2009 Author Share Posted November 20, 2009 Cant find the edit post button. Right i just copied the same if statement code but without the if to my main page and its working.. So my jquery is loading correctly.. Just need to find out why my phps loaded into div's aren't not picking up the jquery functions... Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961901 Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 if they are loaded through ajax the javascript code in them will not execute Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961903 Share on other sites More sharing options...
duckstar Posted November 20, 2009 Author Share Posted November 20, 2009 Solution ? Quote Link to comment https://forums.phpfreaks.com/topic/182264-call-javascript-function-on-php-if/#findComment-961904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.