iframe中的PHP POST变量

时间:2016-07-23 11:40:58

标签: php post iframe

我正在尝试将表单中的变量发布到表单本身。表单里面有一个iframe,指向一个php文件(其中包含我需要发布的元素)。

以下是代码摘录:

  //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"/>

在提交表单时,我收到此错误“注意:未定义的索引:在第32行的C:\ wamp \ www \ bossmed \ clerking \ clerkingpatient.php中投诉” 因为我想使用iframe中的元素,所以我做错了。

1 个答案:

答案 0 :(得分: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
        */
    }
?>