MyBB Hacks

Full Version: Learning AJAX
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Do you have any recommended Ajax tutorials?
Google?

If you know Javascript, AJAX is easy (cause AJAX is Javascript Tongue).  Thus I'd start by looking into Javascript first.
I know Javascript and PHP well, but I just can't get Ajax. I have tried the sites I normally learn from (w3schools and Tizag), but if I execute the example scripts one, then change the echo output in the php file, the result stays the same as the previous one.

But thats my problem. I vow to learn it one day Wink
Umm, tried sending no cache headers, or clearing the cache?
OK, clearing the cache works, but is highly annoying to do - and is only necessary on Opera. it works fine on IE7 and Firefox 2. Basically I want to click a button, and everytime the button is clicked, the exact time is shown, but in Opera you have to clear the cache EVERY time, so the time does not update.

Sending a no-cache header does work though - thanks a tonne Smile
OK, so I have a few Ajax questions that Zinga will know Wink

1. Can you execute specific PHP functions?
Say all my PHP file contains is

PHP Code:
<?
function name(){echo "something";}
?>


Is it possible to specify (in the JS) to execute only that function?

2. Retrieving a value from an input

How do I do this in the PHP. Say I have

HTML Code
<input name="input" type="text">

  

How do I transfer that to the PHP form without submitting it? Via $_GET?.

That is all my incredibly inportant questions,

BP

Consider something like this:

HTML Code
<form onsubmit="doAjaxSubmit(this);" method="post" action="...">
<input type="text" name="myvalue" />
<input type="submit" value="Go" />
</form>

<script language="javascript">
<!--
function doAjaxSubmit(fObj)
{
 var requestString = fObj.elements[0].name+'='+fObj.elements[0].value; // I _think_ that's correct...
 // I'm going to assume you're writing for MyBB, and using the moofx library
 new ajax("xmlhttp.php?action=my_custom_action", {method: "post", postBody: requestString, onComplete: function(request) { doAjaxComplete(request); }});
}

function doAjaxComplete(response)
{
 // handle stuff when AJAX request is complete here
}
//-->
</script>


PHP Code:
//...
if($mybb->input['action'] == 'my_custom_action') execute_my_custom_function();
//...

No, I am not coding for MyBB. Afaik I am not using any libraris, I am just codig straight into Notepad++...

Thanks for your help though.
^ Just use the moo.fx library - it's small and portable.

If you don't want to, you'll have to interface with xmlhttp request object yourself, and also handle various browser oddities...

Up to you.
I'll look into it on Sunday. My grandparent just bought a new yacht. So we're going out to the reef for a few days Smile
Reference URL's