简体   繁体   English

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

[英]PHP Form based on button clicked, submit value of Subscribe or Unsubscribe

I have a form with a Subscribe button and an Unsubscribe button. 我有一个带有“订阅”按钮和“取消订阅”按钮的表单。 I want to, based on which button is clicked, submit the value of either Subscribe or Unsubscribe. 我想基于单击哪个按钮来提交“订阅”或“取消订阅”的值。

I've already put together what should happen based on if Subscribe or Unsubscribe is clicked. 我已经根据单击“订阅”或“取消订阅”将发生的情况放在一起。 Now I just have to determine which Button is clicked. 现在,我只需要确定单击了哪个按钮。

Here is my HTML: 这是我的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>

This is my PHP, in multisubscribe.php, that the form action posts to. 这是我的PHP,在multisubscribe.php中,表单操作将其发布到该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;
}

?>

I made a few changes in your html, like adding a name to your form (I'm deleting some inline css just for clarification purposes), like: 我对您的html进行了一些更改,例如为您的form添加name (出于澄清目的,我删除了一些内联css ),例如:

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

Also in both submit inputs I added an id and changed both names , like: 同样在两个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>

I also change the hidden input, adding an id , like: 我还更改了hidden输入,添加了id ,例如:

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

Now, with the javascript code(it could be improved, I know): 现在,有了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();
})

This way we are storing our action( subscribe or unsubscribe ) into the hidden input. 这样,我们将我们的动作( subscribeunsubscribe )存储到hidden输入中。

Then in your php code, just check for your action value: 然后在您的php代码中,只需检查您的action值即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM