Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19250

Re: trying to call onclick with document.getElement() - document.getElement() returns null

Message-ID <7902669.URqpOMyR7R@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-04-13 10:29 +0200
Subject Re: trying to call onclick with document.getElement() - document.getElement() returns null
Newsgroups comp.lang.javascript
References <17650677-77bd-4bca-8e10-ed45772f2e9f@googlegroups.com> <87fvyv1r7q.fsf@gmail.com>

Show all headers | View raw


Mikael Andersson wrote:

> jorythompson@gmail.com writes:
> 
> [snip]
>> 
*****************************************************************************
>> <?php
>>   require($_SERVER['DOCUMENT_ROOT'].'/../cgi-bin2/db.php');

Use “require_once”, remove the parentheses (it is not a function call), and 
see <http://php.net/include_path> and <http://php.net/autoload> for how to 
avoid this partially or altogether.

>>   function showCitiesCombo($name) {
>>     $result = mysql_query("SELECT distinct city FROM Lot ORDER BY city");

Never do it that way, see the warning in <http://php.net/mysql_query>.

>>     echo 'Select a City:<br>';
>>     echo '<select name="' . $name . '" multiple>';
>>       echo '<form>';

Never do it that way; write templates that you include, as a first step to 
separate presentation from business logic.  See also: MVC.

>>         while($row = mysql_fetch_array($result)) {

Make it clear that you are evaluating the right-hand side of the assignment:

  while (($row = mysql_fetch_array($result))) {

But see above.

>>           if ($row['city'] != "") {
> 
> [snip]
>>     <script>
>>       function searchForLots() {
>>         street = document.getElementById('street');
>>         city = document.getElementById('cities');
>>         state = document.getElementById('state');

You have neglected to declare your identifiers as local variables.  So by 
assignment they become names of properties of the Global Object, if that.
Instead:

  var street = …
  …

>>         if (window.XMLHttpRequest) {

Insufficient and error-prone, see isHostMethod().

>>           // code for IE7+, Firefox, Chrome, Opera, Safari

Learn to think in terms of layout engines and object models, not browsers.

<http://www.w3.org/TR/XMLHttpRequest/>

> You should either use document.getElementsByName('street'), which
> returns an array.

No, it does not.  It returns a reference to an object that implements the 
NodeList interface.  In some DOM implementations (like the Gecko DOM), that 
object also implements the HTMLCollection interface.

<http://www.w3.org/TR/2012/CR-html5-20121217/dom.html#dom-document-
getelementsbyname>

> Or change the select to: (you can in php write)
> 
> echo "<select name='$name' id='$name'>";
>                            ^^
> easier to read and understand. When using " double quotes you can have
> php variables inside.

  <select name="<?php echo ($name = htmlspecialchars($name, …)); ?>" 
          id="<?php echo $name; ?>">

or (PHP 5.4+)

  <select name="<?= ($name = htmlspecialchars($name, …)); ?>" 
          id="<?= $name; ?>">

is recommended instead.  Works better with templates, too.  However, a case 
could be made for DRY, so that

  <select <?php
    echo sprintf('name="%s" id="%*1$s"', htmlspecialchars($name, …));
  ?>>

becomes an option.

<http://php.net/manual/en/tutorial.firstpage.php>
<http://php.net/htmlspecialchars>
<http://php.net/manual/en/migration54.new-features.php>
<http://php.net/sprintf>

Note that IDs must be unique in an element's home subtree; names for 
controls do not need to.

<http://www.w3.org/TR/2012/CR-html5-20121217/dom.html#the-id-attribute>
<http://www.w3.org/TR/2012/CR-html5-20121217/forms.html#attr-fe-name>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

trying to call onclick with document.getElement() - document.getElement() returns null jorythompson@gmail.com - 2013-04-12 10:45 -0700
  Re: trying to call onclick with document.getElement() - document.getElement() returns null Jordan Thompson <jorythompson@gmail.com> - 2013-04-12 11:01 -0700
  Re: trying to call onclick with document.getElement() - document.getElement() returns null Mikael Andersson <qubex2@gmail.com> - 2013-04-12 21:31 +0200
    Re: trying to call onclick with document.getElement() - document.getElement() returns null Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-13 10:29 +0200
      Re: trying to call onclick with document.getElement() - document.getElement() returns null Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-04-13 11:10 +0200
        Re: trying to call onclick with document.getElement() - document.getElement() returns null Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-13 11:45 +0200
          Re: trying to call onclick with document.getElement() - document.getElement() returns null Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-04-13 13:04 +0200
            Re: trying to call onclick with document.getElement() - document.getElement() returns null Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-13 13:26 +0200
              Re: trying to call onclick with document.getElement() - document.getElement() returns null Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-04-13 21:12 +0200
                Re: trying to call onclick with document.getElement() - document.getElement() returns null Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-13 21:31 +0200
                Re: trying to call onclick with document.getElement() - document.getElement() returns null Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-04-16 20:01 +0200
                Re: trying to call onclick with document.getElement() - document.getElement() returns null Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-16 23:13 +0200
      Re: trying to call onclick with document.getElement() - document.getElement() returns null Mikael Andersson <qubex2@gmail.com> - 2013-04-13 18:35 +0200

csiph-web