我必須通過將信息插入到datbase表後生成的Json數據,現在我必須將此信息傳遞給其他頁面BookingConformation.html。如何將json數據從一個html頁面傳遞到使用javascript的其他html頁面?
例如,我們會得到表格服務器歡迎先生/女士密特爾感謝預約上門服務預訂ID爲1所以,請告訴我如何傳遞信息是可以在javascript形式的服務器現在我已經通過這個信息到其他頁面,請幫助我。
腳本
<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>
服務器代碼
<?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
<!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>
感謝您的回答,我根據你的改變,但它不工作,請檢查更新的代碼, – geeks 2015-02-12 05:03:25
嘗試使用(response.d),因爲我更新了我的代碼 – 2015-02-12 05:09:58
什麼是response.d ?? – geeks 2015-02-12 05:10:46