Jump to content

Dynamic javascript function


cmattoon

Recommended Posts

I have two SELECT boxes that are dynamically generated by PHP, via the 'create_dropdown' function (below). The java code "should" update the value of a textbox with "Error" if both SELECTs are equal to each other.

 

I keep getting a "searchpickup is undefined" javascript error (in the bottom-left of status bar), and nothing happens.

 

I've tried moving the "script" tags, changing id names, using document.getElementById(name).value, document.getElementById(name).selected, document.entryform.[name].selected, document.entryform.[name].value

 

PHP - create_dropdown:

function create_dropdown($indentifier,$pairs,$firstentry,$jname)
        {
        // starts the dropdown list with the <select> element and title
        $dropdown = "<select name=\"$indentifier\" id=\"$jname\" onBlur=\"validate_facility();\">";
        $dropdown .= "<option name=\"\">$firstentry</option>";
        
        // Create the dropdown elements
        foreach($pairs AS $value => $id)
            {
            $dropdown .= "<option value =\"$value\">$id</option>";
            }
        //conclude the dropdown and return it
            return $dropdown;
		echo "</select>";
        }

 

Javascript:

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

				pickup = document.getElementById(searchpickup).selected;

				dropoff = document.getElementById(searchdropoff).selected;


				yeserror = "Error!";
				noerror = "No Error";
				if(pickup == dropoff){
					document.entryform.warningbox.value = yeserror;

				}else{
					document.entryform.warningbox.value = noerror;
				}
			}

			</script>

 

 

The main document (entry.php) generates a "regular" html-like page, with some inline PHP content:

<td>
     <?php
while($row = mysql_fetch_array($result3))
{
$value = $row["fac_name"];
$id = $row["fac_name"];
$pairs["$value"] = $id;
                }
                
echo create_dropdown("searchdropoff", $pairs, "Choose one","searchdropoff","searchdropoff");
echo "</select>";
?>
</td>

(This is repeated with "searchpickup")

 

Basically...if the user picks the same pick up and drop off locations:

--- the textbox "warningbox" should have a value of "Error, you're a dumbass" or something

--- and the "submit" button should be disabled (name/id="button1", type="submit")

 

Otherwise, the textbox will:

--- preferably be hidden

--- otherwise have a value of "Press OK to continue" or something.

 

 

 

Link to comment
Share on other sites

You need to take a close look at your create_dropdown() function.  You have a slight logic error there that is generating invalid HTML which could lead to problems:

            return $dropdown;
                           echo "</select>";
        }

you are returning the SELECT code you just generated without the closing tag, and then echo-ing the closing tag. I suspect the closing tag is appearing in the HTML before the actual SELECT tag.  Which is probably why you added a closing SELECT tag after your call to this function in the main document.

 

The reason you get that javascript message is that these two statements are using variables as the element IDs and the variables are not defined. 

               pickup = document.getElementById(searchpickup).selected;

               dropoff = document.getElementById(searchdropoff).selected;

you probably meant to put those in quotes:

               pickup = document.getElementById('searchpickup').selected;

               dropoff = document.getElementById('searchdropoff').selected;

Link to comment
Share on other sites

I'm not sure about the closing SELECT tag.. this page was written by another guy working on the project, and I can't reach him... the function has worked for now, so i've left it alone....

 

 

I replaced the names with 'name', as you said, and the 'warningbox'.value is getting changed, but it's always "Error!" (yeserror value).

 

when I view source of the generated page, everything seems to be intact (names are right, etc), so I'm guessing it's now an issue of my if-then statement?

 

 

(Javascript)

yeserror = "Error!";
noerror = "No Error";
if(pickup == dropoff){
     document.getElementById('warningbox').value = yeserror;
}else{
     document.getElementById('warningbox').value = noerror;
}

Link to comment
Share on other sites

Note: I've also tried replacing "else" with:

 

}elseif(searchpickup != searchdropoff){

 

This results in an "object expected" error on Line 53 (which, in the generated page is)

<select name....onBlur="validate_facility();" />

 

Without the elseif, it doesn't expect an object??? :confused:

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.