简体   繁体   中英

Execute a php file with javascript onchange

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!

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.

The simple way to use 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

   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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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