2

Hi I would like to transfer my javascript variable to php, it is just like a real time transferring. The user input, which is entered in a text box, will be transferred to php script to check if it is in the database. Then, the output will be displayed in another textbox that is readonly.

iTech
  • 18,192
  • 4
  • 57
  • 80
lebronJames9
  • 33
  • 1
  • 5

2 Answers2

2

You will have to use AJAX for this. I hope it would be better if you can understand the difference between client-side(JavaScript) and server-side(PHP) script. 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

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243
  • – lebronJames9 Feb 12 '13 at 02:45
1

You'll need to look into 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":

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

In your PHP file "search.php":

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

If you run the javascript on the textbox's keyup event, it'll get data live. So if you typed: "Hello World" into the box, you'd get a stream of annoying messageboxes saying:

"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. For that look into JSON. From there, it's a matter of writing Javascript to do something with the new, useful information.

http://jquery.com/

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

http://www.json.org/

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

turiyag
  • 2,757
  • 2
  • 22
  • 21