Jump to content

Recommended Posts

<?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?

 

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.

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()".

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

?>

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

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);
}

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.

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

 

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.