首页 > 解决方案 > 无法将我的表单数据传递给我的 php 函数

问题描述

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

标签: phpwordpressformsshortcode

解决方案


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

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


推荐阅读