[英]How to pass json data from one html page to other html page using javascript?
I have to pass Json data which is generated after inserting the info into datbase table, Now I have to pass this info to other page BookingConformation.html . 我必须传递将信息插入datbase表后生成的Json数据,现在我必须将此信息传递给其他页面BookingConformation.html 。
For example we will get the form server Welcome Mr/Mrs Mittel Thanks for booking home services your booking id is 1. So please tell me how to pass info which is get form server in javascript now I have to pass this info to other page, please help me on this. 例如,我们将获得表格服务器Welcome Mr / Mittel先生/女士感谢您预订上门服务,您的预订ID为1。所以请告诉我如何通过javascript将表格服务器中的信息传递给我,我必须将此信息传递至其他页面,请帮助我。
script 脚本
<script>
$(document).ready(function(){
$("#register-form").validate({
rules: {
userName: "required",
email: {
required: true,
email: true
},
userContactNumber: "required"
},
messages: {
userName: "Please enter your Name",
userContactNumber: "Please enter your Mobile number",
email: "Please enter a valid email address",
},
submitHandler: function(form) {
var uName = $('#userName').val();
var mailId = $('#email').val();
var mobNum = $('#userContactNumber').val();
$.ajax({
url:"http://localhost/bookRoom/book.php",
type:"POST",
dataType:"json",
data:{type:"booking", Name:uName, Email:mailId, Mob_Num:mobNum},
ContentType:"application/json",
success: function(response){
//alert(JSON.stringify(response));
//alert("success");
alert(response);
var globalarray = [];
globalarray.push(response);
window.localStorage.setItem("globalarray", JSON.stringify(globalarray));
window.location.href = 'BookingConformation.html';
},
error: function(err){
window.location.href = 'error.html';
}
});
return false; // block regular submit
}
});
});
</script>
server code 服务器代码
<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","7128");
mysql_select_db("service");
if(isset($_POST['type']))
{
if($_POST['type']=="booking"){
$name = $_POST ['Name'];
$mobile = $_POST ['Mob_Num'];
$mail = $_POST ['Email'];
$query1 = "insert into customer(userName, userContactNumber, email) values('$name','$mobile','$mail')";
$query2 = "insert into booking(cust_email, cust_mobile, cust_name) values('$mail','$mobile','$name')";
$result1=mysql_query($query1);
$result2=mysql_query($query2);
$id=mysql_insert_id();
$value = "Welcome Mr/Mrs ".$name." Thanks for booking home services your booking id is = ".$id;
echo json_encode($value);
}
}
else{
echo "Invalid format";
}
?>
BookingConformation.html BookingConformation.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function myFunction() {
var globalarray = [];
var arrLinks =[];
arrLinks = JSON.parse(window.localStorage.getItem("globalarray"));
document.getElementById("booking").innerHTML = arrLinks;
}
</script>
</head>
<body>
<p id="booking" onclick="myFunction()">Click me to change my HTML content (innerHTML).</p>
</bod
y> Y>
If this is your real server side code then...its completely insecure. 如果这是您真正的服务器端代码,则...完全不安全。 You should never pass variables posted by users directly into your queries. 您永远不应将用户发布的变量直接传递到查询中。
$query2 = "insert into booking(cust_email, cust_mobile, cust_name) values('$mail','$mobile','$name')";
At least escape the values using "mysql_real_escape_string", or use prepared statements. 至少使用“ mysql_real_escape_string”对值进行转义,或使用准备好的语句。 And...dont use mysql anymore, use mysqli, which is almost identical to what you are using, but not deprecated soon. 并且...不再使用mysql,请使用mysqli,它与您所使用的几乎相同,但不会很快被弃用。
Also, you are json encoding a string that doesnt need to be json encoded, its just a piece of text and not valid json code. 另外,您正在json编码一个不需要进行json编码的字符串,它只是一段文本,并且不是有效的json代码。 This may be why @SimarjeetSingh Panghlia answer doesnt work for you. 这就是为什么@SimarjeetSingh Panghlia答案对您不起作用的原因。
instead of json_encoding that value, encode a structured array. 而不是json_encoding该值,而是对结构化数组进行编码。
$response = array( "status" => true );
if(isset($_POST['type']))
{
if($_POST['type']=="booking"){
$name = mysql_real_escape_string( $_POST ['Name'] ));
$mobile = mysql_real_escape_string($_POST ['Mob_Num']);
$mail = mysql_real_escape_string($_POST ['Email']);
$query1 = "insert into customer(userName, userContactNumber, email) values('$name','$mobile','$mail')";
$query2 = "insert into booking(cust_email, cust_mobile, cust_name) values('$mail','$mobile','$name')";
$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
$id = mysql_insert_id();
$response["message"] = "Welcome Mr/Mrs ".$name." Thanks for booking home services your booking id is = ".$id;/* make sure you strip tags etc to prevent xss attack */
}
}
else{
$response["status"] = false;
$response["message"] = "Invalid format";
}
echo json_encode($response);
/* Note that you are making the query using ContentType:"application/json", */
which means you should respond using json regardless if query is successful or not. 这意味着无论查询是否成功,都应使用json进行响应。 I would also recommend using a simple jQuery plugin called jStorage, that allows easy get/set of objects without having to serialize them. 我还建议使用一个名为jStorage的简单jQuery插件,该插件可轻松获取/设置对象而无需序列化它们。
You can try this 你可以试试这个
declare variable on first page 在首页上声明变量
<script type="text/javascript">
var globalarray = [];
globalarray.push(response.d);
window.localStorage.setItem("globalarray", JSON.stringify(globalarray));
</script>
Call that variable on second page 在第二页上调用该变量
<script type="text/javascript">
var globalarray = [];
var arrLinks =[];
arrLinks = JSON.parse(window.localStorage.getItem("globalarray"));
console.log(arrLinks);
</script>
You can use sessionStorage
to store and retrieve JSON Data. 您可以使用sessionStorage
来存储和检索JSON数据。
var complexdata = [1, 2, 3, 4, 5, 6];
// store array data to the session storage
sessionStorage.setItem("list_data_key", JSON.stringify(complexdata));
//Use JSON to retrieve the stored data and convert it
var storedData = sessionStorage.getItem("complexdata");
if (storedData) {
complexdata = JSON.parse(storedData);
}
To remove sessionStorage Datas after using use sessionStorage.clear();
要在使用后删除sessionStorage数据,请使用sessionStorage.clear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.