简体   繁体   English

在同一页面上将javascript变量传输到php而不提交

[英]Transfer javascript variable to php on the same page without submitting

Hi I would like to transfer my javascript variable to php, it is just like a real time transferring. 嗨,我想将我的javascript变量传输到php,它就像一个实时传输。 The user input, which is entered in a text box, will be transferred to php script to check if it is in the database. 在文本框中输入的用户输入将被传送到php脚本以检查它是否在数据库中。 Then, the output will be displayed in another textbox that is readonly. 然后,输出将显示在另一个只读的文本框中。

You will have to use AJAX for this. 你必须使用AJAX I hope it would be better if you can understand the difference between client-side(JavaScript) and server-side(PHP) script. 我希望你能理解客户端(JavaScript)和服务器端(PHP)脚本之间的区别会更好。 There are a lot questions have been answered with the similar issues. 有类似的问题已经回答了很多问题。 Please google and you can find them with examples. 请谷歌,您可以通过示例找到它们。 Also refer Client side vs server side basics . 另请参阅客户端与服务器端基础知识

Difference Between Client Side & Server Side Programming 客户端和服务器端编程之间的区别

You'll need to look into AJAX. 你需要研究一下AJAX。 It sounds scary as an acronym, but it's quite easy to use. 它作为首字母缩略词听起来很吓人,但它很容易使用。

In your Javascript (using jQuery, and assuming you have a textbox with the id "searchbox": 在你的Javascript中(使用jQuery,假设你有一个id为“searchbox”的文本框:

$.get("search.php", {"term":$("#searchbox")}, function(data) {
    alert(data);
}

In your PHP file "search.php": 在您的PHP文件“search.php”中:

echo "Yay! " . $_GET['term'] . " Woo!";

If you run the javascript on the textbox's keyup event, it'll get data live. 如果你在文本框的keyup事件上运行javascript,它将获得实时数据。 So if you typed: "Hello World" into the box, you'd get a stream of annoying messageboxes saying: 因此,如果您在框中键入:“Hello World”,您将收到一串恼人的消息框说:

"Yay! H Woo!", "Yay! He Woo!", "Yay! Hel Woo!", "Yay! Hell Woo!"... "Yay! Hello World Woo!" “Yay​​!H Woo!”,“Yay!He Woo!”,“Yay!Hel Woo!”,“Yay!Hell Woo!”......“Yay!Hello World Woo!”

From there it's a matter of making the PHP code return something useful. 从那里开始,让PHP代码返回一些有用的东西。 For that look into JSON. 对于JSON的看法。 From there, it's a matter of writing Javascript to do something with the new, useful information. 从那里开始,需要编写Javascript来处理新的有用信息。

http://jquery.com/ http://jquery.com/

http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.get/

http://www.json.org/ http://www.json.org/

http://en.wikipedia.org/wiki/Ajax_(programming) http://en.wikipedia.org/wiki/Ajax_(programming)

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

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