Jump to content

not sure how to explain - onchange(this.value) trimming values which have spaces


glennnall

Recommended Posts

having this:


$make = "<select name='make' id=\"make\" onchange=\"showModels(this.value)\">";
		 $make .= "<option value=\".$row['value'].\">Value</option>";

 

which calls this function:


function showModels(str)
{
if (str=="")
  {
  document.getElementById("models").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("models").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","models.php?make="+str,true);
xmlhttp.send();


 

if the value being returned has spaces, like "Convertible 2Door", it's only passing the value "Convertible" (causing some pretty poor queries, ya know)...

 

can anyone see why? any idea what i'm talking about?

 

 

Link to comment
Share on other sites

Well, I can see 2 areas of possible problems.

 

The least likely of the two is with the with the request URL, you may want to URL encode that (though now that I think about the values and the string you're sending, I am not sure it would do anything). URL encoding in JavaScript is broken into detail here - http://www.javascripter.net/faq/escape.htm - however, this is probably not the problem.

 

Have you checked the return value in some dev tools? Cause that would be a great way to see if the problem is server-side, or client side, or with the initial post I suppose.

 

I am figuring its server-side with the page you are posting to. Can you give me a link to this page on the web? And more importantly, can you post the code on the file you are requesting with AJAX? The "handling" code that would build your return.

Link to comment
Share on other sites

i appreciate your assistance. i ended up just replacing the spaces with _ in php and changing them back once the value was passed. worked out, and i don't know javascript well enough at all to try to understand why it wasn't passing the entire value as 'str' ...

 

thanks again

Link to comment
Share on other sites

No problem. Glad I could help... in some way I guess.  :)

 

If you want, you could check out my site and keep in contact. I often like helping people with programming and javascript is one language I am pretty good with. I just can help more if the question is directed to me. evileyegames.com - we have a programming help forum to... so, maybe useful for you. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.