Jump to content

Select Dropdown list


zeta1600

Recommended Posts

I have the following code currently:

<?php foreach ((array)$node->field_buy_at as $item) { ?>
<?php print $item['view'] ?>
<?php } ?> 

I would like to make the list a drop down with a link so that when a user selects, he goes to a new page. I tried the following:

        <select name="select">
      <?php foreach ((array)$node->field_buy_at as $item) { ?>
<?php
            $url = $node->field_buy_at[0]['url'];
            $store = $item['view'];
         ?>
        <? echo "<option value='$url'>$store</option>";?>
<?php } ?>
  </select>

I'm pretty sure it's this "$url = $node->field_buy_at[0]['url'];" that I don't have correct.

Link to comment
Share on other sites

Well, SergeiSS, that's a good question. It's because I'm not a programmer. I actually got something to work through fumbling the www. Here it is. If you have any suggestions on how to clean this up, I would appreciate it.

I have this to open in the same window (and another below to open in a new window):

      <!-- Pull Down Store -->
      <select onChange="window.location='<? print ($url); ?>'+this.value">
      <?php foreach ((array)$node->field_buy_at as $item) { ?>
<?php
        $store = $item['display_title'];
        $url = $item['display_url'];
        ?>
        <? echo "<option value='$url'>$store</option>";?>
<?php } ?>
  </select>
      <!-- Pull Down Store -->

 

Or open it in a new window:

<form action="../">
<select name="myDestination"
    onchange="this.form.WINDOW_NAMER.value++;
    ob=this.form.myDestination;
    window.open(ob.options[ob.selectedIndex].value
    ,'Window_Name'+this.value)">     
<?php foreach ((array)$node->field_buy_at as $item) { ?>
<?php
        $store = $item['display_title'];
        $url = $item['display_url'];
        ?>
        <? echo "<option value='$url' target='_blank'>$store</option>";?>
<?php } ?>
  </select>
      <input name="WINDOW_NAMER" type="HIDDEN" value="1">
      </form>

Link to comment
Share on other sites

OK, here are some edits to your code and some pointers:

 

1. Do not use PHP short tags (i.e. '<?') they have been deprecated and will not work on all servers

2. Create your code in a modular fashion so that maintenance/edits is easier. For example, you do not need two different blocks of code for opening in a separate or the same window.

3. As SergeiSS stated you do not need to exit/enter PHP tags like that. You can, but it makes the code more difficult to read.

4. Do not put "code" into your JavaScript event handlers. Create functions and then call the functions.

 

So, here is what I would do. Put this PHP code at the top of your script.

<?php

    //PHP configuration variable
    $newWindow = true; //Determines if the link will open in a new window
    //Generate a variable for the javascript code
    $newWindowJS = ($newWindow) ? 'true' : 'false';

    //Create the options for the destinations select field
    $destOptions = '';
    foreach ((array)$node->field_buy_at as $item)
    {
        $destOptions .= "<option value='{$item['display_url']}'>{$item['display_title']}</option>\n";
    }
?>

 

Add this javascript function to the HEAD of the page

<script type="text/javascript">

function openURL(selObj, newWindow)
{
    urlValue = selObj.options[selObj.selectedIndex].value;
    if(newWindow)
    {
        selObj.form.WINDOW_NAMER.value++;
        window.open(selValue, 'Window_Name'+urlValue);
    }
    else
    {
        window.location = '<?php echo $url; ?>' + urlValue;
    }
}

</script>

 

Lastly, create the form using this

<form action="../">
  <select name="myDestination" onchange="openURL(this, <?php echo $newWindowJS; ?>);">     
    <?php echo $destOptions; ?>
  </select>
  <input name="WINDOW_NAMER" type="HIDDEN" value="1">
</form>

 

Note: all the above was written on-the-fly and is not tested. So, there may be some errors or a couple minor bugs. But, with that logic you can easily change the behavior by simply changing the variable $newWindow to true or false based upon your needs.

Link to comment
Share on other sites

