Forums / Cotonti / Extensions / AJAX parts in plugs

urlkiller
#1 2010-04-21 23:36
Hi,

i got the following Problem with the Ajax call here:
		$(document).ready(function(){
			timestamp = 0;
/*			updateMsg();
*/			$("form#myform").submit(function(){
				$.post("plug.php?r=dashboard",{
							message: $("#sendmessage").val(),
							name: $("#gid").val(),
							action: "postmsg",
							x: $("#x").val(),
							time: timestamp
						}, function(xml) {
								addMessages(xml);
								$('#sendmessage').val('');
				});
				return false;
			});
		});

when submiting the form i got a complete browser freeze. i circled the problem around the security parameter var X everytime i want to use this i got the complete crash.

anybody know where the problem is?
URL shortener: <a href="http://bbm.li/!7AD5C7">http://bbm.li/!7AD5C7</a>
ez
#2 2010-04-22 02:50
A browser freeze probably means that you are sending your data synch. and not a-synch.
So the browser will wait for an answer (explaining the freeze up).

Maybe your xml anser never comes back? (so a timeout will occur)

What is x?
Is x also located as a field within your form or maybe in the cookies? If it is, then you do not have to send it.. maybe this gives a clash..

TIP:
You really shoud look at the jquery forms plugin..
That plugin is great and it works easy!

So a lot off questions and possibilities..
==- I say: Keep it EZ -==
Trustmaster
#3 2010-04-22 16:54
Well, #x is not a correct selector for that element, to get an input by name better use
$("input[name=x]").val()
May the Source be with you!
GHengeveld
#4 2010-04-23 05:18
I noticed that it's not possible to do a POST request to plug.php?r=abc because that will send the $r along with the POST vars, not GET.

I've fixed this in changeset 1213.
With these changes you can make POST request using the ajax hook.

Note that for POST, sending the sourcekey along with the request is required. You can easily put the key in your JS with the {PHP.sys.xk} tag (in tpl files), so you'd get something like this:

$.post('plug.php?r=abc&x={PHP.sys.xk}', 
{ "varname": "value" }, 
function(){ 
// do whatever 
});

Added 5 minutes later:

Also I really recommend testing your jQuery POST requests with RESTTest. It's easy to check the server's response to your POST request that way. (don't forget the sourcekey)
This post was edited by Koradhil (2010-04-23 05:29, 14 years ago)
urlkiller
#5 2010-04-23 10:58
yeah thanks for the tipps

iam using firebug as debugger woirking smooth.

my problem was the x i was sending it 2 times... ;( one in a hidden input (as default) and one as jquery var. i fixed that now...

@trustmaster

as i understand it the $("input[name=x]").val() is deprecated since 1.1.4 or so... it should be: $("input[@name=x]").val()

;)

{PHP.sys.xk} this is really helpful...

Added 7 minutes later:

btw i solved it more like the tut was:
but i did the changes to plug.php and the other file and i still get kicked onto the error page in the background (i see the updates after my POST request i get message.php?msg=950 ! BUT get not redirected and stay on the form part of my testsite). seems like some sort of wierd problem...
		$t->assign('MY_JS_FUNC', "$(document).ready(function(){
			timestamp = 0;
			updateMsg();
			$(\"form#myform\").submit(function(){
				$.post(\"plug.php?r=dashboard\",{
							message: $(\"#sendmessage\").val(),
							group_id: $(\"#group_id\").val(),
							user_id: $(\"#user_id\").val(),
							x: \"".sed_sourcekey()."\",
							user_name: $(\"#user_name\").val(),
							user_avatar: $(\"#user_avatar\").val(),
							action: \"send\",
							time: timestamp
						}, function(xml) {
								addMessages(xml);
								$('#sendmessage').val('');
				});
				return false;
			});
		});");

Added 41 seconds later:

above the highlighter bar seems broken when mixing code and the highlight tag ;)
URL shortener: <a href="http://bbm.li/!7AD5C7">http://bbm.li/!7AD5C7</a>
This post was edited by urlkiller (2010-04-23 11:08, 14 years ago)
Trustmaster
#6 2010-04-23 14:20
# urlkiller : @trustmaster

as i understand it the $("input[name=x]").val() is deprecated since 1.1.4 or so... it should be: $("input[@name=x]").val()
Vice versa.
http://api.jquery.com/category/selectors/
May the Source be with you!
urlkiller
#7 2010-04-24 00:36
ok maybe so ;)

i got now the following problem when inserting html markup in my xml document it dont get parsed correctly.

seems like the .text() from juery is filtering out the whole markup like <h1> and such...

any ideas why it is breaking there?

Added 11 minutes later:

got it already was the cdata thing i was missing for the markup problem:
<![CDATA[".$msgrow['message']."]]>
$msgrow['message'] = sed_parse($msgrow['message']);
$msgrow['date'] = date('d.m.Y H:i:s',$msgrow['date']);
$msgrow['username'] = $msgrow['username'];
		
$sendToOut .= "\t<message>\n";
$sendToOut .= "\t\t<username>".$msgrow['username']."</username>\n";
$sendToOut .= "\t\t<userid>".$msgrow['userid']."</userid>\n";
$sendToOut .= "\t\t<date>".$msgrow['date']."</date>\n";
$sendToOut .= "\t\t<avatar>".$msgrow['avatar']."</avatar>\n";
$sendToOut .= "\t\t<messagebody><![CDATA[".$msgrow['message']."]]></messagebody>\n";
$sendToOut .= "\t</message>\n";

Added 16 minutes later:

hmm somehow it wont send the data when inserting new data over post.
i alwyays get a 930 back and get redirected ... ;( dont find the problem...

Added 3 minutes later:

ok to conclude this:

		$t->assign('MY_JS_FUNC', "$(document).ready(function(){
			timestamp = 0;
			updateMsg();
			$(\"form#myform\").submit(function(){
				$.post(\"plug.php?r=dashboard\",{
							message: $(\"#sendmessage\").val(),
							group_id: \"".$gid."\",
							user_id: \"".$usr['id']."\",
							x: \"".sed_sourcekey()."\",
							user_name: \"".$urow['user_name']."\",
							user_avatar: \"".$urow['user_avatar']."\",
							action: \"send\",
							time: timestamp
						}, function(xml) {
								addMessages(xml);
								$('#sendmessage').val('');
				});
				return false;
			});
		});");

											<form id="myform">
										 		<textarea class="minieditor" name="sendmessage" id="sendmessage"></textarea>
								 				<input type="submit" id="submiter" value="Absenden" style="margin-left:3px;padding:5px;font-size:12px;width:437px;">
								 			</form>

