0

On my page I have one textbox and one button.
-- on the button click event.
-- one function will be called.
Now,
How to get that value of textbox and store in PHP variable? This should be done in that function which we will call on button click.

2
  • What does your code look like so far? Commented May 4, 2010 at 10:21
  • 3
    Theres no direct passing from javascript to PHP - as PHP is serverside and runs before the javascript (of which is clientside) You'll probably want to POST the information to the page itself using a form Commented May 4, 2010 at 10:22

2 Answers 2

1

You'll have to know that Javascript and PHP run on two different servers. So they can not communicate directly.

You'll basicly have two options:

POST the value

Make a form with a post action, submit the form, and you can access the value in PHP with the $_POST array

Disadvantage is that the browser leaves the page to submit the form, but it doesn't require javascript.

Use AJAX

Get the value in Javascript, make an XMLHTTPRequest to the server, and you can access the variable too via $_GET or $_POST, depending on how you sent the value.

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

Comments

0

When your page is displayed, your PHP code has already been executed.

What you want to do is to learn AJAX, i.e. JS functions that call external PHP scripts.

1 Comment

The advice given by Glycerine is a good one too: instead of going through the perils of ajax, you may use POST. This will be much uglier, however.

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.