简体   繁体   中英

Proper way to submit a form with 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.

I am using php as a template for my website. 我使用php作为网站模板。 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).

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!

我不确定第二个问题,但是对于提交联系表,您是否尝试过将表单的操作设置为“ index.php?page = contact”之类的内容?

Your form needs to 'remember' the current page and send it along so:

<input type='hidden' name='page' value='<?php echo $page; ?>'>

and also the script should check if current request is a form submission:

$page = (isset($_GET['page'])) ? $_GET['page'] : $_POST['page'] ;

I assume that your form is on the contact page? The page is retrieved based on a $_GET['page'] from elsewhere. When you refresh the page, it runs the page selection script again. But since you didn't come there from a form with a method=post the $_GET['page'] does not exist, hence the page is set to home as per your code. You need to store the name of the page; either as a $_SESSION variable or by a hidden input on the contact form (remember to change $_GET to $_REQUEST if you do so).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM