Kevin.a
Kevin.a

Reputation: 4296

If option is selected do something in php

I have this code :

<html>

<head>    
</head>

<body>
    <select>    
        <?php 

            $colors = array("red", "green", "blue", "yellow"); 
            foreach ($colors as $value) {
                echo "<option>$value</option>";
            }

            if (in_array("blue",$value)) {
                echo "Blue is your color";         
            } else {
                echo "blue is not your color get out of here";
            }

        ?>
    </select>
</body>

</html>

Basically there is a variable which is an array containing different colors. I put that variable in a for loop inside a select, so people can select the options. I'm trying to achieve when people select the array attribute blue to echo something.

My attempt:

 if (in_array("blue",$value)) {
                echo "Blue is your color";         
            } else {
                echo "blue is not your color get out of here";
            }

I did some research before coming here, but found nothing or i didn't really know what to look for, but this is my attempt, I tried!

Upvotes: 0

Views: 9105

Answers (6)

Huseyin
Huseyin

Reputation: 567

<select onChange="window.top.location='lan.php?lan='+this.value;">
  <option>----</option>
  <option value="en">English</option>
  <option value="tr">Turkce</option>
</select>

Upvotes: -1

moni_dragu
moni_dragu

Reputation: 1163

You need javascript or jquery to achieve what you want. Here is a pure javascript example:

HTML

<head>    
</head>

<body>
    <select onchange="checkColor(this);">    
        <?php 

            $colors = array("red", "green", "blue", "yellow"); 
            foreach ($colors as $value) {
                echo "<option>$value</option>";
            }
        ?>
    </select>
</body>

</html>

Javascript

<script>
function checkColor(selectObj){
if (selectObj.selectedIndex == -1){
        selectedColor = '';
}
else {
    selectedColor = selectObj.options[selectObj.selectedIndex].text;
}

if(selectedColor == 'blue'){
    alert('Blue is your color');
}
else {
   alert("blue is not your color get out of here");
}
}
</script>

Upvotes: 0

Pablo Arredondo
Pablo Arredondo

Reputation: 1

PHP doesn't handle user events. Use jQuery:

<body>
<script   src="https://code.jquery.com/jquery-3.1.1.min.js"   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   crossorigin="anonymous"></script>
<select id="select1">

    <?php 
    $colors = array("red", "green", "blue", "yellow"); 
    foreach ($colors as $value) {
        echo "<option value='$value'>$value</option>";
    }   
    ?>
</select>
<input type="text" id="selected">
<script>
    $('#select1').on('change', function() {
        $('#selected').val( this.value );
    });
</script>

Upvotes: 0

devpro
devpro

Reputation: 16117

If you want to get values on client side than you can use jQuery or Javascript here, or if you want to use server side than you must need to use <form> here:

Here is the basic example with jQuery you can modify as per your need, i am using result in alert box you can display it anywhere where you need:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<select name="select" id="select">    
<option value="">Select</option>
<?php 
$colors = array("red", "green", "blue", "yellow"); 
foreach ($colors as $value) {
?>
  <option value="<?=$value?>"><?=$value?></option>
<?php
}
?>
</select>

jQuery:

<script type="text/javascript">
$(document).ready(function(){
  $("#select").change(function(){
    selectVal = $(this).val();
    myarray = ["red", "green", "blue", "yellow"];
    if($.inArray(selectVal, myarray) !== -1){
      alert(selectVal+' is your color');
    }
    else{
      alert(selectVal+' is not your color get out of here');
    }
  });
});
</script>

Make sure, you have included the jquery file in your code <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

Upvotes: 3

Karthi
Karthi

Reputation: 528

if you use javascript to do that in easy way. before that you need to use form tag. using onchange="myfunction(this.value)" method to get currect selected values form select box.

    <head>    
    <script>
    function myfunction(x)
    {
        alert(x);
    }
    </script>
    </head>

    <body>
    <form action="" method="post">
        <select id="myvalue" onchange="myfunction(this.value)">    
            <?php 

                $colors = array("red", "green", "blue", "yellow"); 
                foreach ($colors as $value) {
                    echo "<option>$value</option>";
                }

                if (in_array("blue",$value)) {
                    echo "Blue is your color";         
                } else {
                    echo "blue is not your color get out of here";
                }

            ?>
        </select>
        </form>
    </body>

    </html>

Upvotes: 1

Abhijit Jagtap
Abhijit Jagtap

Reputation: 2702

You should write

 <html>

    <head>    
    </head>

    <body>
<?php 
$colors = array("red", "green", "blue", "yellow"); 
if(isset($_GET)&&$_GET["colors"]!=""){
if (in_array($_GET["colors"],$colors)) {
                    echo "$_GET["colors"] is your color";         
                } else {
                    echo "blue is not your color get out of here";
                }
 }
?>
<form method="get">
        <select name="colors">    
            <?php    

                foreach ($colors as $value) {
                    echo "<option value='".$value."'>$value</option>";
                }
            ?>
        </select>
<input type="submit"/>
</form>
    </body>

    </html>

in_array

(PHP 4, PHP 5, PHP 7) in_array — Checks if a value exists in an array

Description

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) Searches haystack for needle using loose comparison unless strict is set.

Parameters

needle The searched value.

Note: If needle is a string, the comparison is done in a case-sensitive manner. haystack The array.

strict If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

Return Values

Returns TRUE if needle is found in the array, FALSE otherwise.

refer http://php.net/manual/en/function.in-array.php

Upvotes: 1

Related Questions