将数据从javascript发布到php

时间:2014-11-24 12:46:56

标签: javascript php html ajax

我的网页上有两个下拉列表,我选择了经理和项目。

如果我选择任何经理,那么我想获得分配给特定经理的项目。

我想如果没有javascript和ajax的帮助,我就无法做到这一点。

所以我将经理的选定值传递给javascript文件,然后我再次使用ajax发布了该值。但似乎代码无效。

这是我的PHP代码。

<form method="post" action="<?php $_PHP_SELF ?>">
    Select Manager <select id='managed' name="managed" onchange="getManager()">
                       <option value="">---select---</option>
<?php 
    $conn=mysqli_connect('localhost','root','root','projmanagement'); 
    $result=mysqli_query($conn,'SELECT manager_id,manager_name FROM manager'); 
    while($row=mysqli_fetch_assoc($result)) { 
        echo "<option value='$row[manager_id]'>$row[manager_name]</option>"; 
    } 
?>
                    </select>
            Select Project <select name="projectsd">
                               <option value="">---select---</option>
<?php          
    $temp = $_POST['managed'];
    var_dump($temp);
    die();
    $result1=mysqli_query($conn,'SELECT project_id,project_name FROM project inner join manager on project.m_id=$temp'); 
    while($row1=mysqli_fetch_assoc($result1)) { 
        echo "<option value='$row1[project_id]'>$row1[project_name]</option>"; 
    } 
?> 

这是我的javascript代码。

function getManager() {
    var myvar=document.getElementById('managed').value;

    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("managed").innerHTML = xhr.responseText;
        }
    }
    xmlhttp.open("POST","TaskMaster.php",true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("managed=" + myvar);
}

我是php和javascript的初学者。

请给我一个解决问题的想法。

1 个答案:

答案 0 :(得分:0)

试试这个:

<form method="post" action="<?php $_PHP_SELF ?>">
    Select Manager <select id='managed' name="managed" onchange="getManager()">

        <option value="">---select---</option>
<?php 
        $conn=mysqli_connect('localhost','root','root','projmanagement'); 
        $result=mysqli_query($conn,'SELECT manager_id,manager_name FROM manager'); 
        while($row=mysqli_fetch_assoc($result)) { 
            echo "<option value='$row[manager_id]'>$row[manager_name]</option>"; 
        } 
        ?> </select>
        Select Project <div id="project_container"><select name="projectsd"></div>

JS:

function getManager()
{
var myvar=document.getElementById('managed').value;

   var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {

   document.getElementById("project_container").innerHTML = xhr.responseText;
 }
 }
 xmlhttp.open("POST","TaskMaster.php",true);
 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xhr.send("managed=" + myvar);
   }