Javascript 在Ajax中,我想使用;“选定选项”;作为另一个php文件中的文件名
首先,让我们看看我的html代码Javascript 在Ajax中,我想使用;“选定选项”;作为另一个php文件中的文件名,javascript,php,Javascript,Php,首先,让我们看看我的html代码 <select name="lang" id="lang"> <option value="ko">한국어</option> <option value="en">English</option> <option value="af">Afrikaans</option> </select> <form> <inp
<select name="lang" id="lang">
<option value="ko">한국어</option>
<option value="en">English</option>
<option value="af">Afrikaans</option>
</select>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
한국어
英语
南非荷兰语
下面是javascript代码
<script>
function showResult(str)
{
if (str.length==0)
{
.....
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
....
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
函数显示结果(str)
{
如果(str.length==0)
{
.....
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
....
}
}
open(“GET”,“livesearch.php?q=“+str,true”);
xmlhttp.send();
}
上面的脚本代码,有livesearch.php?。。。
下面是livesearch.php代码
<?php
$file=$_GET['name'];
$xmlDoc=new DOMDocument();
$xmlDoc->load("ds_".$file.".xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1)
load(“ds_u.$file..xml”);
$x=$xmlDoc->getElementsByTagName('link');
//从URL获取q参数
$q=$_GET[“q”];
//如果q的长度>0,则从xml文件中查找所有链接
如果(斯特伦($q)>0)
{
$hint=“”;
对于($i=0;$i长度);$i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
如果($y->item(0)->nodeType==1)
-->>我可以使用选择选项html代码中的“$file”变量吗??
否则,另一种方式??
请帮助我???这应该很容易,假设您的PHP是正确的:
<?php
// function for input validation
// returns only valid input, defaults to 'en' when given invalid input
function validate($str) {
if (in_array($str, array('en', 'ko', 'af', 'etc.'))) {
return $str;
}
return 'en'; // default case for invalid input
}
// create DOMDocument
$xmlDoc=new DOMDocument();
// check if the URL parameter was set
if (isset($_GET['q'])) {
// just add the validated input string in here
$xmlDoc->load('ds_' . validate($_GET['q']) . '.php .xml');
} else {
// invalid URL parameter
die('error');
}
$x=$xmlDoc->getElementsByTagName('link');
load('ds_'.validate($_GET['q'])。.php.xml');
}否则{
//无效的URL参数
死亡(“错误”);
}
$x=$xmlDoc->getElementsByTagName('link');
澄清一下:如果您想从URL参数中输入,请使用
$\u GET
,这是一个数组。我不清楚您想要什么,但听起来好像您想让值成为文件名。您需要获取ajax请求的值。这里只是一个示例。
<?php
$file = $_GET["q"];
//you can use $file wherever you want.
$xmlDoc=new DOMDocument();
$xmlDoc->load("ds_".$file.".php .xml");
$x=$xmlDoc->getElementsByTagName('link');
load(“ds_uz.$file..php.xml”);
$x=$xmlDoc->getElementsByTagName('link');
不太确定这里发生了什么…你只是粘贴了代码,忘记问问题了吗?你的相关php代码在哪里?我看不到你在哪里收到值。例如$\u GET?对不起!最后一句话被省略了。我想在html代码中选择一个选项,该选项在具有ds\u en.xml的livesearch.php文件中处于活动状态(not.php--对不起!!)。在livesearch.html代码“q”是livesearch输入类型中,“en”可以是一个选中的选项值吗?我想要的是选中的选项值(->这里,“lang”是一个变量。在livesearch.php中单击“Select option”时,ds.$file..xml可以更改吗?“q”是livesearch输入类型。我想要的是选中的选项值(->这里,'lang'是一个变量。在选择选项上单击livesearch.php中的ds_389;“.$file.”.xml是否可以更改?