2013-03-17 239 views
0

我一直在玩bookmarklets,並在iframe中創建一個包裝頁面,然後放置一個表單,這樣我就可以將數據提交到我的服務器,但是我無法從頁面獲取PHP變量。從iframe獲取php變量

示例代碼(已編譯的到書籤:http://moxleystratton.com/javascript/bookmarklet-compiler):

javascript:void((function(){ 
var a=document; 
a.write(
     '<!DOCTYPE html> 
     <html> 
      <head> 
      <meta charset="UTF-8"> 
      <title>'+a.title+' - Edit Mode </title> 
     </head> 
     <body> 
     <section> 
     <div id="wrapper"> 
     <iframe src="'+a.URL+'" > 
     </iframe> 
     </div> 
     </section> 
     <footer> 
      <form action="/sys/manage/seo.php" method="post"> 
    <input type="hidden" value=rawData > 
    <label for="title">Title</label> 
    <input type="text" name="title" value="<?= $title;?>"> 
    <label for="title">Meta-Description</label>  
    <input type="text" name="meta-desc" value="<?= $data->meta-desc;?>"> 
    <label for="title">Meta-Keywords</label>   
    <input type="text" name="meta-key" value="<?= $data->meta-key;?>"> 
</form> 
     </footer> 
     </body> 
     </html>')})()); 
+0

這是你真正的代碼?這有問題在所有運行.. – 2013-03-17 10:34:15

+0

你不能寫這樣的JS看到[這裏](http://stackoverflow.com/a/805113/1145578) – soyuka 2013-03-17 10:35:34

+0

它的我真正的代碼,它需要被編譯成書籤工作(也使用它的頁面將需要適當的PHP變量) – 2013-03-17 10:36:30

回答

2

首先解決您的JS是這樣的:

void((function(){ 
var a=document; 
a.write(
     '<!DOCTYPE html>\ 
     <html>\ 
    <head>\ 
      <meta charset="UTF-8\ 
      <title>'+a.title+' - Edit Mode </title>\ 
     </head>\ 
     <body>\ 
     <section>\ 
     <div id="wrapper">\ 
     <iframe src="'+a.URL+'" >\ 
     </iframe>\ 
     </div>\ 
     </section>\ 
     <footer>\ 
      <form action="/sys/manage/seo.php" method="post">\ 
    <input type="hidden" value=rawData >\ 
    <label for="title">Title</label>\ 
    <input type="text" name="title" value="<?= $title;?>">\ 
    <label for="title">Meta-Description</label>  \ 
    <input type="text" name="meta-desc" value="<?= $data->meta-desc;?>">\ 
    <label for="title">Meta-Keywords</label>  \ 
    <input type="text" name="meta-key" value="<?= $data->meta-key;?>">\ 
</form>\ 
     </footer>\ 
     </body>\ 
     </html>')})()); 

演示在這裏:http://jsfiddle.net/shahverdy/Z5EBk/