-1

我从一个知名网站购买了一个模板。我已经根据我的需要调整了模板的布局。但是我不知道如何在 PHP 中对复选框进行编码以保存它们 - 当有人点击提交时 - 在位于我服务器上的 txt 文件中。它将名称和电子邮件地址保存到 txt 文件,但不保存复选框。我在网上搜索了两天,找到一些有用的教程。我找到了一个基本的 php 教程,如何在 php.ini 中设置复选框。我什至不知道我是否在 php 中正确设置了这些复选框:请参阅 php 部分 ''checkbox1'' 和 ''checkbox2''

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('/^[^@]+@[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('/^[^@]+@[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');



    ?>
4

1 回答 1

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...
    }
  }
于 2013-08-22T18:51:04.800 回答