我目前正在开发一个项目,我尝试使用Ajax自动保存整个设置表单。我在项目目录中基本上有这个设置(只显示与此问题相关的文件):
/FireGuard/
|-> headincludes.php
|-> index.php
|-> settings.php
|-> settingsupdate.php
|-> settings.json
settings.php文件以及headincludes.php都包含在index.php中。 headincludes.php文件包含一个名为settingsUpdater()的Javascript函数,它通过GET请求将表单发送到settingsupdate.php,后者将所有值包装到settings.json中。 问题是调用settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);在Web控制台中出现此错误:
[15:30:17.807] TypeError:alarmonoff未定义
以下是您需要了解的不同源代码。
headincludes.php:
<!-- Ajax Request function -->
<script type="text/javascript">
var alarmonoff = $('#alarmonoff:checked').val();
var emailnotifications = $('#emailnotifications:checked').val();
var smsnotifications = $('#smsnotifications:checked').val();
var firstname = $('#firstname').val();
var lastname = $('#lastname').val();
var username = $('#username').val();
var email = $('#email').val();
var phone = $('#phone').val();
function settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone) {
if (alarmonoff.checked) {
var alarmonoffstate = "on";
}
else {
var alarmonoffstate = "null";
}
if (mailnotifications.checked) {
var mailnotificationsstate = "on";
}
else {
var mailnotificationsstate = "null";
}
if (smsnotifications.checked) {
var smsnotificationsstate = "on";
}
else {
var smsnotificationsstate = "null";
}
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","settingsupdate.php?alarmonoffstate="+alarmonoffstate+"&mailnotificationsstate="+mailnotificationsstate+"&smsnotificationsstate="+smsnotificationsstate+"&firstname="+firstname+"&lastname="+lastname+"&username="+username+"&email="+email+"&phone="+phone,true);
xmlhttp.send();
}
</script>
settingsupdate.php:
<?php
$settings = array(
'alarmonoff' => $_GET['alarmonoffstate'],
'mailnotifications' => $_GET['mailnotificationsstate'],
'smsnotifications' => $_GET['smsnotificationsstate']
'firstname' => $_GET['firstname']
'lastname' => $_GET['lastname']
'username' => $_GET['username']
'email' => $_GET['email']
'phone' => $_GET['phone']
);
echo("Array ok: \n");
echo(var_dump($settings));
echo("\n");
$jsonsettings = json_encode($settings);
echo("JSON Encode ok: \n");
echo($jsonsettings);
echo("\n");
$handle = fopen("settings.json","w") or die("Fopen not working");
echo("fopen ok: \n");
echo($handle);
echo("\n");
fwrite($handle, $jsonsettings) or die("Fwrite not working");
echo("fwrite ok \n");
fclose($handle) or die("Fclose not working");
echo("fclose ok \n");
?>
settings.php(有点长,抱歉,但我想上传整个文件会更好):
<?php
$handle = fopen("./settings.json","r");
$settings = fread($handle, 512);
$jsonsettings = json_decode($settings, true);
extract($jsonsettings);
fclose($handle);
function alarmonoffcheck () {
global $alarmonoff;
if ($alarmonoff == "on") {
echo("checked");
}
}
function emailnotifcheck () {
global $mailnotifications;
if ($mailnotifications == "on") {
echo("checked");
}
}
function smsnotifcheck () {
global $smsnotifications;
if ($smsnotifications == "on") {
echo("checked");
}
}
global $firstname;
global $lastname;
global $username;
global $email;
global $phone;
?>
<section id="settings" class="tab-pane fade">
<div class="row">
<nav class="col-sm-3">
<ul id="settingsmenu" class="nav nav-pills nav-stacked">
<li class="active"><a href="#general" data-toggle="tab">General</a></li>
<li><a href="#profile" data-toggle="tab">Profile</a></li>
<li><a href="#detection" data-toggle="tab">Detection</a></li>
<li><a href="#network" data-toggle="tab">Network</a></li>
</ul>
</nav>
<div class="col-sm-6">
<section class="tab-content">
<section id="general" class="tab-pane fade in active">
<h2>General settings</h2>
<form class="form-horizontal">
<div class="control-group row">
<label class="switch-label col-sm-5 control-label" for="alarmonoff">Alarm/Detection</label>
<div class="col-sm-6">
<div class="switch-wrapper">
<input onChange="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);" id="alarmonoff" name="alarmonoff" type="checkbox" <?php alarmonoffcheck(); ?>>
</div>
</div>
</div>
<div class="control-group row">
<label class="switch-label col-sm-5 control-label" for="mailnotifications">Email Notifications</label>
<div class="col-sm-6">
<div class="switch-wrapper">
<input onChange="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);" id="mailnotifications" name="mailnotifications" type="checkbox" <?php emailnotifcheck(); ?>>
</div>
</div>
</div>
<div class="control-group row">
<label class="switch-label col-sm-5 control-label" for="smsnotifications">SMS Notifications</label>
<div class="col-sm-6 switch-master-wrapper">
<div class="switch-wrapper">
<input onChange="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);" id="smsnotifications" name="smsnotifications" type="checkbox" <?php smsnotifcheck(); ?>>
</div>
</div>
</div>
</form>
</section>
<script type="text/javascript">
$("input#alarmonoff").switchButton();
$("input#mailnotifications").switchButton();
$("input#smsnotifications").switchButton();
</script>
<section id="profile" class="tab-pane fade">
<h2>Your profile</h2>
<form class="form-horizontal" role="form">
<div class="row">
<div class="form-group">
<div class="col-sm-4">
<label for="firstname" class="control-label">First name</label>
<input type="text" class="form-control" id="firstname" value="<?php echo($firstname); ?>" onkeypress="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);">
<label for="lastname" class="control-label">Last name</label>
<input type="text" class="form-control" id="lastname" value="<?php echo($lastname); ?>" onkeypress="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);">
</div>
<div id="profilepiccontainer" class="col-sm-4">
<label class="filebutton" style="background-image: url('noprofilepic.png'), url('overlay.png'); background-size: cover;">
<span><input type="file" id="profilepicbrowser" name="profilepic"></span>
</label>
<!-- <img id="profilepic" class="img-thumbnail" src="noprofilepic.png" width="125px" /> -->
</div>
<!-- <div class="col-sm-4">
<span class="btn btn-default btn-file">
Browse <input type="file">
</span>
</div> -->
</div>
</div>
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" value="<?php echo($username); ?>" onkeypress="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" value="<?php echo($email); ?>" onkeypress="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone number</label>
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" value="<?php echo($phone); ?>" onkeypress="settingsUpdater(alarmonoff,mailnotifications,smsnotifications,firstname,lastname,username,email,phone);">
</div>
</div>
</form>
</section>
<section id="detection" class="tab-pane fade">
<h1>Detection settings</h1>
</section>
<section id="network" class="tab-pane fade">
<h1>Detection tab</h1>
</section>
</section>
</div>
</div>
</section>
我一直在谷歌搜索和修改我的代码一段时间试图找到答案,但我仍然不明白这个错误。
非常热烈地感谢那些能够找到它来自哪里的人!