AJAX POST& PHP POST在同一页面 [英] AJAX POST & PHP POST In Same Page
问题描述
我有2个分别为drpcategory
和drpitem
的下拉列表,如下所示;
I have 2 dropdowns called drpcategory
and drpitem
as below;
<div class="form-group">
<label>Category</label>
<select class="form-control bg-dark btn-dark text-white" id="drpcategory" name="drpcategory" required>
<?php
$category = ''.$dir.'/template/post/category.txt';
$category = file($category, FILE_IGNORE_NEW_LINES);
foreach($category as $category)
{
echo "<option value='".$category."'>$category</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Item</label>
<select class="form-control bg-dark btn-dark text-white" id="drpitem" name="drpitem">
<?php
$category = $_POST["drpcategory"] ?? 'Electronics';
$item = ''.$dir.'/template/post/'.$category.'/item.txt';
$item = file($item, FILE_IGNORE_NEW_LINES);
foreach($item as $item)
{
echo "<option value='".$item."'>$item</option>";
}
?>
</select>
</div>
如您所见,根据选定的drpcategory
值,应动态填充drpitem
.
As you can see, depending on the selected value of drpcategory
, the drpitem
should be dynamically populated.
如果您想知道,如果我没有进行任何post
检查的情况下手动设置$category
,则两个下拉列表都将经过PHP循环并填充.
In case you were wondering, both dropdowns will go through the PHP loop and populate if I set $category
manually without any post
checks.
现在,我正在使用AJAX post
将drpcategory
中的更改发布到下面的同一页面中;
Now, I'm using AJAX post
to post the changes in drpcategory
into the same page as below;
<script>
$(function(){
$('#drpcategory').on('change',function()
{
$.ajax({
method: 'post',
data: $(this).serialize(),
success: function(data)
{
alert(data);
}
});
});
});
</script>
以下是上述AJAX post
的 Chrome 浏览器> 检查> 网络选项卡输出;
Here is the Chrome Browser > Inspect > Network tab output for the above AJAX post
;
是的,我正在post
将此AJAX放入同一页面,并且url
是:http://example.com/?page=post
,与此网络"选项卡显示的相同.
And yes, I'm post
ing this AJAX into the same page and the url
is: http://example.com/?page=post
which is the same as this Network tab shows.
我已经从AJAX函数中删除了url
字段,因为浏览器会自动选择当前页面,因此更好,而且不行,在其中手动编写任何url
并不会改变我要更改的内容在下面问.
I have removed the url
field from AJAX function because the browser automatically picks up the current page so it's better, and no, manually writing any url
in there didn't change anything about what I'm about to ask below.
问题是,我怎样才能使drpitem
提取AJAX post
ed drpcategory
值并开始动态填充?
Question is, how can I make that drpitem
to pickup the AJAX post
ed drpcategory
value and start populating dynamically?
我想AJAX post
到同一页面,并且所有这些都应该在不重新加载页面的情况下发生.
I want to AJAX post
to the same page and all of this should happen without a page reload.
推荐答案
最后,在等待7天以上以解决此问题后,我无法AJAX post
到同一页面,可能是因为<html>
元素已经在同一页面中.但是,这里使用外部post.php
文件.除了上面我最初的问题中提到的以外,我仅需要进行这些更改即可完成此工作.
Finally after waiting more than 7 days trying to figure this out, I couldn't AJAX post
to the same page probably because there are too much of <html>
elements already in the same page. But here goes it using an external post.php
file. Only these changes were required for me to get this working other than mentioned in my original question above.
<script>
$(function()
{
$('#drpcategory').change(function()
{
$.ajax({
method: 'post',
url: 'post.php',
data: $(this).serialize(),
success: function(data)
{
$('#drpitem').html(data);
}
});
});
});
</script>
然后,我必须将此post.php
保存在index.php
所在的根网站目录中.根据需要更改目录,并且还记得更改上述AJAX函数中的url
字段.
Then I had to save this post.php
in the root website directory where index.php
is located. Change your directory as necessary and also remember to change the url
field in the above AJAX function.
<?php include 'config.php';?> //this is where the $dir variable is derived in my website. i had to include this since post.php is an external file that doesn't know about the variable.
<?php
$category = $_POST["drpcategory"];
$item = ''.$dir.'/template/post/'.$category.'/item.txt';
$item = file($item, FILE_IGNORE_NEW_LINES);
foreach($item as $item)
{
echo "<option value='".$item."'>$item</option>";
}
?>
感谢你们中的每个人都引导我完成此任务.如果给出直接答案,我会标记您中的某个人为答案,但是如果有人已经给出了答案,对不起您未能正确理解您.但是我认为最终目标是让某人以此作为帮助.
Thank you for each and everyone of you for walking me through this. I would have marked someone of you as the answer if a direct answer was given but if someone already given this, I'm sorry for not understanding you properly. But I think the end goal is for someone to use this as help.
这篇关于AJAX POST& PHP POST在同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!