how to get nid of a node when user is not on a node page I had made a custom form and want a field in it that would work as a node reference field. Currently iam not on the node page but i want the nid of nodes of a content type to be the options of the select field which is my node reference field. here is the code for the select field that should work as node reference field.



function formModule_form($form, &amp;$form_state) {



    $form['selectbox'] = array(
    '#type' =&gt; 'select', 
    '#title' =&gt; t('What is your Qualification?'), 
    '#required' =&gt; TRUE,
    '#options' =&gt; array(
      '1' =&gt; 'UnderGraduate',
      '2' =&gt; 'Graduate',
      '3' =&gt; 'Post Graduate',
         ),
    '#description' =&gt; t('Choose an option.'),
  );

return $form
}




the #option in the above code should have the nid of nodes.
You'll want to Query the database for the nodes you need and then use the results of that query to populate the #options property. Something like this:

&lt;?php
    $result = db_select('node', 'n')
    -&gt;fields('n')
    -&gt;condition('type', 'node_type_name','=')
    -&gt;condition('status', 0,'&gt;')
    -&gt;execute()
    -&gt;fetchAssoc();
?&gt;


You can then use the results of that query easily since it comes back as an associative array. You'll want to specify exactly which fields you want (in your case, probably just title and node. See http://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7 for how to do a proper select.