我的ajax正在工作,但php给出了undefine变量错误

时间:2013-03-31 04:45:47

标签: php javascript mysql ajax

下面是我的ajax代码 我通过这种方式向javascript发送价值

<select id="ds" onchange="myf3(this.value)">
<option value=000>Select branch</option>
<?php
    mysql_connect("localhost","root","mydata");
    mysql_select_db("pbank");
    $q="select * from branch order by branch_name";
    $rs=mysql_query($q);
    for($i=0;$i<mysql_num_rows($rs);$i++)
    {
        $rd=mysql_fetch_object($rs);
        echo"<option value='$rd->branch_code'>$rd->branch_name</option>";
    }
?>
</select>
<span id="reslt"></span>

和javascript通过这种方式接收值

<script language="javascript">
function myf3(str)
{
if (str=="")
  {
  document.getElementById("reslt").innerHTML="";
  return;
  }
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("reslt").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","city.php?q="+str,true);
xmlhttp.send();
document.getElementById('moon').style.display="none";
}
</script> 

并显示此结果

<?php
 mysql_connect("localhost","root","mydata");
        mysql_select_db("pbank");

    if(isset($_GET["q"]))
    {
    $my_q = $_GET['q'];
    $q="select * from product_cat where branch_code='$my_q'";
    $rs=mysql_query($q);
?>
    <select id="res" name="cat_code" >
    <option value=0>Select category</option>
<?php
        for($i=0;$i<mysql_num_rows($rs);$i++)
        {
            $rd=mysql_fetch_object($rs);

            </option>";
?>
            <option value=" <?php echo $rd->id; ?> "> <?php echo $rd->cat_name; ?> </option>

<?php
        }
?>
        </select>
<?php

    }
?>

但是当我试图在数据库中存储值时,它给出了未定义的变量错误 查询数据库中的商店价值低于

db_connect();

            $cat_name = $_POST['cat_code'];
                $name_of_branch = $_POST['brand_branch_code'];
                $name_of_brand = $_POST['brand_name'];
                $g ="insert into product_brand (parent_id,brand_name,category,branch_code) values               ('$cat_name','$name_of_brand','$cat_name','$name_of_branch')";
                    if(mysql_query($g))
                    {
                        echo "hello";   
                    }
                    else
                    {
                        mysql_error();
                    }

如何解决,请帮帮我

1 个答案:

答案 0 :(得分:1)

也许这就是问题...

if(isset(**$_GET[**'get']))
            {
            $cat_name = **$_POST[**'cat_code'];
            echo $cat_name;