This article mainly shares with you how PHP realizes front-end and back-end data value transfer through json. It is mainly shared with you in the form of code. I hope it can help you.
Front-end key code (need to download jquery-3.3.1):
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script> <script> $(function(){ $("#btn").click(function(){ var my_data="前端变量"; $.ajax({ url: "translate.php", type: "POST", data:{trans_data:my_data}, error: function(){ alert(''); }, complete:function() { //location.href = "index.php?trans_data=" + my_data; }, success: function(data){//如果调用php成功 var dataObj=eval("("+data+")"); alert(dataObj.name); } }); }); }); </script> ...... <form action="translate.php" method="post"> <input type="text" name="title" value="标题"/> <input id="btn" type="button" value="点击"/> </form>
Backend translate.php code:
<?php header('Content-Type: text/html; charset=UTF-8'); $backValue=$_POST['trans_data']; $info = array('name' =>'张三','sex'=>'男'); if(isset($backValue)){ echo json_encode($info); }
Related recommendations:
Implementing the value transfer function of session and cookie data in PHP
The above is the detailed content of PHP realizes front-end and back-end data value transfer through json. For more information, please follow other related articles on the PHP Chinese website!