dskyx46424 2014-01-01 05:35
浏览 29
已采纳

在div中加载内容

I have the 3 php files when the user clicks a form the 2.php file is loaded to see if user can fill out the form or not if he can a form shows up and after he fills up the form I want the result displayed in the div id=content of 1.php

Is calling/opening the php file with form content in new window better practice than loading it in div.Is loading the div unnecessary hardwork? i think its better for user to click links and see what he wishes to see in the div id=content on the same page. Is it good practice?

Also i'm facing an issue with loading the form results in the div after i click submit the form disappears something i'm doing wrong?

here are the 3 files

1.php

<!DOCTYPE html>
<html>
    <head>
        <title> Test </title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        </script>
        <link rel="stylesheet" href="css/layout.css"; />
    </head>
    <body>
        <div id="header">
        </div>
        <div id="menu">
            <a href="#" id="l1"> Form </a>
        </div>  
        <div id="content">
        </div>  
        <div id="footer">
        </div>
    </body>
    <script>
        $(document).ready(function(){
            $('#l1').click(function(){
                $('#content').load('2.php');
            });
        });
    </script>
</html>

2.php

<?php
//code here that checks if user can fill form

echo'<script>$("#content").load("3.php");</script>';

// if user cannot
echo "You have already filled";
?>

3.php

<?php
    //check for user session and form_status using $_SESSION 
    if( isset($_POST['submit']) ){
        $form_value=array();
        foreach($_POST as $key => $value){
            $form_value[$key]=htmlspecialchars(trim($value),ENT_QUOTES);
            echo "{$form_value[$key]}<br/>";
        }

    }
?>
    <form method="post" name="stud_det" id="stud_det" action="" >
        <table>
            <tr>
                <td>Name :</td><td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <td>Register Number :</td><td><input type="text" name="regno"/></td>
            </tr>
            <tr>
                <td>Date of Birth :</td><td><input type="text" name="dob"/></td> 
            </tr>
            <tr>
                <td><input type="submit" name="submit" value="Submit" style="font-size:16px;"/></td>
            </tr>
        </table>
    </form>

<script>
    $("#stud_det").submit(function(){
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr("post"),
                url: $(this).attr("3.php");
                success: function(response){
                    $("#content").html(response);
                }
            });
            return true;
        });
</script>

layout.css

#menu{
    position:absolute;
    top:10%;
    bottom:10%;
    left:0;
    right:90%;
    background-color:#7c7;
    font-size:18px;
}
#header{
    position:absolute;
    top:0;
    bottom:90%;
    left:0;
    right:0;
    background-color:#ff0;
}
#footer{
    position:absolute;
    top:90%;
    bottom:0;
    left:0;
    right:0;
    background-color:#fcf;
}
#content{
    position:absolute;
    top:10%;
    bottom:10%;
    left:10%;
    right:0;
    background-color:#ccc;
    text-align:center;
    font-size:18px;
}
  • 写回答

1条回答 默认 最新

  • doucheng8471 2014-01-01 05:42
    关注

    Loading content into the current page without reloading is often more flexible, so it's common practice.

    For the last problem, in your .submit() function, change return true to return false. Returning false tells the browser not to run the default action of the submit button, which is to do a normal form submission that reloads the page.

    Other errors I found when running your code:

    You have an unwanted ; here:

    <link rel="stylesheet" href="css/layout.css"; />
    

    Your AJAX has:

                type: $(this).attr("post"),
                url: $(this).attr("3.php");
    

    Those are wrong, it should be:

                type: $(this).attr("method"),
                url: "3.php",
    

    Another issue is that when you use $(this).serialize(), it doesn't include the submit button in the data. So when 3.php does if(isset($_POST['submit'])), it doesn't succeed. I suggest you check whether one of the input fields is set instead.

    So with all the fixes, 3.php now looks like:

    <?php
        //check for user session and form_status using $_SESSION 
        if( isset($_POST['name']) ){
            $form_value=array();
            foreach($_POST as $key => $value){
                $form_value[$key]=htmlspecialchars(trim($value),ENT_QUOTES);
                echo "{$form_value[$key]}<br/>";
            }
    
        }
    ?>
        <form method="post" name="stud_det" id="stud_det" action="" >
            <table>
                <tr>
                    <td>Name :</td><td><input type="text" name="name" /></td>
                </tr>
                <tr>
                    <td>Register Number :</td><td><input type="text" name="regno"/></td>
                </tr>
                <tr>
                    <td>Date of Birth :</td><td><input type="text" name="dob"/></td> 
                </tr>
                <tr>
                    <td><input type="submit" name="submit" value="Submit" style="font-size:16px;"/></td>
                </tr>
            </table>
        </form>
    
    <script>
        $("#stud_det").submit(function(){
                $.ajax({
                    data: $(this).serialize(),
                    type: $(this).attr("method"),
                    url: "3.php",
                    success: function(response){
                        $("#content").html(response);
                    }
                });
                return false;
            });
    //@ sourceURL=dynamicScript.js
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型