Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

javascript - Scraping code not working in php to form controls

I am trying to scrape webpage content to my form controls, Without function i am getting output, but when i created function and assigned to button click event to display the output in textbox its not working, let me know where i am messed.

<script type="text/javascript">
function Assign()
{
$html = file_get_contents("http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812");
preg_match_all('(<li.*?>.*?</li>)', $html, $matches);
$one=$matches[0][0];
document.getElementById("OutputField").value = $one;
}
</script>
<input id="OutputField" type="text" style="width:200px"/>
<input type="button" value="Assign Value" onclick="Assign()"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are mixing two different execution paradigms here:

PHP is executed server-side. That means that the server, hosting your code executes it. As a consequence PHP code is usually not visible to clients. The execution of the php code is triggered by a clients request, and the output of the script (e.g. produced by the printf-function) is passed to the client.

Javascript however, is executed client-side. This implies that the cpu of the website visitor is actually used to execute the code. The code is 100% visible to the client. So what you paste within an HTML documents script-tags is executed client side. It is seen by the client. Contrary, PHP code is not meant to be directly pasted within script tags.

These are very generalized statements, that do always hold. However, they should give you a rough sketch. There is a quite nice answer in this thread that should help you for a better distinction. You should certainly learn these basics, before continuing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...