JS calls PHP and PHP calls JS Method Example ____js

Source: Internet
Author: User
Tags echo date

Turn from: http://www.cnblogs.com/kuyuecs/archive/2012/07/18/2597068.html

1 JS way to call PHP file and get the value in PHP

Give a simple example to illustrate:

As in the page a.html, use the following sentence:

<script type= "Text/javascript" src= "B.php?action=test" ></script>

<script type= "Text/javascript" >

alert (Jstext);

</script>

There is a section of PHP code in b.php:

? $action =$_get[' action ']; echo "var jstext= ' $action '"; Output A JS statement, generate a JS variable, and the value of PHP variable $action value//echo "var jstext= ' AA '"; echo "var jstext=". "' $action ' ";?>

When the a.html file is executed, the b.php file is invoked and the output of the b.php file is executed as a JS statement, so a prompt box will pop up here, which is the value of the JS variable jstext, which is the value assigned to Jstext in the PHP file.

Summary:

In HTML to call the file in the format of JS php file, the php file output will be called the page as JS code to use.

2 PHP calls to the value in JS

There is a section of code in the z.php page:

<script type= "Text/javascript" > var url= "aaaa*"; </script>;? $key = "<script type=text/javascript>document.write (URL) </script>"; echo $key;?>

3 PHP calls in JS method (function)

<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.getelementbyidx_x_x_x ("UserId"). Value;
alert (USERID);
</script>
<input type= "text" name= "userId" id= "UserId" value= "<?php echo $userId;?>" >

(2)

<?php
$url = ' URL of change '; Defining variables
?>
<script type= "Text/javascript" >
JS Call PHP variable
var ds = "<?php echo $url?>"; Assignment alert (DS); Output Effect </script>

5-------------------------------

<script language= "JavaScript" > <!--

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

-->

</script>

6 write their own JS and PHP to call each other

1.php content:

<?php

echo "<script language= ' JavaScript ' >alert (' $php variable ');</script>"; The simplest PHP calls JS

echo "<a href=#></a> ";

echo "<a href= ' 3.php ' >aaaa</a>"; Hyperlinks in PHP

echo "<script type= ' text/javascript ' language= ' JavaScript ' >phpmake (' PHP Construction Station Learning notes Web ');</script>"; Sometimes you need to call JavaScript custom functions during PHP execution (error validating)

echo "function ok (msg) {alert (msg);}";

?>

<HTML>

<HEAD>

<TITLE> PHP to call the JS file a good way to </TITLE>

</HEAD>

<BODY>

<!--JS calls the js--> defined in PHP

<scrīpt language= ' javascrīpt ' type= ' text/javascrīpt ' src= ' 1.php ' ></scrīpt>

<scrīpt>

Ok ("aaaaaa!");

</scrīpt>

</script>

</BODY>

</HTML>

2.php content:

<!--JS Call php-->

<?php

$userId = 100;

?>

<script>

var userId;

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

alert (USERID);

</script>

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

<!--JS Call php-->

<?php

if ($_get["action"]== "OK")

{

echo "I ' m ok!";

}

Else

{

echo "I ' m not ok!";

}

?>

<script Language = "JavaScript" >

function func ()

{

if (Confirm ("Are OK with this?")

{

This.location = "Ok.php?action=ok";

}

Else

{

This.location = "Ok.php?action=cancel";

}

}

</SCRIPT>

<body>

<a href= "#" href= "#" onclick= "Javascript:func ();" >please click</a>

</body>

<!--JS Call php-->

<script>

function IsMail (poststring)

{

re=/\w*/

if (Re.test (poststring))

{

return true;

}

Else

{

return false;

}

}

function Test () {

if (IsMail (<?php echo $email?>))

{document.write ("<?php echo" N ";?) > ");}

Else

{document.write (' <?php echo ' Y ';? > ');

}

</script>

<body>

<?php

$email = "AA";

?>

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.