我可以通过表格数据/变量使用AJAX在jQuery脚本到PHP? [英] Can I pass form data/variables to php in jQuery script using AJAX?
问题描述
我有一个将数据传递到同一页上的jQuery脚本的形式。形式是这样的:
I have a form which passes data to a jQuery script on the same page. The form looks like this:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="radio" name="favourites" value="all" checked="checked"> all tracks   <input type="radio" name ="favourites" value="favourites"> favourites only<br><br>
<input type="submit" name="submit" id="filter" class="submit" value="filter"><br>
</form>
有关收藏的价值,然后通过PHP在同一个页面上,动态地生成一个MySQL查询的头检索中的jQuery脚本。但是,这是因为在页面重新加载。如果可能的话,我想通过表单数据/变量的jQuery脚本无需重新加载页面,使用AJAX。我知道,我可以操纵HTML和CSS使用AJAX,但我也可以操纵头的jQuery脚本?如果是的话,我将如何着手呢?
The value for favourites is then retrieved by PHP within a jQuery script in the head of the same page, which dynamically generates a MySQL query. But this happens because the page is reloaded. If possible I would like to pass the form data/variables to the jQuery script without reloading the page, using AJAX. I'm aware that I can manipulate html and css using AJAX, but can I also manipulate the jQuery script in the head? If so, how would I go about this?
推荐答案
看看这 。 code会去这样的事情。
Take a look at this. Code would go something like this.
HTML:
<form id="postForm" method="POST">
<input type="radio" name="filter" value="1" /> Only favorites
<input type="radio" name="filter" value="2" /> Everything
<input type="submit" name="postFormSubmit" />
</form>
JavaScript的:
Javascript:
jQuery("#postForm").submit(function(event){
event.preventDefault();
jQuery.post("/ajax.php", jQuery("#postForm").serialize(), function(){
//done
});
}
这篇关于我可以通过表格数据/变量使用AJAX在jQuery脚本到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!