简体   繁体   English

使用javascript onchange执行php文件

[英]Execute a php file with javascript onchange

I have a dynamically created dorpdown list where users can select newsletters. 我有一个动态创建的dorpdown列表,用户可以在其中选择新闻稿。 I am trying to get it to work so when you select something from the dropdown list then a php file gets executed. 我正在尝试使其工作,因此当您从下拉列表中选择某些内容时,将执行php文件。 My dropdown list looks like this: 我的下拉列表如下所示:

<?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>";  
} 
?>

And my php file looks like this: 和我的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);
?>

Currently this: $title = $_REQUEST["show"]; 当前是这样的:$ title = $ _REQUEST [“ show”]; is not working and gives the error Undefined index: show. 无法正常工作,并给出错误未定义索引:显示。 So basicly I have 2 questions: 所以基本上我有两个问题:
-How do I get the php file to execute when I select a newsletter? -选择简讯后,如何执行php文件?
-How do I the right data from my DB according to my selected newsletter? -如何根据我选择的新闻通讯从数据库中获取正确的数据?

I have tried several things with examples I found on other forums etc but since I have almost no experience with javascript I can't get anything to work. 我已经尝试了一些在其他论坛等地方找到的示例的方法,但是由于我几乎没有使用javascript的经验,因此无法进行任何工作。 If anyone could give me an example or push me in the right direction it would be great! 如果有人能给我一个例子或向正确的方向推我,那将是很棒的! If you have any other questions just ask them as a comment! 如果您还有其他问题,请以评论为准!

NOTE I know that mysql_* is deprecated and I will change to PDO once I get everything to work! 注意我知道mysql_ *已过时,一旦一切正常,我将更改为PDO!

In this line 在这条线

echo "<select id=\"NieuwsbriefSelect\" name=\"show\" onchange=\"what do I do here???  \">";

you should put a JavaScript code in the onchange="" attribute. 您应该将JavaScript代码放在onchange=""属性中。

For example, you can call a function, let's name it changeSelect() that will (I guess) load contents of the php file. 例如,您可以调用一个函数,将其命名为changeSelect() ,它将(我认为)加载php文件的内容。 You can do this by Ajax. 您可以通过Ajax完成。 I won't explain more because the subject is very wide and you need more researching. 我将不做更多解释,因为该主题涉及面很广,您需要进行更多研究。

I propose, if you would have changed to PDO or mysqli, that you pass the titel variable by $_GET (not $_REQUEST ). 我建议,如果您将更改为PDO或mysqli,则可以通过$_GET (而不是$_REQUEST )传递titel变量。 For example, your changeSelect() function could call your php file (say: "myfile.php") by "myfile.php?titel="+document.getElementById("NieuwsbriefSelect").value or something like that. 例如,您的changeSelect()函数可以通过"myfile.php?titel="+document.getElementById("NieuwsbriefSelect").value或类似名称来调用您的php文件(例如:“ myfile.php”)。

The simple way to use ajax. 使用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>";  
} 
?>

than the javascript file goes like this 比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();
}

Try this 尝试这个
Excuse me if something went wrong as i didn't run the code 如果我没有运行代码而出现问题,请问好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM