更新:它在Firefox中不起作用,但可以在任何其他浏览器上使用。我甚至尝试在安全模式下加载Firefox(禁用所有插件等),但仍然没有用。 :(
我正在尝试将AJAX帖子(在表单提交上)发送到单独的PHP文件,该文件工作正常,而不尝试通过帖子发送电子邮件地址。我是AJAX的新手,非常熟悉PHP。这是我的表格和ajax电话
<form class="form" method="POST" name="settingsNotificationsForm">
<div class="clearfix">
<label>Email <em>*</em><small>A valid email address</small></label><input type="email" required="required" name="email" id="email" />
</div>
<div class="clearfix">
<label>Email Notification<small>...when a new subscriber joins</small></label><input type="checkbox" name="subscribe_notifications" id="subscribe_notifications"> Receive an email notification with phone number when someone new subscribes to 'BIZDEMO'
</div>
<div class="clearfix">
<label>Email Notification<small>...when a subscriber cancels</small></label><input type="checkbox" name="unsubscribe_notifications" id="unsubscribe_notifications"> Receive an email notification with phone number when someone new unsubscribes to 'BIZDEMO'
</div>
<div class="action clearfix top-margin">
<button class="button button-gray" type="submit" id="notifications_submit"><span class="accept"></span>Save</button>
</div>
</form>
和AJAX调用:
<script type="text/javascript">
jQuery(document).ready(function () {
$("#notifications_submit").click(function() {
var keyword_value = '<?php echo $keyword; ?>';
var email_address = $("input#email").val();
var subscribe_notifications_value = $("input#subscribe_notifications").attr('checked');
var unsubscribe_notifications_value = $("input#unsubscribe_notifications").attr('checked');
var data_values = {
keyword : keyword_value,
email : email_address,
subscribe_notifications : subscribe_notifications_value,
unsubscribe_notifications : unsubscribe_notifications_value
};
$.ajax({
type: "POST",
url: "../includes/ajax/update_settings.php",
data: data_values,
success: alert('Settings updated successfully!'),
});
});
});
和接收页面:
<?php
include_once ("../db/db_connect.php");
$keyword = FILTER_INPUT(INPUT_POST, 'keyword' ,FILTER_SANITIZE_STRING);
$email = FILTER_INPUT(INPUT_POST, 'email' ,FILTER_SANITIZE_EMAIL);
$subscribe_notifications = FILTER_INPUT(INPUT_POST, 'subscribe_notifications' ,FILTER_SANITIZE_STRING);
$unsubscribe_notifications = FILTER_INPUT(INPUT_POST, 'unsubscribe_notifications' ,FILTER_SANITIZE_STRING);
$table = 'keyword_options';
$data_values = array('email' => $email, 'sub_notify' => $subscribe_notifications, 'unsub_notify' => $unsubscribe_notifications);
foreach ($data_values as $name=>$value)
{
// See if keyword is already in database table
$filter = array('keyword' => $keyword);
$result = $db->find($table, $filter);
if (count($result) > 0 && $new != true)
{
$where = array('keyword' => $keyword, 'keyword_meta' => $name);
$data = array('keyword_value' => $value);
$db->update($table, $where, $data);
}
else
{
$data = array('keyword' => $keyword, 'keyword_meta' => $name, 'keyword_value' => $value);
$db->create($table, $data);
$new = true; // If this is a new record, always go to else statement
}
}
unset($value);
以下是一些奇怪的事情:
想法?
答案 0 :(得分:0)
如果是我,我首先通过传递序列化形式而不是单个变量来简化事物。这是微不足道的,但是再次设置所有这些不同的变量单独进行确实留下了很大的错误空间。要做到这一点,只需这样做:
$.ajax({
...
data:$('#formid').serialize(),
...
});
请注意,serialize根据输入字段的名称变量发送。确保根据需要进行设置。
旧版jQuery中的There is a known bug with the @ symbol。如果你落伍,版本控制也可以解决问题。
答案 1 :(得分:0)
尝试转发电子邮件地址。 http://www.w3schools.com/jsref/jsref_escape.asp
我知道你正在使用类型:“POST”,但让我们看看。
答案 2 :(得分:0)
我最终使用来自不同网站的教程并从头开始重建...下面的jquery代码是有用的。
jQuery(document).ready(function () {
$("#settingsNotificationsForm").submit(function() {
//setup variables
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method');
/* responseMsg = $('#signup-response') */
//send data to server
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
success:function(data){
alert('Settings updated successfully!');
}
})
//prevent form from submitting
return false;
});
});
答案 3 :(得分:0)
我也有同样的问题..你需要在提交按钮上使用preventDefault()。 然后继续发布。
$("notifications_submit").click(function(ev){
ev.preventDefault();
//form variables
//post via ajax
}
);