ajaxSubmit和serialize的问题

时间:2013-08-19 19:45:47

标签: jquery ajax forms submit

我在使用ajax将textarea内容发布到我的数据库时遇到问题。我试图使用malsap的jquery.form.js插件,以及好的fashon ajax,并且无法将值发布到我的textareas中。继承我的代码:

HTML:

 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
 <script type="text/javascript" src="javascript/jquery.form.js"></script> 
    <form id="defaultemail-ebook-form">
        <label for="defaultemail-ebook">Email with ebook:</label><br>
        <textarea id="defaultemail-ebook" name="defaultemail-ebook"></textarea>
        <br>
        <div id="defaultemail-ebook-submit-div">
            <input type="button" id="defaultemail-ebook-submit" value="Set Email">
        </div>
    </form>

defaultemails.php

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$defaultemailebook = $_REQUEST['defaultemail-ebook'];

if (isset($_GET['defaultemail-ebook'])) {
mysql_query("UPDATE default_emails SET email_ebook='". $defaultemailebook ."' WHERE id = '1'");

echo $defaultemailebook;
}

$defaultemailnoebook = $_REQUEST['defaultemail-no-ebook'];

if (isset($_GET['defaultemail-no-ebook'])) {
mysql_query("UPDATE default_emails SET email_no_ebook='". $defaultemailnoebook ."' WHERE id = '1'");

echo $defaultemailnoebook;
}

?>

AJAX:

$(function() {

$("#defaultemail-ebook-submit").click(function(){
var data = $('#defaultemail-ebook-form').serialize();

$.ajax({
url: "defaultemails.php",
type: "POST",
data: data,
success: function() {                        
  alert("Yay");       
  }
}); 
});
});

使用.ajaxSubmit

$(function() { 
$( "#defaultemail-no-ebook-submit" ).click(function() {
    $("#defaultemail-no-ebook-form").ajaxSubmit({url: 'defaultemails.php', type: 'post'}).delay(100, function() {
        $("#current-no-ebook-message").text($("#defaultemail-no-ebook").val());
        alert("Email without ebook had been set!");
    });
});
});

defaultemails.php会在用它后面的尾随名称访问时更新现有的字段文本(defaultemails.php?defaultemail-ebook = Here + is + your + ebook),所以我不认为它是php文件。似乎由于某种原因,当我使用ajax调用时,没有提交defaultemail-ebook的价值。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您的ajax电话正在使用POST。在您的php文件中,您正在检查是否设置了$_GET['defaultemail-ebook']。将其更改为$_POST['defaultemail-ebook']