user1459766
user1459766

Reputation: 118

Javascript variable cut off after whitespace

I have a PHP page that I'm using JavaScript with to update dropdown lists based on MySQL results. My problem is that when I pass a customer name via JavaScript variable it cuts off at the first white space. For example: "Home Depot" would show up as "Home" or "The Home Store" shows up as "The". In order to execute a proper query I would like for the whole string to be passed.

Here is the relevant code from my form:

    function getXMLHTTP() { //function to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
      }


    function getCustCont(cID)
    {
    var strURL="Includes/cdd.php?custid="+cID;
    var req = getXMLHTTP();
    if (req)
    {
      req.onreadystatechange = function()
      {
       if (req.readyState == 4)
       {
     // only if "OK"
     if (req.status == 200)
          {
        document.getElementById('Contdiv').innerHTML=req.responseText;
     } else {
        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
      }
        }
       }
    req.open("GET", strURL, true);
    req.send(null);
    }

//VARIABLES DEFINED AND START OF FORM

    <td width="60%">

    <select name="Customer" onchange="getCustCont(this.value)">
    <option value="SAC">Select Customer</option>
    <?php
    $i=0;
    while ($i < $num) {
    $Name=mysql_result($result,$i,"Name");

    echo "<option value=$Name>$Name</option>";
    $i++;
    }       
    ?>  

    </select>
    </td>

the cdd.php file includes the following:

  <? 
    $Cust_ID = $_GET['custid'];
    include('../Includes/TrackingDB.php');

    mysql_connect($dbhost,$dbuser,$dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbdb) or die('Error connecting to database');

    $query="SELECT t1.ContName FROM Cust_Contacts t1 INNER JOIN Customer_Listing t2 ON t1.Cust_ID = t1.Cust_ID WHERE t2.Name = '$Cust_ID'";
    $result=mysql_query($query);

    mysql_close();

    echo var_dump($Cust_ID); //I added this solely for the purposes of trouble shooting this issue. This is where I am seeing the truncated string.

    ?>
    <select name="Cust_Buyer" onchange="getCity(<?=$country?>,this.value)">
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value="<?=$row['ContName']?>"><?=$row['ContName']?></option>
    <? } ?>
    </select>

Again, within the cdd.php file the string truncates at white space.

Upvotes: 1

Views: 1294

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117334

encode cID

var strURL="Includes/cdd.php?custid="+encodeURIComponent(cID);

and surround the value with quotes:

echo "<option value='$Name'>$Name</option>";

Upvotes: 2

Related Questions