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.

1
  • you can't do this. javascript is client side, php is server side...you're basically looking for AJAX. Commented Feb 11, 2013 at 7:39

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

<script> jQuery.validator.addMethod("ordrNum",function(value,element) var isExist=false; $.ajax({ type : "POST", async: false, data : "action=checkOrdrNum&id=0&orderno=" + value, url : ajaxurl, dataType:"json", success: function (data) { //console.log(data["order_no"]); if (data["order_no"] >0) { isExist=true; } } }); //console.log(isExist); return isExist;//true; },"This order number does not exists"); </script> <?php echo $_POST['orderno']; ?>
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)

2 Comments

can i put the code: "$.get("search.php", {"term":$("#searchbox")}, function(data) { alert(data); }" to search.php?
and how to use keyup using the given sample you provide

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.