2016-07-23 111 views
1

我試圖將表單中的變量發佈到表單本身。表單裏面有一個iframe,它指向一個php文件(它也包含我需要發佈的元素)。iframe中的PHP POST變量

下面是代碼的摘錄:

//clerkingpatient.php 
    <?php 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 

     $con = @pg_connect("host=localhost dbname=datamed user=admin password=admin "); 

     $name = test_input($_POST["name"]); 
     $patient_no = test_input($_POST["patientNo"]); 
     $complains = test_input($_POST["complains"]); 
    } 
    ?> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" target="myframe" onsubmit="return validateForm()" > 
     <table cellpadding="5"> 
<tr> <td><input type="text" name="name" value=""/></td> </tr> 
<tr> <td><input type="text" name="patientNo" value=""/></td> </tr> 
... 
... 
<tr><td> 
      <iframe name="myframe" src="ttabcontrol.php" width="1160px" height="200px" frameborder="0"></iframe> 
       </td> </tr> 
<tr><td width="200px"><button type="submit" value="save" style="height: 35px">Save Data</button></td></tr> 
     </table> 
    </form> 

的抱怨文本字段是ttabcontrol.php

 //ttabcontrol.php 
     <input type="text" name="complains" value="complains"/> 

在提交表單我得到這個錯誤「通知:未定義指數:抱怨在C:\ wamp \ www \ bossmed \ clerking \ clerkingpatient.php在線32「 我做錯了,因爲我想使用iframe中的元素。

+0

請告訴我iframe的意義呢? –

+0

您可能需要使用javascript來提交表單,但將操作設置爲iframe而不是將表單提交回同一頁面 – RamRaider

+0

您只需將表單的'action'設置爲'ttabcontrol.php'。不是'PHP_SELF'。另外,你無法通過iframe分割表單。 –

回答

0

作爲您如何在不重新加載頁面的情況下將數據發佈到iframe的示例,也許您可​​以使用以下簡短示例。

<!doctype html> 
<html> 
    <head> 
     <title>Post to an iFrame using Ajax</title> 
     <script type='text/javascript' charset='utf-8'> 

      function ajax(m,u,p,c){ 
       /* 
        m=method 
        u=url 
        p=params {name:value} 
        c=callback 
       */ 
       var xhr=new XMLHttpRequest(); 
       xhr.onreadystatechange=function(){ 
        if(xhr.readyState==4 && xhr.status==200)c.call(this,xhr.response); 
       }; 

       var params=[]; 
       for(var n in p)params.push(n+'='+p[n]); 

       switch(m.toLowerCase()){ 
        case 'post': p=params.join('&'); break; 
        case 'get': u+='?'+params.join('&'); p=null; break; 
       } 

       xhr.open(m.toUpperCase(), u, true); 
       xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
       xhr.send(p); 
      } 


      function initialise(){ 
       var bttn=document.querySelectorAll('input[type="button"]')[0]; 
        bttn.onclick=function(event){ 
         var params={}; 
         var fd=new FormData(document.forms['myform']); 
         for(var key of fd.keys()) params[key]=fd.get(key); 
         ajax.call(this, 'post', document.querySelectorAll('iframe[name="ifr"]')[0].src, params, cbfd); 
        }; 
      } 

      function cbfd(r){ 
       var iframe=document.querySelectorAll('iframe[name="ifr"]')[0].contentWindow.document; 
       iframe.body.innerHTML=r; 
      } 


      document.addEventListener('DOMContentLoaded',initialise,false); 
     </script> 
     <style type='text/css' charset='utf-8'> 
      input[type='text']{ 
       margin:1rem; 
      } 
     </style> 
    </head> 
    <body> 
     <form name='myform' method='post'> 
      <input type='hidden' name='field_1' value='value 1' /> 
      <input type='hidden' name='field_2' value='value 2' /> 
      <input type='hidden' name='field_3' value='value 3' /> 

      <input type='text' name='comment' placeholder='eg: we love javascript' /> 
      <input type='text' name='username' placeholder='eg: fred' /> 

      <input type='button' value='Post' /> 
     </form> 
     <iframe name='ifr' src='iframesrc.php' width=600 height=400></iframe> 
    </body> 
</html> 

而且,在這個例子中,iframe是一個簡單的PHP頁面是這樣的:

<?php 
    /* iframesrc.php */ 
    if($_SERVER['REQUEST_METHOD']=='POST'){ 

     /* add in some random data at the iframe for fun and frolics */ 
     $_POST['date']=date(DATE_COOKIE); 
     $_POST['ip']=$_SERVER['REMOTE_ADDR']; 


     echo json_encode($_POST); 
     /* 
     this script could do whatever processing was required 
     with the post variables, any response is used by the 
     ajax callback 
     */ 
    } 
?> 
+0

您的解決方案將數據發佈到iframe。但在我的情況下,我需要與發佈的數據結合在iframe中的元素。也就是說,患者姓名和號碼是從表單發佈的,但抱怨位於iframe中。如何在點擊帖子後獲取所有數據(包括iframe中的元素)? – user5747822

+0

ok - 所以你說iframe包含你需要在你發佈到'clerkingpatient.php'腳本的數據中包含的項目(表單元素?)? – RamRaider

+0

是的。讓我詳細解釋一下。表單中包含一些元素(Patient_name textfield,Patient_number textfield&Post按鈕),而iframe具有更多元素(Compains文本窗格)。我需要將所有數據(姓名,號碼和投訴)保存到數據庫,然後點擊發布按鈕。有沒有辦法? – user5747822