elabuwa Posted October 28, 2011 Share Posted October 28, 2011 Hi guys, calling a php page through AJAX. and it works Aok. I wanted the ajax backend php file to call a function in the front end page. even a simple alert from the backend php file wont work. Any help is greatly appreciated. My code to call the alert is below. Front End Page <head> <script language='javascript'> function show_media_type(pub_type){ var url = "get_media.php?media_type=" + pub_type; http.open("GET", url, true); http.onreadystatechange = showmedia; http.send(null); } function confirm_media(){ if(http.readyState == 4){ document.getElementById('header').innerHTML = http.responseText; } } </script> </head> The get_media.php (Backend Page) <?php session_start(); $uid = $_SESSION['my_id']; include 'functions.php'; $user_verify = login_check(); echo "<script language='javascript'>alert('Hi');</script>"; include 'library/database.php'; include 'library/opendb.php'; $sess_id = session_id(); $media_id = $_GET['media_id']; $pid = $media_id; ?> Even the simple alert won't be called. Please let me know if you need the entire code. Cheers Ela Buwa Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/ Share on other sites More sharing options...
Network_ninja Posted October 28, 2011 Share Posted October 28, 2011 is it not possible to call the function after the script? e.g: <head> <script language='javascript'> function show_media_type(pub_type){ var url = "get_media.php?media_type=" + pub_type; http.open("GET", url, true); http.onreadystatechange = showmedia; http.send(null); alert('Hi'); } function confirm_media(){ if(http.readyState == 4){ document.getElementById('header').innerHTML = http.responseText; alert('Hi'); } } </script> </head> Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1282916 Share on other sites More sharing options...
elabuwa Posted October 28, 2011 Author Share Posted October 28, 2011 Hi Ninja, Thanks for your reply. if you can have a look at the backend php file : get_media.php : code, I need to trigger a javascript function depending on the value of $user_verify. I started the function I needed but since it was not called, i tried a simple alert. Any help is greatly appreciated guys Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1282930 Share on other sites More sharing options...
Network_ninja Posted October 28, 2011 Share Posted October 28, 2011 Ok... You can try out this code if you want. frontend.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('.link1').click(function(){ var param = $(this).attr("id"); $.get("backend.php?param="+param, function(data) { $("#result").html(data); }); }); }); </script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $param = 'this is parameter'; echo "<a href='#' class='link1' id='$param'>Pls click me</a>"; ?> <div id="result"></div> </body> </html> backend.php <?php echo "The parameter you pass -- <b>". $_GET['param']."</b>"; echo "<script>alert('success');</script>"; ?> I tested out that code and it's working the way you wanted it to be. Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1282931 Share on other sites More sharing options...
elabuwa Posted October 28, 2011 Author Share Posted October 28, 2011 Ninja, Thank you for your help. Will check it out. Honestly, my know how is not great to understand the the jquery thingi will take bout two hours of scouring the net to understand it due to my lack of knowledge. Could there be a reason for even the below line not to work? echo "\"javascript:selected_media('$pid','$nama');\""; It simply prints the text "selected_media('Value 1','Value 2');" Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1282932 Share on other sites More sharing options...
Network_ninja Posted October 28, 2011 Share Posted October 28, 2011 I also encounter that kind of scenario in one of my project so what I did is the first thing that I mention to you if possible. But if the case is similar to yours I used the code I provided to you. I am not pretty much sure why it cannot call a javascript that way, so most probably that will not also work. Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1282934 Share on other sites More sharing options...
elabuwa Posted October 28, 2011 Author Share Posted October 28, 2011 Hi Ninja, Do you know a link that can help me understand what the above code does? Cheers mate Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1282938 Share on other sites More sharing options...
Network_ninja Posted October 29, 2011 Share Posted October 29, 2011 Did you try out the code? it's somewhat similar in your code. it's just that you are using AJAX and mine is Jquery-AJAX. As I experience if I use AJAX I cannot execute a javascript on the backend file, so I use the Jquery-AJAX. have a look at this page: http://www.w3schools.com/jquery/ajax_get.asp http://api.jquery.com/jQuery.get/ Hav nyc weekend! Quote Link to comment https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/#findComment-1283175 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.