A summary of the method of transferring variables between PHP and JavaScript

Source: Internet
Author: User
Tags html form http request php and php code strlen

Method One

Code to pass a variable
in PHP, we can
using Variables in JavaScript

The code is as follows Copy Code
$code = ' <script> ';
$code. = '/* <! [cdata[*];
$code. = ' var global = {';
$code. = ' Url:http://www.111cn.net, ';
$code. = ' Name: Xiangzi ';
$code. = '}; ';
$code. = '/*]]> * *;
$code. = ' </script> ';

Build out and then show
Echo $code;

Using variables is simple

The code is as follows Copy Code

var myurl = Global. URL;
var myname = Global.name;

PHP variable to Javascript variable:

The code is as follows Copy Code


<?php $myvar = 10; ?>
<script type= "Text/javascript" >
Jsvar = <?php echo $myvar;?>;
document.write (Jsvar); Test to the If its prints 10:
</script>


Form variable to Javascript variable:

The code is as follows Copy Code


<form name= "MYFORM4" >
<input type= "hidden" name= "Formvar" value= ">"
</form>
<script type= "Text/javascript" >
Jsvar = Document.myform4.formvar.value;
document.write (Jsvar)//test
</script>


PHP variable to Form variable:

The code is as follows Copy Code


<form name= "MYFORM4" >
<input type= "hidden" name= "Formvar" value= "<?php" $phpvar = 10; echo $phpvar?> ">//PHP code inside html!!
</form>


Javascript variable to Form variable:

The code is as follows Copy Code


<form name= "MYFORM3" >
<!--It Needn ' t be a ' hidden ' type, but anything from radio buttons to check boxes-->
<input type= "hidden" name= "Formvar" "value=" ">
</form>

<script type= "Text/javascript" >
jsvar=10;
Document.myform3.formvar.value = Jsvar;
</script>


Ajax and PHP pass values

This is an HTML form. It contains a simple HTML form and a link to JavaScript:

The code is as follows Copy Code

<script src= "Clienthint.js" ></script>

<body>

<form>
The Name:
<input type= "text" id= "Txt1"
Onkeyup= "Showhint (this.value)" >
</form>

<p>suggestions: <span id= "Txthint" ></span></p>

</body>


Javascript

The JavaScript code is stored in the "clienthint.js" file, which is linked to an HTML document:

The code is as follows Copy Code

var xmlHttp

function Showhint (str)
{
if (str.length==0)
{
document.getElementById ("Txthint"). Innerhtml= ""
Return
}
Xmlhttp=getxmlhttpobject ()
if (xmlhttp==null)
{
Alert ("Browser does not support HTTP Request")
Return
}
var url= "gethint.php"
url=url+ "q=" +str
url=url+ "&sid=" +math.random ()
Xmlhttp.onreadystatechange=statechanged
Xmlhttp.open ("Get", url,true)
Xmlhttp.send (NULL)
}

function statechanged ()
{
if (xmlhttp.readystate==4 | | xmlhttp.readystate== "complete")
{
document.getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext
}
}

function Getxmlhttpobject ()
{
var xmlhttp=null;
Try
{
Firefox, Opera 8.0+, Safari
Xmlhttp=new XMLHttpRequest ();
}
catch (E)
{
Internet Explorer
Try
{
Xmlhttp=new ActiveXObject ("msxml2.xmlhttp");
}
catch (E)
{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

The code in "gethint.php" checks the array of names and then returns the corresponding name to the client:

The code is as follows Copy Code

<?php
Fill up array with names
$a []= "Anna";
$a []= "Brittany";
$a []= "Cinderella";

$q =$_get["Q"];


if (strlen ($q) > 0)
{
$hint = "";
for ($i =0; $i <count ($a); $i + +)
{
if (Strtolower ($q) ==strtolower (substr ($a [$i],0,strlen ($q)))
{
if ($hint = = "")
{
$hint = $a [$i];
}
Else
{
$hint = $hint. ",". $a [$i];
}
}
}
}


if ($hint = = "")
{
$response = "no suggestion";
}
Else
{
$response = $hint;
}

Echo $response;
?>

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.