无法将我的表单数据传递给我的php函数

时间:2019-04-09 12:39:25

标签: php wordpress forms shortcode

在我的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"]

1 个答案:

答案 0 :(得分:0)

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

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