2013-08-22 32 views
-1

我從知名網站購買了模板。我調整了模板的佈局以滿足我的需求。但我無法弄清楚如何在PHP中對複選框進行編碼以保存它們 - 當有人單擊提交時 - 在位於我的服務器上的txt文件中。 它將名稱和電子郵件地址保存到txt文件中,但不保存複選框。我一直在網上搜索兩天,找到一些有用的教程。我發現了一個基本的PHP教程,如何在php中設置複選框。我甚至不知道我是否已正確設置PHP這些複選框:看到PHP節'checkbox1 '' 和 '' checkbox2'複選框未保存在txt文件中

HTML:

  <div> 
      <label for="newsletter1"> 
      <input type="checkbox" class="input-check" id="newsletter1" name="newsletter1" value="huurder"> 
      <span style="width:120px;margin-top:30px;display:inline-block;clear">Ik ben een huurder</span> 
      <input type="checkbox" class="input-check" id="newsletter2" name="newsletter2" value="verhuurder"> 
      <span style="width:120px;margin-top:30px;display:inline-block;">Ik ben een verhuurder</span> 
      </div> 

PHP:

<?php   
     function response($responseStatus, $responseMsg) { 
     $out = json_encode(array('responseStatus' => $responseStatus, 'responseMsg' => $responseMsg)); 

     ob_end_clean(); 
     die($out); 
     } 

     // only AJAX calls allowed 
     if (!isset($_SERVER['X-Requested-With']) && !isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { 
     response('err', 'ajax'); 
     } 

     // can't read/write to emails file? 
     if (($file = fopen($emailsFile, 'r+')) == false) { 
     response('err', 'fileopen'); 
     } 

     // invalid name? 
     if(!isset($_POST['newsletter-name']) 
     || !trim($_POST['newsletter-name']) 
     || strtolower($_POST['newsletter-name']) == 'name' 
     || strlen($_POST['newsletter-name']) < 3) { 
     response('err', 'name'); 
     } 

     // invalid email address? 
     if(!isset($_POST['newsletter-email']) || !preg_match('/^[^@][email protected][a-zA-Z0-9._-]+\.[a-zA-Z]+$/', trim($_POST['newsletter-email']))) { 
     response('err', 'email'); 
     } 

     // checkbox1 
     if(isset($_POST['newsletter1']) && 
     $_POST['newsletter1'] == 'huurder') 
    { 
     echo "Maak a.u.b. een keuze tussen huurder of verhuurder. Beiden opties zijn mogelijk."; 
    } 
    else 
    { 
     echo "Ik ben geen huurder."; 
    }  

     // checkbox1 
    if(isset($_POST['newsletter2']) && 
     $_POST['newsletter2'] == 'verhuurder') 
    { 
     echo "Maak a.u.b. een keuze tussen huurder of verhuurder. Beiden opties zijn mogelijk."; 
    } 
    else 
    { 
     echo "Ik ben een verhuurder."; 
    }  

     // duplicate entry 
     $name = trim(ucfirst($_POST['newsletter-name'])); 
     $emailAddress = trim(strtolower($_POST['newsletter-email'])); 
     while($line = fgets($file)) { 
     $line = explode(' ', trim($line)); 
     $email = $line[0]; 
     if ($email == $emailAddress) { 
      response('err', 'duplicate'); 
     } 
     } // while 

     // write email to file 
     fseek($file, 0, SEEK_END); 
     if (fwrite($file, $emailAddress . ' - ' . $name . PHP_EOL) == strlen($emailAddress . ' - ' . $name . PHP_EOL)) { 
     // send email to site owner with new subscrciber info 
     if (preg_match('/^[^@][email protected][a-zA-Z0-9._-]+\.[a-zA-Z]+$/', trim($myEmail))) { 
      $headers = "MIME-Version: 1.0 \n"; 
      $headers .= "Content-type: text/plain; charset=UTF-8 \n"; 
      $headers .= "X-Mailer: PHP " . PHP_VERSION . "\n"; 
      $headers .= "From: {$myEmail} \n"; 
      $headers .= "Return-Path: {$myEmail} \n"; 
      $message = 'The following person was kind enough to subscribe to your newsletter:' . PHP_EOL . $name . ' - ' . $emailAddress; 
      @mail($myEmail, 'You have a new newsletter subscriber', $message, $headers); 
     } 
     response('ok', 'subscribed'); 
     } else { 
     response('err', 'filewrite'); 
     } 

     response('err', 'undefined'); 



    ?> 
+0

你有什麼試過? –

+0

我發現了一個基本的PHP教程如何在php中設置複選框。我甚至不知道我是否已經正確地在php中設置了這些複選框:請參閱php部分''checkbox1''和''checkbox2'' – user1943906

+0

在您的代碼(HTML)中,您使用名稱「newsletter1」兩次。這可能是問題。 – Dragony

回答

0

試着這樣做:

<input type="checkbox" name="newsletter[]" value="huurder" />huurder<br /> 
<input type="checkbox" name="newsletter[]" value="verhuurder" />verhuurder<br /> 

,然後訪問它在你的PHP文件是這樣的:

$newsletter = $_POST['newsletter']; 
    if(empty($newsletter)) 
    { 
    echo("You didn't select any newsletter."); 
    } 
    else 
    { 
    $N = count($newsletter); 

    for($i=0; $i < $N; $i++) 
    { 
     echo($newsletter[$i] . " "); 
     //save to variables and then write to file... 
    } 
    } 
+0

我現在就試試。我希望這在過去的兩天裏發揮了很大的作用,這是網站非常重要的組成部分。 – user1943906

+0

當然,讓我知道它是如何:) – RicardoE

+0

它不會工作,當我點擊提交它什麼都不做。 我已經把你的代碼放在/ //在php文件中有重複的條目 也許我把它寫錯了,它並不假設它是PHP。 – user1943906