2016-09-18 90 views
0

我在wordpress中很漂亮,我在处理一个简单的表单布局/提交时遇到了一些困难。 我有一个页面(从WP-管理页面创建的),它保存下面的代码:在wordpress中提交一个web表单

<div class="container-fluid"> 
    <form class="form-horizontal" action="" method="POST" > 
     <div class="row form-group"> 
      <div class="col-xs-12"> 
       <label for="name">Nome</label> 
       <input type="text" class="form-control" id="name" name="name" /> 
      </div> 
     </div> 
     <div class="row form-group"> 
      <div class="col-xs-12"> 
       <label for="residence">Residência</label> 
       <input type="text" name="residence" id="residence" /> 
      </div> 
     </div> 
     <div class="row form-group"> 
      <div class="col-sm-6"> 
       <input class="btn btn-default" type="submit" value="Registar"> 
      </div> 
     </div> 
    <form> 
</div> 

我在这里的问题是,我不知道要放什么东西在action属性以及如何处理表单之后。我试图在模板文件夹中创建一个php文件,并将其放入操作中,但完全无法使用。 我也看到了一些可以添加到functions.php的动作,但我并没有把它看作是解决问题的最佳解决方案。我只需要提交表单,将其作为元数据存储在数据库中,并将用户重定向到主页面。

回答

1

我的建议是使用action="#",它将用户按下提交重定向到当前页面。然后,您可以检查隐藏字段,而不显示表单,而是检查数据并将其存储在数据库中,然后将用户重定向到主页面。

你可以把这个文件放在Wordpress安装的任何地方(只要你的用户可以访问它)。如果可移植性不是问题,请将其保留在根目录中,或者如果可移植性确实很重要,请查找making a page template file

此外,请谨慎使用属性name="name",因为这会掩盖您的Wordpress页面处理,我似乎记得(Wordpress使用它来标识它在哪个页面上)。

下面是相同的页面处理的例子:

<?php 
if (isset($_POST["formSubmitted"])) { 
    $name = $_POST["formName"]; 
    $residence = $_POST["formResidence"]; 
    // do database work here with $name and $residence 
    // then output javascript to redirect to main page 
    echo '<script>window.location = "http://example.com";</script>'; 
} else { 
    // we display the form 
?> 
<div class="container-fluid"> 
    <form class="form-horizontal" action="#" method="POST" > 
     <div class="row form-group"> 
      <div class="col-xs-12"> 
       <label for="name">Nome</label> 
       <input type="text" class="form-control" id="name" name="formName" /> 
      </div> 
     </div> 
     <div class="row form-group"> 
      <div class="col-xs-12"> 
       <label for="residence">Residência</label> 
       <input type="text" name="formResidence" id="residence" /> 
      </div> 
     </div> 
     <div class="row form-group"> 
      <div class="col-sm-6"> 
       <input class="btn btn-default" type="submit" value="Registar"> 
      </div> 
     </div> 
    <input type="hidden" name="formSubmitted" value="1"> 
    </form> 
</div> 
<?php 
} 
?> 

替代解决方案可能是使用get_stylesheet_directory_uri(),返回的主题样式表所在的目录。您可以使用此像这样:

<form action="<?php echo get_stylesheet_directory_uri()."/some_php_file.php"; ?>" method="POST"> 

这将然后将数据传递到该文件,从中你可以把它添加到数据库中。