kumar1425
kumar1425

Reputation: 45

passing values from java script to php

can any one please help how to get the values from the javascript to php other than while using submit button.

scenario: i am searching for record, if the record found then, i need confrim alert asking to continue or not, if he click continue how can i say he selected continue

Upvotes: 0

Views: 285

Answers (3)

Andy
Andy

Reputation: 3021

You can use this as an example using jquery and PHP:

$.post('searchimage.php', { action: 'searchimage', imgreference: $(this).val() },
            function(data) {imgsample.html(data);}
);

Basically apply the above function in a document ready function so its run when the page loads.

This can be triggered using $("#prodcode").click() or what ever event handler you want to use.

The php page in my example will get sent the value from imgreference as a post, you can do whatever you want in the php page then return the value which gets added to the imgsample (in this case a td)

hope this helps.

Upvotes: 0

Teekin
Teekin

Reputation: 13259

You can never use JavaScript to communicate with the page while it is loading, you can only send a new request to the web server from the JavaScript layer... although you can send that request to the same script that's already running, it will still be a new instance of the PHP script, just like when you open a new browser tab to the same page.

The only way for JavaScript to communicate with PHP at all, is by sending an HTTP request. But you don't have to refresh the page in order to do that if you use AJAX.

AJAX is basically a word to describe JavaScript exchanging information with web pages without refreshing the page. But note that it will still not be able to change variables in the PHP script which is running when the JavaScript code is executed.

In the case of PHP, I've used the open-source library SAJAX which is quite simple. You will find it at http://www.modernmethod.com/sajax/

Hope it helps and good luck!

Upvotes: 0

SW4
SW4

Reputation: 71140

If you want to check without having a page reload, you probably want to execute an AJAX call, then depending on the result returned by the underlying PHP script, take the appropriate action. If you have no knowldege of how to implement this, take a look here

Upvotes: 1

Related Questions