Jump to content

Recommended Posts

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. :D

 

Cheers

Ela Buwa

Link to comment
https://forums.phpfreaks.com/topic/249961-javascript-function-not-called-in-php/
Share on other sites

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>

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

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.

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');"

 

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.

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!  8)

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.