This article is recently done Wamp Web site learning knowledge, this site in the process of the need through the news of the hyperlinks to display the corresponding specific content, so it involves some related variable assignment of content, including: HTML hyperlink passing value through JavaScript display, JavaScript variables are translated into PHP variables, hyperlink implementations are passed to the PHP Connection database (emphasis), PHP output JavaScript content.
I. HTML hyperlink pass-through value
The first thing to do is to implement the jump via HTML hyperlink <a href=></a>, and then get the passed value through the JavaScript implementation. The code is as follows: where testa.php is a hyperlink, testb.php is the passed value obtained.
The testb.php code is as follows:
The values are then displayed in testb.php via javascript:
var src = document.getElementById ("new_id"). Value = Location.href;
<script>document.write (SRC);</script>
two. JavaScript variables convert PHP values
At this point we have obtained var idparams[1] = 10001, but the effect I want to achieve is through the ID database primary key to get the contents of the database, and the part of the database code is as follows:
$result = $hgi->doquery (' 2 ', "SELECT * from course_news where cn_nid= '". $id. "';");
So you just need to assign $id to the Var variable of JavaScript. References Mingfish2 Post "How to assign a JS variable to php?"
because PHP or Ajax learning less, so I did not implement, only reference other people's methods and ideas. The following methods are commonly used:
1. assigning JS variables to PHP via AJAX (reference: http://www.xgllseo.com/?p=3241)
JS section code $ (' button '). Click (function () {var js2_101=30;$.ajax ({type: ' GET ', url: "3.php", Data:{text:js2_101},success: function (data) {alert (data)}}); return false;}); PHP section code $m2_10=$_get[' text ']; $php 2=10;echo $m 2_10+ $php 2. " <---js assignment to PHP, can be programmed in PHP ";
What I'd like to share here is some of the fundamentals of it:
JS variable assignment to PHP, you need to know that JS is the client language can be run directly on the browser, PHP is the server language running in the background, so the JS variable can not be directly assigned to PHP. So how do you know what a server language is and what is a client language? In fact, to view the source code of the page we will find that we can directly view the source of JS and PHP cannot view the sources can only output the results to the page. To achieve this, use Ajax, define a JS variable, assign the variable to data, and then assign the data parameter to the PHP variable.
andPHP can assign a value to JS because PHP is parsed on the server side. The server side will not parse the JS code, JS code and HTML code on the server side are output. PHP will parse and execute only if it has a location labeled PHP to recognize. And JS is to the client before being resolved by the client's browser.
If you need to "assign" the JS variable to PHP, this involves interaction. is the so-called commit,because whenever you see the page in the browser, whether it is php,asp,jsp, and so on, are HTML documents, local needs to modify the server-side data, of course, to interact with the server. And the middleman that interacts is the bridge that js,js the client and the server interaction.
2. Get the value from the form get or POST method
(Reference: http://jingyan.baidu.com/article/fd8044faf3cda05030137a47.html)
The data that the client wants to pass through JavaScript is first handed to a hidden label in the form, and then in the form of a submit form, the data is pphp through the Post method.
1) Mainfunc.js Code: function func () {var a = "javascript variable";d Ocument.getelementbyid ("test"). Value = A;} 2) test.php code. <?php if (isset ($_post[' Sub '))) {$test = $_post[' Test '];echo ' <br> '. $test. ' ----The PHP variable to display ";}? ><form action= "" method= "post" ><input type= "hidden" id= "test" name= "test"/><input type= "Submit" id= "Sub" value= "Variable Conversion"/></form>
three. Hyperlink implementation passed to PHP connection database
In fact, the above "one or two" is about how to implement the process is as follows:
html=> Hyperlink =>javascript=>php=> Database
Since Ajax was not implemented, I later found an easier way to do this is to get the parameters directly when the hyperlink was set, and not to use JavaScript to manipulate the string to get the corresponding value. The code is as follows:
//testa.php Setting up hyperlinks
<a href= "main_right_yk4-1-content.php? Newid=<?php echo $id;?> "> Hyperlinks </A>
Get database contents in//testb.php
$hgi =new httppostinf ();
$result = $hgi->doquery (' 2 ', "SELECT * from Course_news wherecn_nid= ' ". $_get[' newid ']." ';");
//Parse JSON
$dj =json_decode ($result, true);
Later in this article I'll tell you about PHP and the database, where you just need to know how to get a connection. As the project is completed by me and another classmate, he put the background database operations with PHP class package long (using Sina SAE), so I call very convenient front-end (using Apache). The initial effect that this section can achieve is as shown in the following:
Four. PHP output JavaScript content
This section is simple, put PHP variables in the HTML hidden type of tags, and then javascript through the document.getElementById (""). Value to get the value of the tag, you can get the value of PHP variable. You can also direct the assignment of PHP and JS variables. the code is as follows:
<script> //php variable can be assigned to JS programming operation <?php $m =10;?> //php variable $mvar js =; JS variable Jsvar n=<?php echo $m;?>; PHP value assignment to JS variable ndocument.write (js+n); Result: PHP value can be assigned to JS variable, and can be used in the JS program </script>
The result of the code output is 30. Another type of code is as follows:
<?php $test = "It ' s a test!" ;? ><input type= "hidden" id= "test" name= "test" value= "<?= $test;? > " /><script> var test_js = document.getElementById (" Test "). value;</script><?php //Other processing?>
Finally hope that the article is helpful to everyone, next want to write wamp about database knowledge (window+apache+mysql+php).
(By:eastmount 2015-2-20 4 o'clock in the afternoon http://blog.csdn.net/eastmount/)
Assignment and transfer between php+javascript+html variables