2013-05-02 65 views
-1

所以基本上,如果他們在85700和85775之間的郵遞區號,我希望它發佈到:如果輸入別的http://medicarechoicesofarizona.com/在PHP中,如何根據輸入文本指定不同的表單操作?

,我希望它發佈到http://www.ehealthmedicare.com下面像它。我猜JavaScript也可以,但PHP會很好,因爲這是一個Wordpress小部件。感謝大家!

<form action="http://www.ehealthmedicare.com/find-coverage?allid=Med35993" method="post" target="blank" _lpchecked="1"> 
    <p style="font-size:16px; text-align:center"> 
     Zip Code: <input type="text" name="zip" style="width:60px; height: 25px;"> 
    </p> 
    <p style="text-align:center"> 
     <input type="image" src="/wp-content/plugins/medicare-quote-widget/ehealth-medicare-go.png" title="Find Medicare Insurance Options" alt="Find Medicare Insurance Options" class="go-btn"> 
    </p> 
</form> 
+1

你可能會用一個通過javascript提交事件以更改表單提交時的操作。 – 2013-05-02 23:26:32

+1

沒有辦法與純PHP做到這一點 - PHP是服務器端,只會被調用來檢查何時提交表單。 – 2013-05-02 23:27:17

+0

@Snowsickle我相信有一種純PHP的方式,請查看我的答案。我願意就此實施發表意見 – verbumSapienti 2013-05-02 23:52:00

回答

0

其實,這可以通過使用純PHP來實現:

<form action="forwarder.php" method="post" target="blank" _lpchecked="1"> 
    <p style="font-size:16px; text-align:center"> 
     Zip Code: <input type="text" name="zip" style="width:60px; height: 25px;"> 
    </p> 
    <p style="text-align:center"> 
     <input type="image" src="/wp-content/plugins/medicare-quote-widget/ehealth-medicare-go.png" title="Find Medicare Insurance Options" alt="Find Medicare Insurance Options" class="go-btn"> 
    </p> 
</form> 

,然後添加這forwarder.php

<?php 
    if($_POST) 
    { 
     $zip = (int)$_POST['zip']; 
     if($zip >= 85700 && $zip <= 85775) 
     { 
      header('Location: http://medicarechoicesofarizona.com/'); 
     } 
     else 
     { 
      header('Location: http://www.ehealthmedicare.com/find-coverage?allid=Med35993'); 
     } 
    } 
?> 
1

您可以使用curl擴展名。檢查功能,例如:

function post($url, $data) { 
    //open connection 
    $ch = curl_init(); 

    //set the url, number of POST vars, POST data 
    curl_setopt($ch,CURLOPT_URL, $url); 
    curl_setopt($ch,CURLOPT_POST, 1); 
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data); 

    //execute post 
    $result = curl_exec($ch); 

    //close connection 
    curl_close($ch); 
} 

這樣稱呼它:

if($zip > 85699 && $zip < 85776) { 
    $url = 'http://url1'; 
} else { 
    $url = 'http://url2'; 
} 

post($url, 'foo=bar&hello=world'); 
相關問題