PHP表单基于点击按钮,提交订阅或取消订阅的值

时间:2017-09-13 16:40:17

标签: php html forms magento

我有一个带有“订阅”按钮和“取消订阅”按钮的表单。我想根据点击的按钮,提交订阅或取消订阅的值。

如果单击“订阅”或“取消订阅”,我已经将根据应该发生的事情放在一起。现在我只需确定点击了哪个按钮。

这是我的HTML:

<form action="../external/multisubscribe.php" method="post" width="600px" style="margin: 0 auto;">
    <table style="text-align: center; vertical-align: middle; width: 100%;">
        <tbody>
            <tr>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <a href="www.example.com">
                        <img style="width: 100%; margin: 0 auto;" src="{{media url='wysiwyg/spslogo.png'}}" alt="Pilot Shop" />
                    </a>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <a href="http://example.com">
                        <img style="width: 60%; margin: 0 auto;" src="{{media url='wysiwyg/Aflogo.jpg'}}" alt="Journal" />
                    </a>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <a href="http://example.com/">
                        <img style="width: 60%; margin: 0 auto;" alt="iPad Pilot News" src="//examplenetwork.com/ipad/files/2016/03/iPad-Pilot-News-header-544-white-300x99.png" />
                    </a>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: padding: 0 20px 0 20px;">
                    <a href="http://example.com/">
                        <img style="width: 50%; margin: 0 auto;" alt="Student Pilot News" src="http://www.example.com/media/wysiwyg/Student-Pilot-News-final-140.png" />
                    </a>
                </td>
            </tr>
            <tr>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <p>The online magazine where readers are pilot in command. Opinion, analysis, tips and debate.</p>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <p>The online magazine where readers are pilot in command. Opinion, analysis, tips and debate.</p>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <p>Your #1 source for tips, tricks, videos and quizzes about flying with the iPad.</p>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%; padding: 0 20px 0 20px;">
                    <p>Everything new pilots need, from free videos to a flight school directory.</p>
                </td>
            </tr>
            <tr>
                <td style="text-align: center; vertical-align: middle; width:25%;">
                    <label>
                        <input style="text-align: center; vertical-align: middle; margin: 0 auto; display: inline-block;" name="pilotshop" type="checkbox" /> Sign Me Up 
                    </label>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%;">
                    <label>
                        <input style="text-align: center; vertical-align: middle; margin: 0 auto; display: inline-block;" name="airfacts" type="checkbox" /> Sign Me Up 
                    </label>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%;">
                    <label>
                        <input style="text-align: center; vertical-align: middle; margin: 0 auto; display: inline-block;" name="ipad" type="checkbox" /> Sign Me Up 
                    </label>
                </td>
                <td style="text-align: center; vertical-align: middle; width:25%;">
                    <label>
                        <input style="text-align: center; vertical-align: middle; margin: 0 auto; display: inline-block;" name="learntofly" type="checkbox" /> Sign Me Up 
                    </label>
                </td>
            </tr>           
        </tbody>
    </table>
    <div style="text-align:center;vertical-align:center;width:100%;margin-top:3.0em;">
        <input style="text-align: center; vertical-align: middle; margin: 0 auto;" name="email" placeholder="Enter Your Email" type="email" />
        <button style="text-align: center; vertical-align: middle; margin: 0 auto;margin-top:1.200em;" type="submit" name="submit" title="Subscribe" class="button">Subscribe</button>
        <button style="text-align: center; vertical-align: middle; margin: 0 auto;display: block; margin-top:2.500em;" type="submit" name="submit" title="Unsubscribe">Unsubscribe</button>
    </div>
    <input name="redirect" value="{{store url="email-subscribe-success"}}" type="hidden" />
    <input name="action" value="subscribe" type="hidden" />
</form>

这是我的PHP,在multisubscribe.php中,表单操作发布到。

<?php
include_once "../app/Mage.php";
if (isset($_POST['submit'])) {
    $choices = array();
    if (isset($_POST['airfacts'])) {
        $choices[] = 'airfacts';
    }
    if (isset($_POST['ipad'])) {
        $choices[] = 'ipad';
    }
    if (isset($_POST['learntofly'])) {
        $choices[] = 'learntofly';
    }
    if (isset($_POST['pilotshop'])) {
        $choices[] = 'pilotshop';
    }
    if (count($choices) == 0) {
        echo "Please choose at least one subscription.";
    } else {
        $email = $_POST['email']; //obtain email from post, place into $email variable
        $email = filter_var($email, FILTER_SANITIZE_EMAIL); //sanitizing email
        wpSubscription($choices, $email, $_POST['action']);
    }
    $redirect = $_POST['redirect'];
    header('Location: ' . $redirect);
}

function wpSubscription($sites, $email, $action)
{
    $mh                    = curl_multi_init();
    $curl_reference_array  = array();
    $unsubscribe_url_array = array();

    foreach ($sites as $site) {

        if ($site == 'pilotshop' && $action =='subscribe') { //if $site is pilotshop, then perform soap xml integration with listrak
            $sh_param   = array( //setting username & password array
                'UserName' => "", //removed username & pass for stackoverflow
                'Password' => ""  //removed username & pass for stackoverflow
            );
            $authvalues = new SoapVar($sh_param, SOAP_ENC_OBJECT); //encoding username and password array
            $headers[]  = new SoapHeader("http://webservices.listrak.com/v31/", 'WSUser', $sh_param);
            $soapClient = new SoapClient("https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL", array(
                'trace' => 1,
                'exceptions' => true,
                'cache_wsdl' => WSDL_CACHE_NONE,
                'soap_version' => SOAP_1_2
            ));

            $soapClient->__setSoapHeaders($headers);
            $params = array( //parameters for soap xml integration with listrak
                'WSContact' => array(
                    'EmailAddress' => $email,
                    'ListID' => '' //removed for stackoverflow
                ),
                'ProfileUpdateType' => 'Overwrite',
                'ExternalEventIDs' => '', //removed for stackoverflow
                'OverrideUnsubscribe' => true
            );

            try {

                $rest = $soapClient->SetContact($params); //using SetContact method, send parameters

            }
            catch (SoapFault $e) { //if an error occurs, display it

                echo '<pre>';

                print($e->getMessage());

                echo '</pre>';
            }
        } elseif ($site == 'airfacts') {
            $url = "http://example.com/" . $site . "/?na=ajaxsub";
        } elseif ($site == 'ipad') {
            $url = "http://example.com/" . $site . "/?na=ajaxsub";
        } elseif ($site == 'learntofly') {
            $url = "http://example.com/" . $site . "/?na=ajaxsub";
        } elseif ($action == 'unsubscribe') {
            $url = "http://example.com/unsubscribe.php";
        } else {
            return;
        }

        $fields = array(
            'ne' => $email,
            'site' => $site
        );
        $ch     = curl_init($url);
        if ($action == 'unsubscribe') {
            $returntransfer = true;
        } else {
            $returntransfer = false;
        }
        $opts = array(
            CURLOPT_POSTFIELDS => $fields,
            CURLOPT_RETURNTRANSFER => $returntransfer
        );
        curl_setopt_array($ch, $opts);
        curl_multi_add_handle($mh, $ch);
        $curl_reference_array[] = $ch;
    }

    $active = null;

    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($active > 0);

    foreach ($curl_reference_array as $chref) {
        $html   = curl_multi_getcontent($chref);
        $chu    = curl_init($html);
        $fields = array(
            'ts' => time()
        );
        $opts   = array(
            CURLOPT_POSTFIELDS => $fields
        );
        curl_setopt_array($chu, $opts);
        curl_exec($chu);
        curl_close($chu);
        curl_multi_remove_handle($mh, $chref);
    }

    curl_multi_close($mh);
    return;
}

?>

1 个答案:

答案 0 :(得分:0)

我在您的html中进行了一些更改,例如向name添加form(我为了澄清目的而删除了一些内联css),例如:< / p>

<form action="../external/multisubscribe.php" method="post" name="theForm">

同样在submit个输入中,我添加了id并更改了names,例如:

<button type="submit" name="subscribe" id="subscribe" class="button">Subscribe</button>
<button type="submit" name="unsubscribe" id="unsubscribe"title="Unsubscribe">Unsubscribe
</button>

我还更改hidden输入,添加id,例如:

<input name="action" id="action" value="subscribe" type="hidden" />

现在,使用javascript代码(可以改进,我知道):

document.getElementById('subscribe').addEventListener('click',function(e){
  e.preventDefault();
  document.getElementById('action').value = 'subscribe';
  document.theForm.submit();
})

document.getElementById('unsubscribe').addEventListener('click',function(e){
  e.preventDefault();
  document.getElementById('action').value = 'unsubscribe';
  document.theForm.submit();
})
  

这样我们就会将行动(subscribeunsubscribe)存储到hidden输入中。

然后在您的php代码中,检查您的action值:

 $theAction = $_POST['action'];
 if($theAction === "subscribe"){
   //subscribe code  
 }
 else{
   //unsubscribe code
 }