【问题标题】:Can not get my form data passed to my php function无法将我的表单数据传递给我的 php 函数
【发布时间】:2019-04-09 12:39:24
【问题描述】:

在我的 Wordpress 网站上有一个带有邮政编码的表单,提交后它会将您带到该服务区域的静态页面。我无法将表单中的数据发送到我的 php 函数。我尝试过使用短代码属性并将表单操作设置为 zip-finder.php。任何帮助将不胜感激..

形式:

 /*<div class="zip"><form action="[zip_finder]" method='post'>Check           pricing and availability in your city or town: <input name='zip' size="12"     type="text" placeholder="Enter Zip Code" /> <button id="zip"        type="submit">Get Started</button></form></div></p>
*/
I have added the line include('zip-finder.php'); to my theme    functions.php file.

处理表单数据的php文件:

//<?php
function zipfinder_function() {

$zip=$_POST['zip'];

//commented out some things I tried to get it to work
/*$a = shortcode_atts( array(
    'zip' => 'something',
    'bar' => 'something else',
), $atts);*/

//switch ($atts['zip'])

   switch ('zip')
    {

            case "02052":
            header("Location: https://somewebsiteurl.com/");
            break;

            case "02056":
            header("Location: https://somewebsiteurl.com/");
            break;

            case "02090":
            header("Location: https://somewebsiteurl.com/");
            break;

            default;
            //header("Location: https://websiteurl.com/");
            break;
    }
 }
 add_shortcode('zip_finder', 'zipfinder_function');
//?>


I've tried using short code attributes:  [zip_finder zip="zip"]

【问题讨论】:

  • action="[zip_finder]" 这是您的实际代码吗? Action 应该是一个 URL
  • 是的,这就是代码。 action='[zip_finder]'
  • 是的,这就是代码。 action='[zip_finder]' 它确实有效。在 php 文件中,如果我设置 $zip=02056;它将转到邮政编码和服务区域的正确页面。我无法从表单中获取数据来工作。

标签: php wordpress forms shortcode


【解决方案1】:

乍一看,您似乎正在切换一个静态值“zip”。尝试切换变量开关($zip)。

https://www.php.net/manual/en/control-structures.switch.php

【讨论】:

  • 谢谢尼克。我对 switch ($zip) 进行了更改,但仍然无法正常工作。点击提交后,它将进入一个新页面:somewebsiteurl.com/[zip_finder]
  • 正如 Joni 所说,action 应该是一个 URL。您应该指向将处理发布数据的文件。这就是您最终访问 example.com/[zip_finder] 的原因,您告诉它去那里并发布 zip 的值。将其指向 zip-finder.php
猜你喜欢
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
相关资源
最近更新 更多