dsaba Posted March 30, 2007 Share Posted March 30, 2007 hey i have this sample code: <?php <a href="linkurl" target="_self" onmouseover="<?php echo '<a href="linkurl" target="_self">The link title</a>'; echo '<br>'; echo "Original text:"; echo '<br>'; echo "bla bla bla text in its original language"; ?>">The link title</a> ?> as you can see I am attempting to execute php code with javascript "onmouseover" well the problem is the php code is always being executed, i need to make some kind of check statement to ensure that it will only be executed "onmouseover" how would I do that? and is this possible to do this with php and javascript? -thanks Quote Link to comment https://forums.phpfreaks.com/topic/44867-solved-executing-php-code-with-javascript/ Share on other sites More sharing options...
AndyB Posted March 30, 2007 Share Posted March 30, 2007 onmouseover is javascript/client browser executed. php is server executed. It's not going to happen. Quote Link to comment https://forums.phpfreaks.com/topic/44867-solved-executing-php-code-with-javascript/#findComment-217868 Share on other sites More sharing options...
dsaba Posted March 30, 2007 Author Share Posted March 30, 2007 thanks for the reply andy! yeah i figured that, but i don't know crap about javascript i suppose you get the general idea of what i'm trying to do: 1. on mouseover I want some additional html to display (a link and some text underneath it) how can I do that in javascript solely then? (yes i know this is PHPfreaks and not javascriptfreaks, but this community is so great and knowledable....) Quote Link to comment https://forums.phpfreaks.com/topic/44867-solved-executing-php-code-with-javascript/#findComment-217870 Share on other sites More sharing options...
chronister Posted March 30, 2007 Share Posted March 30, 2007 you will have to use ajax to do it. check out this tutorial http://www.ajaxfreaks.com/tutorials/1/0.php I used it to make a sticky note so that an onBlur event triggers my php code. its pretty easy to use too. Here is my code. the actual call to the ajax function. <div id="main" > </div> <textarea name="notes" cols="28" rows="10" wrap="physical" id="textarea" onblur="javascript: MyAjaxRequest('main','/etc/get.php?notes=','textarea')" type="text"><?php get_notes() ?></textarea> ajaxrequest.js // start ajaxrequest.js // Following is a javascript function that makes a httprequest - AJAX. This is the AJAX bit and all that is needed in that manner. // Only in this one we won't be using XML in our response, we will accept and handle // pure text and html and display this response directly to the user within the // desired <div id> tags. It can even be used to include pure html files as a substitute // solution to the "old" frames method where as no php or other scripting language is nessesary on the server. // but use it with care - it is not a search engine supported method and indexing will fail. Workaround for this is not included here function MyAjaxRequest(target_div,file,check_div) { var MyHttpRequest = false; //var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var var MyHttpLoading = '<div align="center"><img src="/images/loading.gif" border="0" alt="running" / > Loading </div>'; var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead'; if(check_div) { var check_value = document.getElementById(check_div).value; } else { var check_value = ''; } if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product { try { MyHttpRequest = new XMLHttpRequest(); } catch(e) { MyHttpRequest = false; } } else if(window.ActiveXObject) // client use Internet Explorer { try { MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { MyHttpRequest = false; } } } else { MyHttpRequest = false; } if(MyHttpRequest) // browser supports httprequest { var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file if(file_array[1] == 'php') // no query string, just calling a php file { var query_string = '?rand=' + random; } else if(file_array[1] == 'htm' || file_array[1] == 'html') // calling a htm or html file { var query_string = ''; } else // we have presumable a php file with a query string attached { var query_string = check_value + '&rand=' + random; } MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET // handle the httprequest MyHttpRequest.onreadystatechange = function () { if(MyHttpRequest.readyState == 4) // done and responded { document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result } else { document.getElementById(target_div).innerHTML = MyHttpLoading; // still working } } MyHttpRequest.send(null); } else { document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest } } // end of "AJAX" function // Here follows a function to urlencode the string we run through our httprequest, it has nothing to do with AJAX itself // If you look carefully in the above httprequest you se that we use this url_encode function around the file and query_string // This is very handy since we are using GET in our httprequest and for instance // any occurrance of the char # (from textboxes etc) will brake the string we are sending to our file - we don't want that to brake! // It will also convert spaces to + function url_encode(string) { var string; var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?="; var hex = "0123456789ABCDEF"; var encoded_string = ""; for(var i = 0; i < string.length; i++) { var character = string.charAt(i); if(character == " ") { encoded_string += "+"; } else if(safechars.indexOf(character) != -1) { encoded_string += character; } else { var hexchar = character.charCodeAt(0); if(hexchar > 255) { encoded_string += "+"; } else { encoded_string += "%"; encoded_string += hex.charAt((hexchar >> 4) & 0xF); encoded_string += hex.charAt(hexchar & 0xF); } } } return encoded_string; } // end .js file The get.php file contains the code executed. The div above is where the message pops up at. Quote Link to comment https://forums.phpfreaks.com/topic/44867-solved-executing-php-code-with-javascript/#findComment-217874 Share on other sites More sharing options...
dsaba Posted March 30, 2007 Author Share Posted March 30, 2007 thanks a lot chron! currently all i really want to execute is html with javascript and i have figured out some js code to do this in the future i may want to execute php with javascript and I will use this code you gave me Quote Link to comment https://forums.phpfreaks.com/topic/44867-solved-executing-php-code-with-javascript/#findComment-217876 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.