1

I want create an dynamic HTML page were the class name is defined by the php program

My code:

<?php

 error_reporting(E_ALL);
 ini_set("display_errors", 1);
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function hideTd(){
    var elements = document.getElementsByClassName("ca");
    for(var i = 0, length = elements.length; i < length; i++) {
          elements[i].style.display = 'none';
    }
  }
</script>
</head>
<body onload="hideTd()">
<select name="retailerselect" id="retailerselect" onchange="retailerSelect()" width: 66px;> 
  <option selected="selected" size="10" >--SELECT--</option>
  <option class="ca">hi</option>
  <option class="ca">this</option>  
          <?php $connect = " yes-ad";?>
                <option class="<?php echo $connect;?>"> <?php echo $connect;?> </option>

  </select>
 </body>
  </html>

I the HTML script i would have hide the value of option tag in the class "ca"

Input:

$connect:"yes-ad"

Output:

class="ad" value in the option ="yes-ad"

Sorry if my script is wrong i am a newbie in HTML or is there any other way to do this

Edit: Simplified the script

The Output iam getting now

enter image description here

I want it to show yes-ad with its class

3
  • I do not at all understand what you are trying to do but the variable definition ` $connect = "yes-ad" ` in your code is outside of the php tags <?php and ?>. Commented Feb 4, 2015 at 7:21
  • @Bowdzone is this correct now Commented Feb 4, 2015 at 7:24
  • The input for $connect will be given from python variable i have to match the variable with reg-ex the matched variable should be given to class and the whole variable should go to value Commented Feb 4, 2015 at 7:28

2 Answers 2

3

The php code is wrong. There should not be { and } .Just remove them.

And to change the classname, just do like this:

<option class="<?php echo $connect;?>">....</option>
Sign up to request clarification or add additional context in comments.

6 Comments

@VigneshKalai You want to change the class name?
yes i want to change the class name and assign a value
i want it to be like this <option class="<?php echo $connect;?>"> <?php echo $connect;?> </option> is this possible
although the code looks not beauty, but it works, you can do this.
But nothing is shown only --select-- is shown the drop down
|
1
<?php

 error_reporting(E_ALL);
 ini_set("display_errors", 1);
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function hideTd(){
    var elements = document.getElementsByClassName("ca");
    for(var i = 0, length = elements.length; i < length; i++) {
          elements[i].style.display = 'none';
    }
  }
</script>
</head>
<body onload="hideTd()">
<select name="retailerselect" id="retailerselect" onchange="retailerSelect()" width: 66px;> 
  <option selected="selected" size="10" >--SELECT--</option>
  <option class="ca">hi</option>
  <option class="ca">this</option>  
          <?php $connect = " yes-ad";?>
                <option class="<?php echo $connect;?>"> <?php echo $connect;?> </option>

  </select>
 </body>
  </html>

6 Comments

<option class="<?php echo $connect?>"> <?php echo $connect?> </option> is this possible
@yd600lty like he said there is no need for the brackets
@VigneshKalai you should look on PHP file as HTML only that you can inject PHP code when you open PHP tag <?php ?> there is also short tag support and can be enabled in the php.ini config file, these short tag make your coding easier and shorter,e.g. <?=$param?> will do the same thing as <?php echo $param?>
in case you dont see anything it probably fatal error, and display errors is disabled, add the following lines to the top of your page: error_reporting(E_ALL); ini_set("display_errors", 1);
because of this JS line: elements[i].style.display = 'none'; It hides all ca classes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.