我從知名網站購買了模板。我調整了模板的佈局以滿足我的需求。但我無法弄清楚如何在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');
?>
你有什麼試過? –
我發現了一個基本的PHP教程如何在php中設置複選框。我甚至不知道我是否已經正確地在php中設置了這些複選框:請參閱php部分''checkbox1''和''checkbox2'' – user1943906
在您的代碼(HTML)中,您使用名稱「newsletter1」兩次。這可能是問題。 – Dragony