[英]PHP include using jquery AJAX from a dropdown select option?
I am trying to include an external php (on my own server) in another php page using jQuery AJAX
from a select form
. 我试图使用jQuery AJAX
从select form
在另一个PHP页面中包含一个外部php(在我自己的服务器上)。
basically I want to let the user include the external php
file if they select YES
option from the dropdown select form
and remove it if they select NO
. 基本上,我想让用户包括external php
文件,如果他们从dropdown select form
选择YES
,而如果选择“ NO
则将其删除。
I have the following code: 我有以下代码:
<html>
<head>
<script>
$('#test2').change(function(){
var selectedValue = $(this).val();
if (selectedValue === 'Yes') {
$("#content").load("include.php");
} else {
//WHAT DO I NEED TO PUT HERE TO REMOVE THE "include.php" if it has been inlcluded?
}
});
</script>
</head>
<body>
<form>
<select id="test2">
<option value=""></option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</form>
<br>
<div id="content"></div>
</body>
</html>
First question: am I doing this right? 第一个问题:我这样做正确吗?
Second question: what do I need to do in order to remove the include.php
file if they select NO option
? 第二个问题:如果他们选择“ NO option
我需要怎么做才能删除include.php
文件?
any help would be appreciated. 任何帮助,将不胜感激。
Thanks in advance. 提前致谢。
PS i do have jquery included in my page. 附言:我的页面中确实包含了jquery。
The $('#comment').html('')
in the else block are right. else块中的$('#comment').html('')
是正确的。 However, your if-condition is wrong ( .val()
returns the value not the text of the chosen option). 但是,您的if条件错误( .val()
返回的值不是所选选项的文本)。 Use: 采用:
$('#test2').change(function(){
var selectedValue = $(this).val();
if (1 == selectedValue) {
$("#content").load("include.php");
} else {
$('#content').html('');
}
});
Your code looks like it's requesting include.php just fine. 您的代码看起来就像请求include.php一样。 You can clear the loaded data by calling 您可以通过调用清除加载的数据
$('#content').html('');
in the else
section 在else
部分
If yes is selected then
Include file
else
$( ".page" ).empty();
//make the page content empty Simple
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.