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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