By tm0032938 on
I want to save data from Textbox on Button click. I am using JQuery AJAX for this task like below. Please note that I made this tags inside theme function.
function theme_user_post_block($vars)
{
$themeUserCommentInput ='';
$themeUserCommentInput .= '<textarea id="txt_1" rows="1" cols="50"></textarea>';
$themeUserCommentInput .= '<input type="submit" value="Post Comment" align="center"
class="btnPostComment" id="btn_1" />
return $themeUserCommentInput;
}
This able to show me Textbox and Button inside the page. Now here is my JS code:-
(function($)
{
Drupal.behaviors.PostComment= {
attach: function (context, settings) {
$('.btnPostComment', context).click(function (event) {
var post = "&newcomment=Comment1&logid=log1";
jQuery.ajax({
url: 'postcomment',
type: 'POST',
dataType: 'json',
data: post,
success: function (data) { alert(data); },
error: function(jqXHR, textStatus, errorThrown) {alert(textStatus + errorThrown);}
});
});
}
}
})(jQuery);
Next I create a Menu Page with URL Name as follows:-
function postcomment_menu(){
$items=array();
$items['postcomment']=array(
'title'=>t(''),
'type'=> MENU_CALLBACK,
'page callback' => 'user_comment_post',
'access arguments' => array('access content'),
);
return $items;
}
function user_comment_post(){
global $user;
$cid = db_insert('user_comment')
->fields(array(
'comment_user_id' => $user->uid,
'reference_id' => $_POST['logid'],
'comment_desc'=>$_POST['newcomment'],
'createdon'=>REQUEST_TIME,
))
->execute();
if($cid!=0)
{
//GetUserComments($i);
drupal_json_output("success");
}
}
So I have done all things that is required for jQuery+Ajax Submit functionality. When I press
"Post Comment" button it gives me error in alert says "errorundefined". The alert shows as a
result of error inside the jQuery.AJAX function. Also the custom menu callback is also not getting
called.
Comments
From what you provided the
From what you provided the error is coming from
You don't say why you think user_comment_post() is not called but it does have some errors.
1) You are not declaring $user as global.
2) As a result, $user->uid will always be undefined (depending on your PHP settings this may be the source of your error).
3) You do not have a return on error ($cid == 0)
I forget to put global $user
I forget to put global $user here but it is there in actual code.I am sorry for putting some simple code mistakes in other place also like:-
1) var post = "&newcomment=Comment1&logid=log1;
Modified To :-var post = "&newcomment=Comment1&logid=log1";
2) Inside user_comment_post() the line
'comment_desc'=>$_POST['comment'],
Modified To :- 'comment_desc'=>$_POST['newcomment'], //we are passing newcomment from JQuery.Ajax
3) Theme function look like below
I put my debugger in user_comment_post() context which doesn't get hit when running the code.This is the reason why I say that user_comment_post() is not called.
I am not clear about your 3rd point.It would be great if you would elaborate a bit.
Ok, that helps. Have you
Ok, that helps. Have you looked at the form API, it would make this easier.
It may even be required. OP:
It may even be required.
OP: Can you please cost your actual code? Trying to figure out what you actually have by looking at your original code that wasn't in code tags, along your 'fixes' is too much trouble.
Contact me to contract me for D7 -> D10/11 migrations.
Forms API will reduce my
Forms API will reduce my effort as I used it in many cases .I tried Forms API in my scenario but it will not be better approach as my use case contains many different things apart from this. Only JSON/JQuery API will fulfill the situation out here. Please help in rectifying my problem or provide better references for posting the data through Jquery/Json API.I followed this reference http://www.phpgainers.com/content/post-data-using-jquery-json-and-ajax-d... for writing my code. I have modified my code and remove all erroneous things now and put it inside
tag. Please take a look.Here is a better article that
Here is a better article that uses the form API: https://gist.github.com/pascalduez/1517513 and more detail about ajax in the form API: http://drupal.org/node/752056
Thanks for the references.I
Thanks for the references.I used Form API before and already had look at the references posted by you. But my use case will not fit for Forms API as there are other things also involve with this. I have to go for JQuery/JSON output like posted in code above. Please help me in giving suggestion on the code posted.
Here is the answer finally. I
Here is the answer finally. I added one line(async: false) and everything works perfectly.Below is the code.
I dont know about the exact usefulness about the line. I got this from another post. If any one knows about this then please share with us.
_
The first 'a' in AJAX stand for asynchronous-- based on the fact you mentioned some sort of 'undefined' error, you were likely trying to do with something with the results of your ajax before it was actually available. By specifying 'async:false', you are effectively removing the 'a' from ajax and forcing the code to wait for the ajax to complete before proceeding.
Also, in the future I recommend
<?php ?>tags so the code is color coded as well.I'll add one last comment.
I'll add one last comment. I've gone back and looked at your code, and I can see you are creating comments by directly inserting the data into the database. This bypasses many Drupal APIs, including all of the comment APIs, which is only asking for issues to creep up on you in the future, as you start adding additional modules.
I would suggest looking at this thread on creating comments and look at implementing the solutions they have discussed for saving your comment, rather than a direct insertion into the database.
Contact me to contract me for D7 -> D10/11 migrations.