2013-07-02 71 views
0

我遇到了我的PHP問題,我希望你能幫助我。PHP:一種形式,一種提交,PHP中有幾個「動作」

實質上,客戶端需要一個表單和一個提交按鈕,但是他希望PHP將信息發送到兩個地方:1.)將「評論」消息直接發送到他的電子郵件收件箱,但也2.)將電子郵件地址和隱藏變量發送給Constant Contact(這是執行他的通訊的公司)。

這裏是通過HTML表單:

<form action="SendToConstantContact.php" name="myform" method="post"> 
Your email*:<br/> 
<input type="text" name="ea" size="39" value="" style="font-size:10pt;"><br/> 
Your message (optional):<br/> 
<textarea name="comments" cols="30" rows="3" style="font-size:10pt;"></textarea><br/> 
<input type="hidden" name="llr" value="z4u4kndab"> 
<input type="hidden" name="m" value="1103180583929"> 
<input type="hidden" name="p" value="oi"> 
<input type="submit" value="Submit" /> 
</form> 

這裏是PHP爲止。我有第一個目標 - 工作到這個PHP,PHP直接發送一封電子郵件到郵件內容的收件箱。我可以在這個PHP代碼中添加什麼以將變量一起發送到常量聯繫人?

<?php 
$emailSubject = 'Customer Has a Question!'; 
$webMaster = '[email protected]';  

$ea = $_POST['ea']; 
$comments = $_POST ['comments']; 
$llr = $_POST ['llr']; 
$m = $_POST ['m']; 
$p = $_POST ['p']; 

$body = <<<EOD 
<br><hr><br> 
Email: $ea <br> 
Comments: $comments <br> 
&nbsp;<br> 
And below are the four variables sent to Constant Contact:<br> 
Email: $ea <br> 
LLR: $llr <br> 
M: $m <br> 
P: $p <br> 

&nbsp;<br> 
These comments were sent from the test.html page of the JW Secure site. <br> 
The visitor has checked the box that would send their information to Constant Contact.<br> 
EOD; 
$headers = "From: $ea\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
$success = mail($webMaster, $emailSubject, $body, $headers); 
$theResults = <<<EOD 
<META http-equiv="refresh" content="0;URL=http://www.jwsecure.com/contact/thank-you/"> 
EOD; 

echo "$theResults"; 
?> 

我環顧四周,在這個網站和其他人,但我還沒有發現的只是沿着你的PHP文件發送信息的例子。看起來它會比較簡單,但我很難過。我所嘗試的一切都會導致錯誤。如果它是一個直接發佈到ConstantContact的HTML表單,那麼這個動作應該是:「action =」http://visitor.r20.constantcontact.com/d.jsp「......但我不是一個PHP人(顯然) ,這樣有利於與語法非常感謝!

謝謝你在前進, 巴里

+2

你可以使用捲曲。 – DaGardner

+0

你可以使用ajax。 – DevZer0

+0

嘗試谷歌搜索「cURL POST PHP」(我不是故意粗魯,但我沒有時間完整的答案) –

回答

0

您可以通過使用捲曲或Jquery的完成它。

捲曲在其中您可以針對一個方式從您的代碼獲取來自它的html響應的URL ..cURL表示客戶端URL,它允許您連接其他URL並根據需要使用響應。

檢查此鏈接: -

Working with CURL

+0

謝謝你,Vivek!我正在檢查該鏈接。這似乎正在做我想做的事!我會將該代碼發佈到我的PHP中,編輯必要的變量,然後回答我的問題。 –

0

以下是你需要改變,如果你想使用jquery的基本結構。您可以通過ajax提交一個,然後成功提交另一個表單。我只包含了一個如何將表單數據處理爲ajax調用的例子。如果不想手動編寫所有的變量,你可以使用jQuery插件像ajaxform

<form id='myform' action="SendToConstantContact.php" name="myform" method="post"> 
Your email*:<br/> 
<input type="text" name="ea" size="39" value="" style="font-size:10pt;"><br/> 
Your message (optional):<br/> 
<textarea name="comments" cols="30" rows="3" style="font-size:10pt;"></textarea><br/> 
<input type="hidden" name="llr" value="z4u4kndab"> 
<input type="hidden" name="m" value="1103180583929"> 
<input type="hidden" name="p" value="oi"> 
<input type="button" id='submit' value="Submit" /> 
</form> 

<script type='text/javascript'> 
    $(document).ready(function (f) { 
     $('#submit').click(function (e) { 
      e.preventDefault(); 
      var data = {"llr" : $("input[name=llr]").val()….}; 
      var options = { 
       data: data, 
       type: "post", 
       url: "http://visitor.r20.constantcontact.com/d.jsp", 
           success: function (e) { 
             $('#myform').submit(); 
           } 

      }; 
      $.ajax(options); 
     }; 
    } 
</script> 
+0

酷,DevZer0!謝謝。我應該提到這一點,但我已經有一個onclick函數腳本運行,它檢查常量聯繫人的複選框是否被單擊,然後將它們發送到兩個PHP中的一個。我沒有把我的問題留下,因爲我不想混淆這個問題。但基本上,我正在尋找一種涉及PHP而不是HTML的解決方案。再次感謝,這很有趣,很棒。 –

0

只是用捲曲用PHP來發表您的數據:

小例子,沒有測試...查看PHP更多curl_setopt

$ch = curl_init(); 
$path = "http://visitor.r20.constantcontact.com/d.jsp"; 
curl_setopt($ch, CURLOPT_URL, $path); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray); 
curl_exec ($ch); 
curl_close ($ch); 
unset($ch); 

我的經驗文檔:儘量少的選項,然後再添加更多的,不以百萬計的設置啓動,並期望它的工作:)

0

要將信息傳遞給ConstantContact,您需要使用PHP curl extension AJAX將無法正常工作,因爲它位於不同的域上。

類似下面應該讓你開始

$data = array('ea' => $_POST['ea'], 'llr' => $_POST['llr'], ...);  

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://visitor.r20.constantcontact.com/d.jsp"); 
curl_setopt($ch, CULROPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Stops the result from being sent to the browser 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$result = curl_exec($ch); //$result now contains the response 
curl_close($ch); 
+0

感謝RMcLeod!是的,所有的跡象都指向捲曲。我真的很喜歡上面的代碼是最小的。我已經添加了這個到我的PHP,完成聲明數組,並上傳。一切仍然順利通過成功的謝謝頁面。沒有我能看到的錯誤。我會跟進Constant Contact並確保他們獲取信息。 –

+0

爲了澄清,我調整的唯一部分是$ data = array('ea'=> $ _POST ['ea'],'llr'=> $ _POST ['llr'],'m'=> $ _POST ['m'],'p'=> $ _POST ['p']); –

+0

@BarryCraig你可以檢查'$ result'的內容來查看常量聯繫人的響應(如果有)。 – RMcLeod

-1
<?php 
if($_POST['Send']!='') 
{ 
$subject = "subject"; 
$message = "content"; 
$from = "enter from "; 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$to="[email protected]"; 
$from="User"; 
$headers .= "From:" . $from; 
$response=mail($to,$subject,$message,$headers); 

if($response) 
{ 
    $msg="show msg."; 
} 
} 
?> 

希望它會幫助你

+0

這不回答這個問題 – RMcLeod