Jump to content

[SOLVED] using ajax,php,javascript


cmazur

Recommended Posts

Hey Guys,

 

I'm having trouble getting the correct values back to my webpage.

I'm using PHP and JavaScript along with SAJAX.

 

The value of the text that is getting set in the label on my html page is "undefined".

 

Here's the code.

 


Javascript:
             function getText_cb(getText)
{
	document.getElementById("lblText").innerHTML = getText;
}

Php:

function getText()
{
             echo "This is the text";
}

 

Any ideas?

 

Thanks,

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/65117-solved-using-ajaxphpjavascript/
Share on other sites

Try:

<?php
function getText(){
        echo "This is the text";
}
?>
<script type='text/javascript'>
function getText_cb(getText) {
document.getElementById("lblText").innerHTML = '<?php getText(); ?>';
}
</script>

 

I think the problem is that javascript interprets getText as a variable, which hasn't been defined in JS yet, so it's value is undefined.

 

Hope that helps.

Javascript:
             function getText_cb(getText)
{
	document.getElementById("lblText").innerHTML = getText;
}

Php:

function getText()
{
             echo "This is the text";
}

 

I figured it out.

The problem was that I didn't have the right syntax when I was trying to call the callback function (getText_cb).

 

The call should've been x_getText(getText_cb) instead of getText_cb();

If my function getText()took arguments, then then syntax would look like this: x_getText(arg1, arg2, getText_cb);

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.