I need to make this code set "str" as "Recent" if the "str" got no value.
Right now i have to use this:

<form>
<select name="users" onChange="showUser(this.value)">
<option value="">Select album:</option>
<option value="Recent">Somealbumname</option>
</select>
</form>

but its really annoying to have to use the select, i want the code to load with the "str" as "Recent" if its not declared already.

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

if (str=="")
  {
  document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","xxxx/xxxx.php?q="+str,true);
xmlhttp.send();
}
</script>

Recommended Answers

All 3 Replies

If you want it that way, then you need to modify the page you are displaying, not Ajax part. You just load the "Recent" data to the page and remove the "Select Album" option from the list.

Do you mean something like <body onload="showUser(this.value)"> ?
im not sure how i should make the ajax run with str as "Recent"

Nope, you don't need to call Ajax when you first load the page. You just simply load the value ready for "Recent" and display it on the page. Then you could get rid of "Select Album" option and let the select tag on "Recent" option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.