简体   繁体   English

从外部php文件返回值以在jQuery中使用

[英]Returning a value from external php file for use in jQuery

So I have an input of type text that the user can enter text into, and when they post the text I'm using jQuery's prepend method to post it onto the screen, but before I let the text publish, I want it to go through some validation in an external PHP file, but I'm not having much luck on achieving this. 所以我有一个文本类型的输入,用户可以输入文本,当他们发布文本时,我正在使用jQuery的prepend方法将其发布到屏幕上,但是在我让文本发布之前,我希望它能够通过在外部PHP文件中进行了一些验证,但是我在实现这一目标上并不走运。

jQuery is sending a POST to the php file and I have a variable with the sanitized text in the php file that's ready to go - I'm just not sure how to return the value (preferably in a good way) back to jQuery. jQuery将POST发送到php文件,并且在php文件中有一个带有清理文本的变量可以使用了-我只是不确定如何将值(最好以一种好的方式)返回给jQuery。

Here is the POST: 这是POST:

$.post('ajaxPost.php', {'message' : msg}, function(){

    $('.messages').prepend(msg + '<br>');

});

Thanks for any help! 谢谢你的帮助!

You can do that in this way: 您可以通过以下方式进行操作:

$.post('ajaxPost.php', {'message' : msg}, function(data){
    $('.messages').prepend(data + '<br>');
});

And in your ajaxPost.php you have to return your text you want to display. 在ajaxPost.php中,您必须返回要显示的文本。 You can just simply echo it like echo $sanitized_message; 您可以像echo $ sanitized_message;一样简单地进行回显 .

you can echo the resposne in your external php script and use get instead of post 您可以在外部php脚本中回显resposne,并使用get而不是post

$.get('ajaxPost.php?message='+msg, function( data ) {
   $( ".someDiv" ).html( data );
   alert( "Load was performed." );
});

you might need to url_encode the message before 您可能需要先对消息进行url_encode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM