doureng1083 2013-04-04 08:28
浏览 81

使用javascript onchange执行php文件

I have a dynamically created dorpdown list where users can select newsletters. I am trying to get it to work so when you select something from the dropdown list then a php file gets executed. 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 
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"]; 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?
-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. 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!

  • 写回答

2条回答 默认 最新

  • doulu2591 2013-04-04 08:45
    关注

    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.

    For example, you can call a function, let's name it changeSelect() that will (I guess) load contents of the php file. You can do this by 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). 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据