defined('SED_CODE') or die('Wrong URL');
// send header
sed_sendheaders();

$time 					= ($_SERVER['REQUEST_METHOD'] != 'GET') ? sed_import('time','P','INT') : sed_import('time','G','INT');
$action 				= ($_SERVER['REQUEST_METHOD'] != 'GET') ? sed_import('action','P','TXT') : sed_import('action','G','TXT');
$user_name 			= sed_import('user_name','P','TXT');
$user_avatar 		= sed_import('user_avatar','P','TXT');
$date_now 			= sed_import('time','P','TXT');
$user_id 				= sed_import('user_id','P','TXT');
$group_id 			= sed_import('group_id','P','TXT');
$message 			  = sed_import('message','P','TXT');

if($action == "send")
{
	$insert = sed_sql_query("INSERT INTO whiteBox_wave (`username`,`message`,`date`,`avatar`,`userid`,`groupid`) VALUES ('".$user_name."','".$message."','".$date_now."','".$user_avatar."','".$user_id."','".$group_id."')");
}

$msg = sed_sql_query("SELECT * FROM whiteBox_wave WHERE date>'".$time."' ORDER BY date ASC ");

if(sed_sql_numrows($msg) == 0) $status_code = 2;
else $status_code = 1;

$sendToOut .= "<?xml version=\"1.0\" ?>\n";
$sendToOut .= "<response>\n";
$sendToOut .= "\t<status>".$status_code."</status>\n";
$sendToOut .= "\t<time>".mktime()."</time>\n";
if($status_code == 1)
{
	while($msgrow = sed_sql_fetcharray($msg))
	{

		$msgrow['message'] 		= sed_parse($msgrow['message']);
		$msgrow['date'] 			= date('d.m.Y H:i:s',$msgrow['date']);
		$msgrow['username'] 	= sed_build_user($msgrow['userid'],$msgrow['username']);
		
		$sendToOut .= "\t<message>\n";
		$sendToOut .= "\t\t<username><![CDATA[".$msgrow['username']."]]></username>\n";
		$sendToOut .= "\t\t<userid>".$msgrow['userid']."</userid>\n";
		$sendToOut .= "\t\t<date>".$msgrow['date']."</date>\n";
		$sendToOut .= "\t\t<avatar>".$msgrow['avatar']."</avatar>\n";
		$sendToOut .= "\t\t<messagebody><![CDATA[".$msgrow['message']."]]></messagebody>\n";
		$sendToOut .= "\t</message>\n";
	}
}
$sendToOut .= "</response>";


header("Content-type: text/xml");
header("Cache-Control: no-cache");

echo $sendToOut;

ob_end_flush();


Added 45 minutes later:

its quite wierd. because i did yesterday something and today its not working anymore.
i guess i had smething working and deleted or changed it due to testing the parser thing so i think its only a minor problem with my code here..

but i think iam too blind today for seeing it..
URL shortener: <a href="http://bbm.li/!7AD5C7">http://bbm.li/!7AD5C7</a>
This post was edited by urlkiller (2010-04-24 02:06, 14 years ago)
GHengeveld
#8 2010-05-02 17:27
Please note that I've just pushed an update to plug.main.php. My last change required you to send $r through POST. Since that is not backwards compatible I've updated it.

See changeset 1220