2016-09-19 28 views
1

好了,所以我在这里面临这个问题 我有2个文件:我想加载上的index.html在iframe中get.php文件一旦提交按钮被按下

  1. index.html
  2. get.php

index.html我有一种形式:

<form method="POST" action="get.php"> 
    <input name="x" placeholder="first value" type="text"> 
    <input name="y" placeholder="second value" type="text"> 
    <input value="send" type="submit"> 
</form> 
<br> 
<iframe id="frame" class="right" width="100%" height="300" src="get.php"></iframe> 

这里是get.php文件

<?php 

if(isset($_POST['x'])==false) 
{ 
    echo "<h3>First variable is required...</h3>"; 
    exit; 
} 

if(isset($_POST['y'])==false) 
{ 
    echo "<h3>Second variable is required...</h3>"; 
    exit; 
} 

echo "<a style='padding:15px;background:#eee;color:#fff;border-radius:5px;' href='index.html'>Go back to the main page</a><br><br><br>"; 
$x=0; 
$y=0; 
$x=$_POST["x"]; 
$y=$_POST["y"]; 

echo "Values entered were $x and $y respectively<br><br><br>"; 
echo "Sum of $x+$y= ".($x+$y)."<br><br>"; 
echo "Multiplication of $x*$y= ".($x*$y)."<br><br>"; 
echo "Division of $x/$y= ".($x/$y)."<br><br>"; 
echo "Power of of $x<sup>$y</sup>= ".pow($x,$y)."<br><br>"; 
echo "power of $y<sup>$x</sup>= ".pow($y,$x)."<br><br>"; 

echo "Table of $x<br><br>"; 

for($i=1;$i<=10;$i++){ 
    echo "$x * $i = ".($x*$i)."<br>"; 
} 

echo "<br><br>Table of $y<br><br>"; 
for($i=1;$i<=10;$i++){ 
    echo "$y * $i = ".($y*$i)."<br>"; 
} 

?> 

现在所需要的是,我把2个值在这两个领域,一旦我点击提交按钮,它应该表现出放在index.html而不是在的get.php的结果正在载入一个新页面get.php

+2

你是怎么达到的假设是,当您提交表单中的iframe中应该刷新? – madalinivascu

+1

你可能会更好地使用JavaScript函数(可能与ajax)将数据提交给'get.php'并使用回调函数更新iframe – RamRaider

+0

我该怎么做? –

回答

0

您可以使用下面的代码片段设置iframe内容。数据使用ajax发送,您需要阻止form提交行为。尝试e.g:

$(function() { // document ready handler 
    $('form[action="get.php"]').on('submit', function(e) { 
    e.preventDefault(); 
    $.post(this.action, $(this).serialize(), function(data) { 
     var iframeDoc = $('#frame')[0].contentDocument; 
     iframeDoc.open(); 
     iframeDoc.write(data); 
     iframeDoc.close(); 
    }) 
    }) 
}); 
+0

使用这个,但没有任何事情发生 你能给我一个编辑我的代码版本 它对我来说真的很重要 –

+0

它可以在我测试它的时候工作。我不知道你是如何使用它...只要确保使用'$('#frame')[0] .contentDocument;'我使用'$('#iframe')[0]犯了以前的错误。 contentDocument;'...这就是说,如果这是你的问题,那么至少它应该在控制台中有一个错误,所以......顺便说一句,我不知道你如何调试它。提交事件是否至少被解雇?等等...... –

+0

暂不提交就无法正常工作 –