JS call PHP and PHP to invoke JS Method Example

Source: Internet
Author: User
Tags echo date

http://my.oschina.net/jiangchike/blog/220988

1, JS way to call PHP file and get the value in PHP
Give a simple example to illustrate:
As in the page Test_json1 the following sentence is called:

<script type= "Text/javascript" src= "Http://callmewhy.sinaapp.com/index.php/test/testjson2" >

</script>

<script type= "Text/javascript" >

alert (Jstext);

</script>

There is a PHP code in test_json2.php:

<?php

$php _test= ' I come from php! ';

echo "var test= ' $php _test ';";

echo "var jstext=". "' $php _test '; ";

?>

When the test_json1.php file is executed, the test_json2.php file is called and the output of the b.php file is executed as a JS statement.

So here will be a popup box, the contents of the JS variable jstext value, that is, the PHP file assigned to Jstext value.


Summary: In the HTML with JS call file in the way the PHP file, the output of the PHP file will be called the page as JS code to use.

2, PHP calls the value of JS

There is a piece of code in the test_json3.php page:

<script type= "Text/javascript" >

var data= "call_me_why";

</script>

<?

echo "<script type=text/javascript>document.write (data) </script>";

?>


3, PHP Call JS method (function)
Similar to the second case, using the Echo script to implement the JS call

<script type= "Text/javascript" >

function Test () {

var t1=3;

T1 = t1+2;

alert (t1);

return t1;

}

</script>

<?php echo "<script type= ' Text/javascript ' >test ();</script>";?>



4, JS call PHP variable
(1)

<?php

$userId = 100;

?>

<script>

var userId;

Userid=document.getelementbyid ("UserId"). Value;

alert (USERID);

</script>

<input type= "text" name= "userid" id= "userid" value= "<?php echo $userId;?>" >


(2)

[PHP] view plaincopy

<?php

$url = ' call_me_why '; Defining variables

?>

<script type= "Text/javascript" >

JS Call PHP variable

var ds = "<?php echo $url?>"; Assign value

Alert (DS); Output effect

</script>



5, JS call php function

<script language= "JavaScript" >

var y=<?php echo date (' Y ')? >,m=<?php echo Date (' n ')? >,d=<?php Echo Date (' J ')?>;

alert (Y);

alert (M);

alert (D);

</script>

JS call PHP and PHP to invoke JS Method Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.