thisisnuts123 Posted October 15, 2007 Share Posted October 15, 2007 <?php function phpfunc() { for ( .... ) { ...... } javfunc(); } ?> <javascript> starts... function javfunc() { alert("test"); } basicly i am trying to call a java script funtion when php funtion is called.. javascript should send out an alert.. but it does not work. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/ Share on other sites More sharing options...
kenrbnsn Posted October 15, 2007 Share Posted October 15, 2007 PHP runs on the SERVER, Javascript runs on the CLIENT. You have to output the Javascript code with PHP so the client can interpret it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370161 Share on other sites More sharing options...
BlueSkyIS Posted October 15, 2007 Share Posted October 15, 2007 yep, ditto. php is a server-side programming language. Javascript is browser-side. They don't work together like you seem to think they should. If you need Javascript to do something based on PHP output, you'll need to have PHP write the Javascript code to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370164 Share on other sites More sharing options...
drewjoh Posted October 15, 2007 Share Posted October 15, 2007 What you're trying is not possible. PHP is executed at the server level, and Javascript is executed at the browser level. You could give text to the browser to execute the function if you'd like, for example: Replace <?php javfunc(); with <?php echo '<script type="javascript">javfunc();</script>'; This will output "<script type="javascript">javfunc();</script>" to the browser, and the browser will take that and execute the javascript "javafunc()". Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370168 Share on other sites More sharing options...
thisisnuts123 Posted October 15, 2007 Author Share Posted October 15, 2007 i tried this but still no luck can u provide an example? when i tried to echo out javascript from within php using my debuger i do see that the variables are getting updated but i do not get the alert pop up. Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370171 Share on other sites More sharing options...
Psycho Posted October 15, 2007 Share Posted October 15, 2007 What you are trying to achieve is not possible. PHP builds the HTML page (javascript and all) and then delivers it to the user. What you could do is create an onload event to call a js function and then create that function, using PHP, to alert whatever message you want. Example <?php if ($a = $b) { $js_onload = "alert('The condition was true.');\n"; } else { $js_onload = "alert('The condition was false.');\n"; } echo "<script type=\"text\javascript\">\n"; echo "function runOnLoad() {\n"; echo $js_onload echo "}\n"; echo "</script>\n"; echo "</head><body onload=\"runOnLoad();\">\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370174 Share on other sites More sharing options...
thisisnuts123 Posted October 15, 2007 Author Share Posted October 15, 2007 ok what i have is a ajax page running a bunch of drop boxes.. so when a client picks one of the boxes and drops it in a "dropbox" ajax sends information to php .. php then goes to sql database gets the information depending on which box was drops. now i need to send an alert out.. all this needs to happen without any page refresh so i am geting stuck at the sending out alert i treid to call java script from within PHP for example echo"java etc...." when i use the debuger program i see that php funtion are being passed on tojava script but no alert pops out.. i hope i make sense Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370180 Share on other sites More sharing options...
BlueSkyIS Posted October 15, 2007 Share Posted October 15, 2007 Can we see your AJAX code? you need to add a onreadystatechange handler to your connection object which then invokes alert(), ala: (javascript:) function sndImgReq(action,imgObj) { var xhReq = createRequestObject(); xhReq.open("get", action, true); xhReq.onreadystatechange = function() { if (xhReq.readyState != 4) { return; } var serverResponse = xhReq.responseText; alert("server responded: "+serverResponse; } xhReq.send(null); } Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370182 Share on other sites More sharing options...
thisisnuts123 Posted October 15, 2007 Author Share Posted October 15, 2007 do i just include this in the .JS file? any way to msg u directly? as the ajax code consists of multple funtion and and sajax.php file might get 2 confusing on here Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370186 Share on other sites More sharing options...
BlueSkyIS Posted October 15, 2007 Share Posted October 15, 2007 all i need to see is where "ajax sends information to php". in that piece of code, there should be a place where you create the connection object var xhReq = createRequestObject(); ... after you create the object, you open it: xhReq.open("get", action, true); // action is the server call, for instance "somephp.php?var1=43&var2=453" after you open it, define a function for it's onreadystatechage, (but we only care if readystate == 4, which is telling us it's the data coming back from the server: xhReq.onreadystatechange = function() { if (xhReq.readyState != 4) { return; } var serverResponse = xhReq.responseText; alert("server responded: "+serverResponse; } then send null... xhReq.send(null); chances are everything is already in your code except the part where you define a function for onreadystatechange. Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370188 Share on other sites More sharing options...
thisisnuts123 Posted October 15, 2007 Author Share Posted October 15, 2007 x.onreadystatechange = function() { if (x.readyState != 4) return; sajax_debug("received " + x.responseText); var status; var data; status = x.responseText.charAt(0); data = x.responseText.substring(2); if (status == "-") alert("Error: " + data); else args[args.length-1](data); } x.send(post_data); sajax_debug(func_name + " uri = " + uri + "/post = " + post_data); sajax_debug(func_name + " waiting.."); delete x; } i found this in the sajax file i downloaded.. i do see those alerts which tell me information is being passed thru.. so i need to write something like this .. the way u explain nice thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/73370-calling-javascript-function-from-php-function/#findComment-370196 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.