将selectbox值发送到php变量,无需重新加载页面
我通过阅读一些在线教程来学习AJAX,所以请理解我对AJAX和编程非常陌生。我已成功使用3个选择框执行以下操作:将selectbox值发送到php变量,无需重新加载页面,php,jquery,ajax,forms,Php,Jquery,Ajax,Forms,我通过阅读一些在线教程来学习AJAX,所以请理解我对AJAX和编程非常陌生。我已成功使用3个选择框执行以下操作: 根据选择框1中的选择填充选择框2 根据选择框2中的选择填充选择框3 一切都运转得很好 这是我的密码: $(document).ready(function() { $(".sport").change(function() { var id=$(this).val(); var dataString = 'id='+ id; $.ajax
- 根据选择框1中的选择填充选择框2
- 根据选择框2中的选择填充选择框3
$(document).ready(function()
{
$(".sport").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_sport.php",
dataType : 'html',
data: dataString,
cache: false,
success: function(html)
{
$(".tournament").html(html);
}
});
});
$(".tournament").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_round.php",
data: dataString,
cache: false,
success: function(html)
{
$(".round").html(html);
}
});
});
});
</script>
然而,正如我上面提到的,我的代码是有缺陷的
如果有人能帮助我解释如何在不重新加载表单的情况下将表单数据发送到3个php变量将不胜感激如果您不想在单击按钮时提交表单,您需要将该输入设置为
按钮
而不是提交
。您还可以将submit
事件处理程序附加到表单并阻止其提交:
$("form").on("submit", function(e){
e.preventDefault(); //This is one option
return false; //This is another option (and return true if you want to submit it).
});
所以,既然这样说,你可能会这样做:
$("form").on("submit", function(e) {
var formData = $(this).serialize();
e.preventDefault();
$.ajax({
url: 'yoururl',
data: formData,
type: 'post', //Based on what you have in your backend side
success: function(data) {
//Whatever you want to do once the server returns a success response
}
});
});
在您的后端:
if (isset($_POST["sport"])) {
//Do something with sport
}
if (isset($_POST["tournament"])) {
//Do something with torunament
}
echo "Successfull response!"; //You have to "write" something in your response and that is what the frontend is going to receive.
希望这有帮助 如果您不想在单击按钮时提交表单,则需要将该输入设置为
按钮
,而不是提交
。您还可以将submit
事件处理程序附加到表单并阻止其提交:
$("form").on("submit", function(e){
e.preventDefault(); //This is one option
return false; //This is another option (and return true if you want to submit it).
});
所以,既然这样说,你可能会这样做:
$("form").on("submit", function(e) {
var formData = $(this).serialize();
e.preventDefault();
$.ajax({
url: 'yoururl',
data: formData,
type: 'post', //Based on what you have in your backend side
success: function(data) {
//Whatever you want to do once the server returns a success response
}
});
});
在您的后端:
if (isset($_POST["sport"])) {
//Do something with sport
}
if (isset($_POST["tournament"])) {
//Do something with torunament
}
echo "Successfull response!"; //You have to "write" something in your response and that is what the frontend is going to receive.
希望这有帮助 尝试使用javascript函数
请参阅。尝试使用javascript函数 请参阅。使用
Submit
元素而不是
,因为提交会自动提交表单
编辑:您必须在上使用('click'),而不是在jQuery中查找提交事件。使用submit
元素而不是
,因为提交会自动提交表单
编辑:您必须在上使用('click'),而不是在jQuery中查找提交事件