$ _POST链接在同一页面内

时间:2014-07-04 15:36:55

标签: php jquery css sql ajax

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP links</title>
<?php 
echo '<div style="background-color:#ccc; padding:20px">' . $_POST['message'] . '</div>'; 
?>
<style type="text/javascript">
// prepare the form when the DOM is ready 
$(document).ready(function() { 
    // bind form using ajaxForm 
    $('#htmlForm').ajaxForm({ 
        // target identifies the element(s) to update with the server response 
        target: '#htmlExampleTarget', 

        // success identifies the function to invoke when the server response 
        // has been received; here we apply a fade-in effect to the new content 
        success: function() { 
            $('#htmlExampleTarget').fadeIn('slow'); 
        } 
    }); 
});
</style>
</head>

<body>
<form id="htmlForm" action="html-echo.php" method="post"> 
        Display List of Links <input type="text" name="message" value="Hello HTML" /> 
        <input type="submit" value="Submit links" /> 
</form>
</body>
</html>

我试图将结果显示在同一页面上,而不必创建.php页面并将结果显示在单独的页面上。它是一个用户可以提交任何类型的链接并将结果显示在页面上的应用程序。

有没有办法可以保存结果供以后参考?刷新页面时是否显示结果?

1 个答案:

答案 0 :(得分:0)

<!doctype html>
 <html>
  <head>
  <meta charset="UTF-8">
   <title>PHP links</title>
  <?php 
 echo '<div style="background-color:#ccc; padding:20px">' . $_POST['message'] . '</div>'; 
 ?>
 <script type="text/javascript">
    $(document).ready(function() {
            $('#formid form').submit(function(){
                    $.get('result.php', $(this).serialize(), function(data){
                            $('#result').html(data);

                    });                             
                    return false;
            });
    });
    </script>

 </head>

 <body>
  <div id="formid">
  <form> 
    Display List of Links <input type="text" name="message" value="Hello HTML" /> 
    <input type="submit" value="Submit links" /> 
     </form>
     </div>

     <div id="result"></div>
     </body>
       </html>

尝试以上格式在同一页面上显示表单结果而不加载另一个php文件作为新的,但你应该创建result.php文件以生成结果取决于表单输入,你可以通过使用获取表单的值关于result.php的$_REQUEST[''],下面是result.php

的示例
<?php

$message=$_REQUEST['message'];

echo $message;

&GT;

您生成的result.php结果将显示在index.php <div id="result"></div>上,无需重新加载或重定向页面