php中的查询失败,带有get和space

时间:2013-12-19 20:26:37

标签: javascript php mysql get

希望有人可以提供帮助。我有动态下拉工作,直到我添加了一个带有空格的新项目。这让我感到困惑,因为我的第二个下拉列表中包含空格的项目,查询运行正常,但第一个下拉列表失败。我试图找到任何可能导致失败的差异,但我无法看到它。这是我发现的脚本的混合,我是Javascript和PHP的新手,所以我迷路了。我希望有人可以指出我的错误。这是我认为您需要看到的代码第一个脚本是控制第二个脚本工作失败的下拉列表的脚本:

    <script type="text/javascript">
function AjaxFunction()
{

var ddstring = document.getElementById("name");
var ddindex = ddstring.options[ddstring.selectedIndex].value;

if (ddindex != 0){  
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
          try
                    {
                 httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
            catch (e)
                    {
                try
            {
            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
             }
                catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
            }
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {
//alert(httpxml.responseText);
var myarray = JSON.parse(httpxml.responseText);
var myarray=myarray.split(",");
for(j=document.searchform.type.options.length-1;j>=0;j--)
{
document.searchform.type.remove(j);
}
for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");

optn.text = myarray[i];
optn.value = myarray[i];
document.searchform.type.options.add(optn);
} 
{
var optn2 = document.createElement("OPTION");

optn2.text = "Select Type";
optn2.value = '%';
document.searchform.type.options.add(optn2,0);
} 
{
document.searchform.type.disabled=false;
document.searchform.type.selectedIndex = 0;
var defaultdd = "<?php echo $_SESSION['type'] ?>";

if (defaultdd != ""){
document.getElementById('s2').value = defaultdd;
if (defaultdd != "%"){
AjaxFunction2(); 
}

}
}

  }
}
var url="dd.php";
var cat_id=document.getElementById('name').value;
url=url+"?cat_id="+cat_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
//alert(url);
httpxml.open("GET",url,true);
httpxml.send(null);
}

else {
    document.searchform.type.selectedIndex = 0;
    document.searchform.subcat.selectedIndex = 0;
    document.searchform.type.disabled=true;
    document.searchform.subcat.disabled=true;       
}
}
</script>




<script type="text/javascript">
function AjaxFunction2()
{
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
          try
                    {
                 httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
            catch (e)
                    {
                try
            {
            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
             }
                catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
            }
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {
//alert(httpxml.responseText);
var myarray = JSON.parse(httpxml.responseText);
var myarray=myarray.split(",");
for(j=document.searchform.subcat.options.length-1;j>=0;j--)
{
document.searchform.subcat.remove(j);
}


for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.searchform.subcat.options.add(optn);

} 
{
var optn2 = document.createElement("OPTION");

optn2.text = "Select Series";
optn2.value = '%';
document.searchform.subcat.options.add(optn2,0);
}
{
document.searchform.subcat.disabled=false;
document.searchform.subcat.selectedIndex = 0;

var defaultdd = "<?php echo $_SESSION['subcat'] ?>";
if (defaultdd != ""){
document.getElementById('s3').value = defaultdd;
}
}

      }
    }
    var url="dd2.php";
var cat_id=document.getElementById('name').value;
var cat_id2=document.getElementById('s2').value;
url=url+"?cat_id="+cat_id;
url=url+"&cat_id2="+cat_id2;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
//alert(url);
httpxml.open("GET",url,true);
httpxml.send(null);
  }
</script>

表单元素:

<form name="searchform" method='POST' action='?'>
  <table width="534" border="1" cellspacing="10" ID="tdspace">
    <tr>
      <td>Manufacturer: </td>
      <td><select name=name id='name' onchange=AjaxFunction();>
        <option value='%'>Select One</option>
        <?Php
try {
$dbo = new PDO('mysql:host=localhost;dbname='. $dbname, $username, $password);
} 
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

