开发者

Submitting HTML Form with AJAX

I've got a form:

<form action="#" method="post" id="hubForm">
    <label class="labelText">Expiration Date:</label>
    <input class="datBox" type="text" id="cal" name="date">
    <div class="clr"></div>
    <div class="clr"></div>
    <label class="labelText">Hub Name</label>
    <input class="inputTxt" type="text" value="" name="name" />
    <div class="clr"></div>
    <label class="labelText">Description</label>
    <textarea class="textArea" name="desc"></textarea>
    <div class="clr"></div>
    <input class="submitButt" type="submit" value="" />
    <div class="clr"></div>
</form>

And I've got this JavaScript:

$('#hubForm').submit(function() {
    $.ajax({
        url: "hubControl.php",
        type: "post",
        data: $(this).serialize()
    });
});

And this PHP code:

//Add the hub, task, or project to the database
mysql_query("INSERT INTO ".$type."s(".$IDvar.", name, description, users, expDate)
VALUES(".$ID.", \"".$_POST['title']."\", \"".$_POST['desc']."\", ".$ID.", ".$_POST['date'].")");

But it refuses to add anything to the database. I've rewritten the code to use $_GET and tested the PHP and I've verifie开发者_StackOverflowd that it works.

As for the JavaScript, I've also tried stuff like

$.post("hubControl.php", $('#hubForm').serialize());

but nothing seems to work. Any ideas?

EDIT: The form HTML was given to me by someone else, so I don't know if it's 100% compatible with the AJAX/JavaScript with stuff like the method="post". You will all have a better idea than I would.


If it works with $_GET and not $_POST, it sounds like the $_POST array is not being filled correctly.

I would recommend you start working through this by viewing your $_REQUEST or $_POST array when the page is loaded. print_r($_POST) in some HTML pre tags can be very useful.

If you know that the PHP isn't even receiving the correct data, you can focus on fixing the Ajax/jQuery.


Since you're using jQuery, you should consider using the ajaxForm plugin. It's very easy to use, you can just drop it in to your project with minimal setup.


Try to use uppercase type:

$.ajax({
    url: "hubControl.php",
    type: "POST",
    data: $(this).serialize()
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