If I have dropdownlist as below

<select onChange="this.value;">  
  <option value="Volvo" id="volvo">Volvo</option>    
  <option value="Saab" id="saab">Saab</option>    
  <option value="VW" id="vw">VW</option>    
  <option id="audi" value="Audi" selected>Audi</option>                  
</select> 

I want to display discription of item selectd so I have below code

<div id="boxpopup" class="box">  
    <a onClick="closeOffersDialog('boxpopup');" class="boxclose"></a>  
    <div id="content">  
    This is popupbox of Audi   
    </div><!--end of content --> 
</div><!--end of popupbox -->     

now in content can I give If else as below

    <div id="boxpopup3" class="box">  
        <a onClick="closeOffersDialog('boxpopup');" class="boxclose"></a>  
        <div id="content"> 
            <?php if($id=="volvo"){ ?> 
            This is popupbox of Volvo
            <?php } elseif($id=="saab"){ ?>
            This is popupbox of Saab 
            <?php } elseif($id=="vw"){ ?>
            This is popupbox of VW 
            <?php } else { ?>
            This is popupbox of Audi
            } ?>
        </div><!--end of content --> 
    </div><!--end of popupbox -->

The error is occurs
Notice: Undefined variable: id
please help me :(

Recommended Answers

All 4 Replies

Well what it says is that you have not defined $id. $id is empty, and therefore you get an error and your script is not working. If you do not define $id in a way such as this:

$id = 'yourvalue';

Then it will be undefined and you will not be able to work with it. PHP cannot check your <div>'s id. You should use Javascript for that :). Does this answer your question?

my index.php page

<select onChange="openOffersDialog(this.value);" name="cars" id="cars">  
  <option value="Volvo" id="Volvo">Volvo</option>    
  <option value="Saab" id="Saab">Saab</option>    
  <option value="VW" id="VW">VW</option>    
  <option id="Audi" value="Audi" selected>Audi</option>                  
</select> 

    <?php 
    $cars = $_POST['cars'];
    if($id=="Volvo"){ ?>
    <table width="50%" border="0">
      <tr>
        <td><a href="http://www.audi.in/" target="_blank"><img src="images/pdf.png"></a></td>
        <td><a href="http://www.javaranch.com/" target="_blank"><img src="images/pdf.png"></a></td>
      </tr>
      <tr>
        <td>audi1</td>
        <td>audi2</td>
      </tr>      
    </table>
    <?php } elseif($id=="s"){ ?>
    <table width="50%" border="0">
      <tr>
        <td><a href="http://www.audi.in/" target="_blank"><img src="images/pdf.png"></a></td>
        <td><a href="http://www.javaranch.com/" target="_blank"><img src="images/pdf.png"></a></td>
      </tr>
      <tr>
        <td>audi1</td>
        <td>audi2</td>
      </tr>      
    </table>    
    <?php } elseif($id=="v"){ ?>
    <table width="50%" border="0">
      <tr>
        <td><a href="http://www.audi.in/" target="_blank"><img src="images/pdf.png"></a></td>
        <td><a href="http://www.javaranch.com/" target="_blank"><img src="images/pdf.png"></a></td>
      </tr>
      <tr>
        <td>audi1</td>
        <td>audi2</td>
      </tr>      
    </table>    
    <?php } if($id=="s"){ ?>    
    <table width="50%" border="0">
      <tr>
        <td><a href="http://www.audi.in/" target="_blank"><img src="images/pdf.png"></a></td>
        <td><a href="http://www.javaranch.com/" target="_blank"><img src="images/pdf.png"></a></td>
      </tr>
      <tr>
        <td>audi1</td>
        <td>audi2</td>
      </tr>      
    </table>    
    <?php }else{ ?>   
    <table width="50%" border="0">
      <tr>
        <td><a href="http://www.audi.in/" target="_blank"><img src="images/pdf.png"></a></td>
        <td><a href="http://www.javaranch.com/" target="_blank"><img src="images/pdf.png"></a></td>
      </tr>
      <tr>
        <td>audi1</td>
        <td>audi2</td>
      </tr>      
    </table>
    <?php } ?>         

<select onChange="openOffersDialog(this.value);

This calls a javascript function not php

You will usually get the selected ID upon submiting the form. The selected value will be stored in either $_GET or $_POST global array, depending on the method, defined in the <form> tag. So in your code you have to check for submission of the form using this pattern:

if(isset($_POST['submit']))

or if you do not have a submit button using some other method (it might be in the openOffersDialog javascripot function).

Then before using a value you have to check for existence of it, again with isset():

 <div id="content">
<?php if(isset($_POST['id']) && $_POST['id']=="volvo"){ ?>
This is popupbox of Volvo
<?php } elseif(isset($_POST['id']) && $_POST['id']=="saab"){ ?>
This is popupbox of Saab
<?php } elseif(isset($_POST['id']) && $_POST['id']=="vw"){ ?>
...

This is just showing how the principle goes. You might need to adapt it to your needs.

AND: security aspect has not been taken into account in the above code. You have to check for the validity of submitted data. How to check it depends on the context where the data will be used. If you intend to put it into HTML (display it on the page), you at least have to use htmlspecialchars() function to replace dangerous characters with their HTML entities. If you want to stick the data in the database, you have to escape it.

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.