$sql="SELECT Distinct Manufacturer FROM `tbl_Printers` order by `Manufacturer` asc ";
foreach ($dbo->query($sql) as $row) {

        if ($row[Manufacturer] == $_SESSION['name'])

        $selected = "selected=\"selected\"";
    else  
        $selected = "";
echo "<option value=$row[Manufacturer] $selected>$row[Manufacturer]</option>";
if( $_SESSION['type'])
{ echo '<script type="text/javascript">AjaxFunction()</script>';
}
}
?>
        <?php
    if ( $_SESSION['name'] == ""){ $namevalue = 'None Selected';}
    else {$namevalue = $_SESSION['name'];}

    if ( $_SESSION['type'] == "%"){ $typevalue = 'None Selected';}
    else {$typevalue = $_SESSION['type'];}

    if ( $_SESSION['subcat'] == "%"){ $subvalue = 'None Selected';}
    else {$subvalue = $_SESSION['subcat'];}
?>
      </select></td>
      <td>Manufacturer:<span style="color:cyan"> <?php echo $namevalue;?></span></td>
      </tr>
    <tr>
      <td>Product Type: </td>
      <td><select name=type id='s2' onchange=AjaxFunction2(); disabled=true>
        <option value='%' selected>Select Type</option>
      </select></td>
      <td>Product Type:<span style="color:magenta"> <?php echo $typevalue?></span></td>
      </tr>
    <tr>
      <td>Printer Catagory:</td>
      <td><select name=subcat id='s3' disabled=true>
        <option value='%' selected>Select Series</option>
      </select></td>
      <td>Printer Series:<span style="color:yellow"> <?php echo $subvalue;?></span></td>
      </tr>
    <tr>
      <td height="27" colspan="2" ID="centered" style="padding-left:90px"><br />        <input type=submit onclick="s3.disabled=false; s2.disabled=false" class="quick-search-button btn"/></td>
      <td>Records Found:<span style="color:black; text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4)"> <?php echo $_SESSION['totalrecords'];?></span></td>
      </tr>
    <tr>
      <td height="28" colspan="3" style="text-align:center"><br />
        *Manufacturer is Manditory, all other fields optional. </td>
      </tr>
  </table>

  <br />
<br />

<p><div id="finish">

</p></div>
</form>

这是应该返回第一个下拉列表结果的php页面,尽管第二个下拉表单实际上是相同的并且有效:

<?Php
@$cat_id=$_GET['cat_id'];
//$cat_id=2;


$dbhost_name = "localhost";
$database = "database";// database name
$username = "username"; // user name
$password = "password"; // password 

//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$str='';
$sql="SELECT Distinct Type FROM `tbl_Printers` where Manufacturer='$cat_id' order by Type asc";
foreach ($dbo->query($sql) as $row) {
$str=$str . "$row[Type]".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string

//$main = array($str);
echo json_encode($str); 
//echo $str;
?>

我感谢任何帮助,我尝试过搜索,通常我不会寻求帮助,但这一点远远超出了我的理解。

目前我已经将所有的Quere替换为这样的读取(很多人都在考虑分页......)我不认为这只是一个解决办法。

"SELECT Distinct Type FROM `tbl_Printers` where Manufacturer Like CONCAT('%', '$cat_id', '%') order by Type asc"

感谢Vigor指出我正确的方向。不幸的是,由于我的声誉,我无法选择评论作为答案,或者直到明天才能回答我自己的问题,但是非常感谢您找到问题。

导致问题的代码行是:

echo "<option value=$row[Manufacturer] $selected>$row[Manufacturer]</option>";

需要将其更改为此选项以根据需要添加引号,并仍然选择正确保存的项目:

echo "<option value='" . $row['Manufacturer']."' ".$selected." >" . $row['Manufacturer'] . "</option>";

2 个答案:

答案 0 :(得分:0)

更好地测试它,但我认为JS应该用+符号替换空字符串以便正确传递它:

替换: var cat_id = document.getElementById('name')。value;

使用: var cat_id = document.getElementById('name')。value.replace('/ / gi','+');

答案 1 :(得分:0)

您应该使用引号添加选项值,否则不会传递间隔值。

 <option value="Kyocera Mita" >Kyocera Mita</option>