php - Insert data into DB with Symfony2 via JavaScript, without a form -


i'm working on symfony2 project user can comment on parts of webpage via contenteditable spans. want save these comments, assembled in array of json objects, database, reference page , user id. i'm stuck.

what steps needed put data javascript code specific db table?

i created comment class in entity folder. , based on this answer added following code in savecomment() in javascript:

$.post('savecomments.php', {     page_id : getpageid(),     user_id : getuserid(),     comments : getjsoncomments(), }); 

what next...?

ok, let's create savecomments.php ... in bundle? or done controller? if so, how , need replace url ("savecomments.php") in $.post(...) call with?

don't post php page breaks symfony 2 model. instead post route, save/comment or along these lines.

configure route point action in controller.

implement controller , action not take parameters. inside action unpack posted data usual php way using (because won't have form bind data to):

$_post 

just working, echo var_dump() console , see get. example on how write console.

decode json data serializer.

the decoded data simple associative php array. interpret data received , act accordingly (don't forget handle security , stuff, -- don't want open security holes through ajax).

best is, check route chose falls security tier need , have configured in symfony 2 application. way don't need handle security manually in action.

once done return http response code 200 ok.

return new response('', response::http_ok); // no response text @ 

if there's error reply 500 class http server error:

return new response('', response::http_internal_server_error); // 500 

also, don't forget handle client-side errors .error() method.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -