我有一个动态创建的休眠列表,用户可以在其中选择时事通讯。我正试图让它正常工作,所以当您从下拉列表中选择某个内容时,将执行一个php文件。我的下拉列表如下:
<?php
echo "<select id=\"NieuwsbriefSelect\" name=\"show\" onchange=\"what do I do here??? \">";
echo "<option size =30 selected>Select</option>";
if(mysql_num_rows($sql_result))
{
while($row = mysql_fetch_assoc($sql_result))
{
echo "<option value=\"$row[Titel]\">$row[Titel]</option>";
}
}
else {
echo "<option>No Names Present</option>";
}
?>我的php文件如下所示:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
<?php
$title = $_REQUEST["show"];
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$strSQL = "SELECT Content from NAW.Mail where Titel = '".$title."' ";
$sql_result = mysql_query($strSQL);
$row = mysql_fetch_assoc($sql_result);
$file = 'nieuwsbrief.txt';
$current = urldecode($row["Content"]);
file_put_contents($file, $current);
?>目前,这是:$title = $_REQUEST" show ";不工作,并给出了错误的未定义的索引:显示。所以我有两个基本问题:
-How --当我选择时事通讯时,我会得到要执行的php文件吗?
-How我是否根据我选定的时事通讯从我的数据库中获得正确的数据?
我尝试了几个例子,我在其他论坛等,但由于我几乎没有经验的javascript,我无法得到任何工作。如果有人能给我举个例子,或者把我推向正确的方向,那就太好了!如果你有任何其他问题,只需问他们作为一个评论!
,注意,,我知道mysql_*是不推荐的,一旦我完成了所有的工作,我就会改为PDO!
发布于 2013-04-04 08:45:09
在这一行
echo "<select id=\"NieuwsbriefSelect\" name=\"show\" onchange=\"what do I do here??? \">";您应该在JavaScript属性中放置一个onchange=""代码。
例如,您可以调用一个函数,让我们将其命名为changeSelect(),它将(我猜)加载php文件的内容。您可以通过Ajax实现这一点。我不会解释更多,因为这个问题非常广泛,你需要更多的研究。
如果您已经更改为PDO或mysqli,我建议您通过titel (而不是$_REQUEST)传递该变量。例如,changeSelect()函数可以通过"myfile.php?titel="+document.getElementById("NieuwsbriefSelect").value或类似的方法调用php文件(例如:"myfile.php")。
发布于 2013-04-04 09:30:44
使用ajax的简单方法。
<?php
echo "<select id=\"NieuwsbriefSelect\" name=\"show\" onchange='hello(value)'>";
echo "<option size =30 selected>Select</option>";
if(mysql_num_rows($sql_result))
{
while($row = mysql_fetch_assoc($sql_result))
{
echo "<option value=\"$row[Titel]\">$row[Titel]</option>";
}
}
else {
echo "<option>No Names Present</option>";
}
?>,而javascript文件如下所示
function hello(value)
{
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
//nothing is here because you want to put the result in file
}
}
xmlhttp.open("GET","yourphpcode.php?val="+value,true);
xmlhttp.send();
}尝尝这个
对不起,如果我没有运行代码,出了什么问题
https://stackoverflow.com/questions/15806115
复制相似问题