Wow, Psycho, thanks! (never thought I'd ever say those words together)

 

I'll give this a shot and let you know what other questions I have. I really hope this helps somebody else.

 

Boy, I have so often wished I knew php. Can you recommend a basic bare bones tutorial?

 

.... Hmmmm.... the selection didn't go anywhere nor opened a new window.

Link to comment
Share on other sites

Wow, Psycho, thanks! (never thought I'd ever say those words together)

 

I'll give this a shot and let you know what other questions I have. I really hope this helps somebody else.

 

Boy, I have so often wished I knew php. Can you recommend a basic bare bones tutorial?

 

.... Hmmmm.... the selection didn't go anywhere nor opened a new window.

 

There are plenty of tutorials out there. I learned through reading existing code snippets, tutorials, the manual and this forum. I think Tizag has some pretty good tutorials.

 

As for the code working, I really didn't expect it to work off the bat. I wrote all that code without any testing as I don't have your data to test against and I made some assumptions. I'll try some mock data to test it but I'm not going to spend too much time on it.

Link to comment
Share on other sites

OK, I had one typo in the code I provided. I changed a var name from selValue to urlValue and didn't change it in one place. The below is a complete page that works. The one thing I'm not sure if you need is the $url variable. Go ahead and try it against real data for both options. If one does not work you should see what URL is being used and how you need to modify it

 

<?php

//Test data
$node->field_buy_at[] = array('display_url'=>'page1.php', 'display_title'=>'One');
$node->field_buy_at[] = array('display_url'=>'page2.php', 'display_title'=>'Two');
$node->field_buy_at[] = array('display_url'=>'page3.php', 'display_title'=>'Three');

//PHP configuration variable
$newWindow = true; //Determines if the link will open in a new window
//Generate a variable for the javascript code
$newWindowJS = ($newWindow) ? 'true' : 'false';

//Create the options for the destinations select field
$destOptions = '';
foreach ((array)$node->field_buy_at as $item)
{
    $destOptions .= "<option value='{$item['display_url']}'>{$item['display_title']}</option>\n";
}

?>
<html>
<head>
<script type="text/javascript">

function openURL(selObj, newWindow)
{
    urlValue = selObj.options[selObj.selectedIndex].value;
    if(urlValue=='') { return false; }
    if(newWindow)
    {
        selObj.form.WINDOW_NAMER.value++;
        window.open(urlValue, 'Window_Name'+urlValue);
    }
    else
    {
        window.location = '<?php echo $url; ?>' + urlValue;
    }
}

</script>
</head>

<body>

<form action="../">
  <select name="myDestination" onchange="openURL(this, <?php echo $newWindowJS; ?>);">
    <option value=''>-- Select One --</option>  
    <?php echo $destOptions; ?>
  </select>
  <input name="WINDOW_NAMER" type="HIDDEN" value="1">
</form>

</body>
</html>

 

Also, you should add one entry as the first item in the select list - else the user cannot "select" the first value since it is already selected so there would be no onchange event. I've made those changes above.

Link to comment
Share on other sites

  • 1 month later...

Psycho... I ended up using the following to open in a new window. It works fine in almost all browsers except for Internet Explorer. Can you help?

 

<form action="../">
<select name="myDestination"
    onchange="this.form.WINDOW_NAMER.value++;
    ob=this.form.myDestination;
    window.open(ob.options[ob.selectedIndex].value
    ,'Window_Name'+this.value)">     
<?php foreach ((array)$node->field_buy_at as $item) { ?>
<?php
        $store = $item['display_title'];
        $url = $item['display_url'];
        ?>
        <? echo "<option value='$url' target='_blank'>$store</option>";?>
<?php } ?>
  </select>
      <input name="WINDOW_NAMER" type="HIDDEN" value="1">
      </form>

Link to comment
Share on other sites

Psycho... I ended up using the following to open in a new window. It works fine in almost all browsers except for Internet Explorer. Can you help?

 

Sounds like a JavaScript problem. To debug those I suggest generating a complete/working HTML page. Save it as a flat file. Then, work on the JavaScript code there to get it working. Once you have it working then you can determine what changes are needed in the PHP script.

 

Trying to fix JavaScript problems in a PHP script can be very difficult.

Link to comment
Share on other sites

Is PHP the best way to open the new window.

 

Zeta said she hoped this helps someone else, so here i am thinking it through.  I have a PHP for putting data into a DB.  The idea is that someone else will be able to access the column totals periodically.

 

My structure is to have another form with options.  Each option will run into a SELECT function that will provide information.  Normally, this would be displayed on the browser, but I've found (not yet implemented) that Javascript can open a NewWindow and should be able to make the display there WITHOUT involving PHP.

 

Am I doing something that makes sense, or sounds stupid?  Feasible or futile?

Link to comment
Share on other sites

Is PHP the best way to open the new window.

 

Zeta said she hoped this helps someone else, so here i am thinking it through.  I have a PHP for putting data into a DB.  The idea is that someone else will be able to access the column totals periodically.

 

My structure is to have another form with options.  Each option will run into a SELECT function that will provide information.  Normally, this would be displayed on the browser, but I've found (not yet implemented) that Javascript can open a NewWindow and should be able to make the display there WITHOUT involving PHP.

 

Am I doing something that makes sense, or sounds stupid?  Feasible or futile?

 

PHP cannot open windows (or do anything on the client-side). But, you are probably misunderstanding some thing. The JavaScript should open the new window - but it would be calling a PHP script to generate the output in that page.

Link to comment
Share on other sites

In case no one was clear as to why you dont need to put php tags <?php *******php code********  ?> on every line its because you only use these php tags when you are going in and out of php.

 

So for example

<html>
<body>

<?php // php code here. 

?> im ending the tag here cause the php code stops and html starts

<form method="post" action="">
<input type="password" name="confirm" id="confirm" />
</form>

<?php // php code here
?> im ending the tag here cause the php code stops and html starts

</body>
</html>

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.