3

I've created a page in Wordpress and used this form as template. But when I submit the form it shows Error 404 page not found.

I've tried various scripts in php but I didnt get fruitful results. Hoping for the best answers. Thanks :)

Here is my coding

<?php
/*
template name: visit
*/

$servername= "localhost";
$username="root";
$password="";
$database="wp_chandan"; 


if(isset($_POST['name']) && isset($_POST['email']))
{

    $name=$_POST['name'];
    $email=$_POST['email'];
    var_dump($email);


    @mysql_connect($servername,$username,$password);
    @mysql_select_db($database);

    $query=mysql_query("insert into visitor_d (NAME,Email) values ('$name','$email') ");    
} ?>

<html>
    <head>  
        <?php get_header(); ?>
        <style type='text/css'>
        /* form elements */
        form {
            margin:100px; 
            padding: 10px 2px;
            background: #F5F5F5;  
            width: 50%;
            height:auto;
            border:1px solid green;
        }

        input {
            padding:2px;
            border:1px solid #eee;
            font: normal 1em Verdana, sans-serif;
            color:#777;
            align:center;
        }

        input.button { 
            font: bold 12px Arial, Sans-serif; 
            height: 24px;
            margin: 0;
            align:center;
            padding: 2px 3px; 
            color: #333;
            background: #e7e6e6 url(MarketPlace-images/button.jpg) repeat-x;
            border: 1px solid #dadada;
        }

        </style>
    </head>

    <form action="" method="POST">
        <fieldset>
            <legend><b><u>Visitor Details:</u></b></legend>
            Name: <input type="text" name="name" placeholder="Fill Name" required><br><br>
            Email: <input type="text" name="email" required placeholder="Enter Email"><br><br>
            <input type="submit" name="submit"><br>
        </fieldset>
    </form>
    <?php get_footer();?>
</html>
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Daniel
  • 33
  • 6

2 Answers2

2

Make sure that you don't have any reserved WordPress terms in the name attributes in the fields of the form.

WordPress uses many of these reserved terms for it's internal rewriting and as a result if you using name attributes such as "name", it breaks the WordPress rewrite rules and causes this 404 Not Found page after submitting the form.

See more here: http://www.lost-in-code.com/platforms/wordpress/wordpress-404-not-found-when-submitting-form/

contrid
  • 950
  • 9
  • 18
  • 1
    I thought I was going crazy when posting a form to a virtual page (using rewrite rules) with a custom template that always returned the 404 header. simply changing 'name' to '_name' fixed the issue. Thank you so much! – anastymous May 18 '17 at 22:54
1

html5 doesn't like the empty action attribute, it does cause issues in some browsers. you have not selected a standard so i assume the browser will default to html5

<form action="#" method="POST"> 

Setting the action as # should work but one thing to watch out for is actions added before your page template is loaded, this can obviously grab the $_POST and redirects would be common so that may also be a reason.

also

Name: <input type="text" name="name" placeholder="Fill Name" required>

WP does not like when you call a field "name" it conflicts with something (i have forgotten the reason by now)

 Name: <input type="text" name="aname" placeholder="Fill Name" required>
David
  • 5,897
  • 3
  • 24
  • 43