开发者

passing values from java script to php

开发者 https://www.devze.com 2023-01-27 16:46 出处:网络
can any one please help how to get the values from the javascript to php other than while using submit button.

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 s开发者_JAVA技巧ay he selected continue


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


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消