dongmuyan5638 2013-07-20 21:44
浏览 33
已采纳

使用php / javascript提交表单的正确方法

I am trying to figure out the proper way to submit a form via php:

In part I have two questions: 1 specifically about the site that I am working on, and another about form submitting in general.

Question 1. I am using php as a template for my website. so I have one index.php page with a header and footer and all of my content is pulled in from a php function like so:

     <article id="main">
        <?php
            $page = $_GET['page'];
                if(empty($page)){
                    $page = 'home';
                }

                $page.='.php';

                if(file_exists("pages/$page")) {
                    include("pages/$page");
                } else {
                    echo "$page no exist";
                }

        ?>
     </article>

This is the code I am using for my form, and am using it in the head tag of my index.php page:

        <head>
<?php
if ($_POST['submit'] && $human == '4') {
    if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }
} else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>

</head>

And the html form code:

<form method="post" action="index.php">

    <legend>Contact Us</legend>
    <fieldset>
        <label>Name</label>
        <input name="name" placeholder="Type Here">

        <label>Email</label>
        <input name="email" type="email" placeholder="Type Here">

        <label>Message</label>
        <textarea name="message" placeholder="Type Here"></textarea>

        <label>*What is 2+2? (Anti-spam)</label>
        <input name="human" placeholder="Type Here">
    </fieldset>

     <input id="submit" name="submit" type="submit" value="Submit">

</form>

When I submit the form it refreshes and puts my echo message into my index.php?page=home page instead of my index.php?page=contact page. How would I get the echo to stay on the same page of the form? (I have tried changing the action=" " part of the form but cant seem to get it to work).

Question 2. In general when I have been inspecting other web pages to see their form code I have been seeing this everywhere:

try
{
    for(var lastpass_iter=0; lastpass_iter < document.forms.length; lastpass_iter++)
    {
        var lastpass_f = document.forms[lastpass_iter];
        if(typeof(lastpass_f.lpsubmitorig2)=="undefined")
        { 
            lastpass_f.lpsubmitorig2 = lastpass_f.submit;
            lastpass_f.submit = function(){ 
                var form=this;
                var customEvent = document.createEvent("Event");
                customEvent.initEvent("lpCustomEvent", true, true);
                var d = document.getElementById("hiddenlpsubmitdiv"); 
                for(var i = 0; i < document.forms.length; i++)
                { 
                    if(document.forms[i]==form)
                    { 
                        d.innerText=i; 
                    }
                } 
                d.dispatchEvent(customEvent);
                form.lpsubmitorig2(); 
            } 
        }
    }
}
catch(e){}

Is anyone familiar with the above code? And if so, is there a general script for properly submitting forms?

Thanks!

  • 写回答

4条回答 默认 最新

  • dougan0529 2013-07-20 22:12
    关注

    See how your code starts with $page = $_GET['page']; ?

    It means you'll send a "page" variable as part of the url. So

    change your form tag to this

    <form method="GET" action="index.php?page=contact">  
    

    and you should start to see what you expect.

    the method GET tells your browser to send the form data as one long URL (so, visible to the user), as opposed to POST, where data will be invisibly sent to the server (much more secure).

    But then you are using $_POST to retrieve GET data, so the rest of your code won't work.

    so try this instead: go to index.php?page=contact and it will solve your first question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!