2015-02-12 83 views
1

我必須通過將信息插入到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>

回答

0

您可以嘗試第一頁上這個

聲明變量

<script type="text/javascript"> 
    var globalarray = []; 
    globalarray.push(response.d); 
    window.localStorage.setItem("globalarray", JSON.stringify(globalarray)); 
</script> 

調用變量第二頁

<script type="text/javascript"> 
     var globalarray = []; 
     var arrLinks =[]; 
     arrLinks = JSON.parse(window.localStorage.getItem("globalarray")); 
     console.log(arrLinks); 
</script> 
+0

感謝您的回答,我根據你的改變,但它不工作,請檢查更新的代碼, – geeks 2015-02-12 05:03:25

+0

嘗試使用(response.d),因爲我更新了我的代碼 – 2015-02-12 05:09:58

+0

什麼是response.d ?? – geeks 2015-02-12 05:10:46

0

。您可以使用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); 
} 

要使用使用sessionStorage.clear();

+0

請檢查更新的代碼,我正在獲取conformation.html上的數據,但是隻有在單擊html elemenet後,請檢查,如何不點擊任何內容html元素。 – geeks 2015-02-12 05:23:09

1

如果這是你真正的服務器端代碼,然後......它完全不安全取出後,sessionStorage的DATAS。您不應該將用戶發佈的變量直接傳遞給您的查詢。

$query2 = "insert into booking(cust_email, cust_mobile, cust_name) values('$mail','$mobile','$name')"; 

至少使用「mysql_real_escape_string」轉義值或使用預處理語句。並且...不再使用mysql,使用mysqli,這與您正在使用的幾乎完全相同,但不會很快棄用。

另外,你是json編碼的字符串,不需要被json編碼,它只是一段文本和無效的json代碼。這可能是爲什麼@SimarjeetSingh Panghlia回答不適合你。

而不是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", */ 

這意味着您應該使用json進行響應,無論查詢是否成功。我還建議使用一個名爲jStorage的簡單jQuery插件,它允許輕鬆獲取/設置對象而無需序列化它們。

相關問題